Ghua975 2023-01-01 15:17 采纳率: 83.3%
浏览 39
已结题

C语言实现二维数组计数

对于炸弹数组,如果坐标(i,j)处的元素为0表示没有炸弹,1表示有炸弹。编写一个函数,计算数组中炸弹的总数。,怎样才能表示坐标(i,j)来完全实现这个代码呢?以下是我的一些思路,想了很久还是不能完整地实现这个计算数组的功能,该怎样调整呢?

#include <stdio.h>
int CountBomb(int a[10][10])
int count = 0;         /*declaration*/
 while (a=1)
 {
  ++count;
  ++a;
 }
 return count;
                            /* for (count = 0; *p != '\0'; p++)
                              count++;
                             This for loop is in the same function of the while loop */
}
int main(void)
{
       int a[10][10] = {{},{},{},{0,0,0,1},{0,0,0,0,1},{},{},{},{},{}};
    int rows;  //Row loop variable
    int cols;  //Column loop variable
    for (rows=0; rows<10; ++rows)
    {
        for (cols=0;cols<10; ++cols)
        {
            printf("%2d", a[rows][cols]);
        }
        printf("\n");
    }
    return 0;
    int sum;
    sum = CountBomb(arr,10,10);
    printf("Count is %d.\n",sum);

    return 0;
}

  • 写回答

2条回答 默认 最新

  • 会修bug的猫 2023-01-01 16:35
    关注
    
    #include <stdio.h>
    int CountBomb(int(*a)[10], int rows, int cols)
    {
        int count = 0;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (a[i][j])
                    ++count;
            }
        }
        return count;
    }
    int main(void)
    {
        int a[10][10] = { {},{},{},{0,0,0,1},{0,0,0,0,1},{},{},{},{},{} };
        int rows;  //Row loop variable
        int cols;  //Column loop variable
        for (rows = 0; rows < 10; ++rows)
        {
            for (cols = 0; cols < 10; ++cols)
            {
                printf("%2d", a[rows][cols]);
            }
            printf("\n");
        }
        int sum;
        sum = CountBomb(a, 10, 10);
        printf("Count is %d.\n", sum);
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月16日
  • 已采纳回答 1月8日
  • 创建了问题 1月1日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统