寰宇榛仁 2022-02-28 11:00 采纳率: 66.7%
浏览 43
已结题

C Primer Plus (第6版)中文版 P267中程序清单10.18 vararr2d.c程序,运行错误

C Primer Plus (第6版)中文版 P267中程序清单10.18 vararr2d.c程序,运行错误,麻烦各位,怎么修改才不会有错误

#include <stdio.h>
#define ROWS 3
#define COLS 4
int sum2d(int rows, int cols, int ar[rows][cols]);
int main(void)
{
    int i, j;
    int rs = 3;
    int cs = 10;
    int junk[ROWS][COLS] = {
        {2, 4, 6, 8},
        {3, 5, 7, 9},
        {12, 10, 8, 6}
    };

    int morejunk[ROWS - 1][COLS + 2] = {
        {20, 30, 40, 50, 60, 70},
        {5, 6, 7, 8, 9, 10}
    };

    int varr[rs][cs];

    for (i = 0; i < rs; i++)
        for (j = 0; j < cs; j++)
            varr[i][j] = i * j + j;
    printf("3x4 array\n");
    printf("Sum of all elements = %d\n", sum2d(ROWS, COLS, junk));

    printf("2x6 array\n");
    printf("Sum of all elements = %d\n", sum2d(ROWS - 1, COLS + 2, morejunk));

    printf("3x10 VLA\n");
    printf("Sum of all elements = %d\n", sum2d(rs, cs, varr));

    return 0;
}

int sum2d(int rows, int cols, int ar[rows][cols])
{
    int r;
    int c;
    int tot = 0;

    for (r = 0; r < rows; r++)
        for (c = 0; c < cols; c++)
            tot += ar[r][c];

    return tot;
}

img

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-02-28 11:56
    关注

    修改见注释,供参考:

    #include <stdio.h>
    #define ROWS 3
    #define COLS 4
    
    int sum2d(int rows, int cols, int* ar);
    int main(void)
    {
        int i, j;
        const int rs = 3;   //修改
        const int cs = 10;  //修改
        int junk[ROWS][COLS] = {
            {2, 4, 6, 8},
            {3, 5, 7, 9},
            {12, 10, 8, 6}
        };
    
        int morejunk[ROWS - 1][COLS + 2] = {
            {20, 30, 40, 50, 60, 70},
            {5, 6, 7, 8, 9, 10}
        };
    
        int varr[rs][cs];
    
        for (i = 0; i < rs; i++)
            for (j = 0; j < cs; j++)
                varr[i][j] = i * j + j;
        printf("3x4 array\n");
        printf("Sum of all elements = %d\n", sum2d(ROWS, COLS, junk[0]));//修改
    
        printf("2x6 array\n");
        printf("Sum of all elements = %d\n", sum2d(ROWS - 1, COLS + 2, morejunk[0]));//修改
    
        printf("3x10 VLA\n");
        printf("Sum of all elements = %d\n", sum2d(rs, cs, varr[0]));//修改
    
        return 0;
    }
    
    int sum2d(int rows, int cols, int* ar)
    {
        int r;
        int c;
        int tot = 0;
    
        for (r = 0; r < rows * cols; r++) //修改
            //for (c = 0; c < cols; c++) 
                tot += ar[r]; //修改
    
        return tot;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 修改了问题 2月28日
  • 创建了问题 2月28日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题