Ghua975 2023-01-01 07: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 08: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月15日
  • 已采纳回答 1月8日
  • 创建了问题 1月1日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部