列和行不计算

时间:2016-04-21 04:11:02

标签: c windows

我编写了一个程序来显示二维数组并计算列和行的总数。最初在编译时我得到了错误"未初始化的本地错误"适用于total1total2。然后我将两者的值设置为等于0,然后总数也变为零。我已附上以下代码。任何帮助,将不胜感激。提前谢谢。

int main()
{

#define NUMROWS 5
#define NUMCOLS 5
    int val[NUMROWS][NUMCOLS] = { 8,3,9,0,10,
                                3,5,17,1,1,
                                2,8,6,23,1,
                                15,7,3,2,9,
                                6,14,2,6,0 };

    int total1, total2, sumrow1, sumrow2, sumrow3, sumrow4, sumrow5, sumcol1, sumcol2, sumcol3, sumcol4, sumcol5;
    total1 = 0;
    total2 = 0;

    printf("\n Enter row 1: %2d %2d %2d %2d %2d", val[0][0], val[0][1], val[0][2], val[0][3], val[0][4]);
    printf("\n Enter row 2: %2d %2d %2d %2d %2d", val[1][0], val[1][1], val[1][2], val[1][3], val[1][4]);
    printf("\n Enter row 3: %2d %2d %2d %2d %2d", val[2][0], val[2][1], val[2][2], val[2][3], val[2][4]);
    printf("\n Enter row 4: %2d %2d %2d %2d %2d", val[3][0], val[3][1], val[3][2], val[3][3], val[3][4]);
    printf("\n Enter row 5: %2d %2d %2d %2d %2d", val[4][0], val[4][1], val[4][2], val[4][3], val[4][4]);

    printf("\n Row totals: %2d", total1);

    sumrow1 = val[0][0] + val[0][1] + val[0][2] + val[0][3] + val[0][4];
    sumrow2 = val[1][0] + val[1][1] + val[1][2] + val[1][3] + val[1][4];
    sumrow3 = val[2][0] + val[2][1] + val[2][2] + val[2][3] + val[2][4];
    sumrow4 = val[3][0] + val[3][1] + val[3][2] + val[3][3] + val[3][4];
    sumrow5 = val[4][0] + val[4][1] + val[4][2] + val[4][3] + val[4][4];

    total1 = total1 + sumrow1 + sumrow2 + sumrow3 +sumrow4 + sumrow5;

    printf("\n Colum totals: %2d", total2);

    sumcol1 = val[0][0] + val[0][1] + val[0][2] + val[0][3] + val[0][4];
    sumcol2 = val[1][0] + val[1][1] + val[1][2] + val[1][3] + val[1][4];
    sumcol3 = val[2][0] + val[2][1] + val[2][2] + val[2][3] + val[2][4];
    sumcol4 = val[3][0] + val[3][1] + val[3][2] + val[3][3] + val[3][4];
    sumcol5 = val[4][0] + val[4][1] + val[4][2] + val[4][3] + val[4][4];

    total2 = total2 + sumcol1 + sumcol2 + sumcol3 + sumcol4 + sumcol5;

    return 0;
}

3 个答案:

答案 0 :(得分:1)

这部分代码:

printf("\n Row totals: %2d", total1);

sumrow1 = val[0][0] + val[0][1] + val[0][2] + val[0][3] + val[0][4];
sumrow2 = val[1][0] + val[1][1] + val[1][2] + val[1][3] + val[1][4];
sumrow3 = val[2][0] + val[2][1] + val[2][2] + val[2][3] + val[2][4];
sumrow4 = val[3][0] + val[3][1] + val[3][2] + val[3][3] + val[3][4];
sumrow5 = val[4][0] + val[4][1] + val[4][2] + val[4][3] + val[4][4];

total1 = total1 + sumrow1 + sumrow2 + sumrow3 +sumrow4 + sumrow5;

实际上首先打印total1然后再次计算它。只要您将其初始化为0,它就会打印zero

如果您要打印total1的新值,请在计算中添加printf语句 ,如下所示:

sumrow1 = val[0][0] + val[0][1] + val[0][2] + val[0][3] + val[0][4];
sumrow2 = val[1][0] + val[1][1] + val[1][2] + val[1][3] + val[1][4];
sumrow3 = val[2][0] + val[2][1] + val[2][2] + val[2][3] + val[2][4];
sumrow4 = val[3][0] + val[3][1] + val[3][2] + val[3][3] + val[3][4];
sumrow5 = val[4][0] + val[4][1] + val[4][2] + val[4][3] + val[4][4];

total1 = total1 + sumrow1 + sumrow2 + sumrow3 +sumrow4 + sumrow5;
printf("\n Row totals: %2d", total1);

同样适用于total2

答案 1 :(得分:0)

可能这可能有用

#include<stdio.h>
#define NUMROWS 5
#define NUMCOLS 5

int main()
{

    int i,j,a=0;
    int total1=0,total2=0;

    int val[NUMROWS][NUMCOLS] = { 8,3,9,0,10,
                            3,5,17,1,1,
                            2,8,6,23,1,
                            15,7,3,2,9,
                            6,14,2,6,0 };

    printf("Given Matrix:\n");
    for(i=0;i<NUMROWS;i++)
    {
        printf("\n");
        for(j=0;j<NUMCOLS;j++)
            printf("%3d",val[i][j]);
    }

    printf("\n\nSum of Coloums:\n");

    for(i=0;i<NUMROWS;i++)
    {
        printf("\n");
        for(j=0;j<NUMCOLS;j++)
            a=a+ val[i][j];

        total1=total1 + a;
        printf("sumrow%d=%3d",i+1,a);
        a=0;
    }

    printf("\n\nSum of Rows:\n");

    for(i=0;i<NUMROWS;i++)
    {
        printf("\n");
        for(j=0;j<NUMCOLS;j++)   
             a=a+ val[j][i];

        total2=total2 +a;
        printf("sumcol%d=%3d",i+1,a);
        a=0;
    }

    printf("\n\ntotal1:%d",total1);
    printf("\n\ntotal2:%d",total2);
    getchar();
    return 0;

}

答案 2 :(得分:0)

问题 - 如前所述 - 是您要访问尚未初始化的变量的值。为了解决这个问题,请初始化变量。在您的情况下,您在计算值之前进行了打印。为了在编译时看到这些错误,请告诉编译器您要查看每个警告。对于gcc,您可以通过添加not null参数来完成此操作。如果您要编译-Wall,您应该执行main.c

之类的操作

除此之外,我强烈建议将for循环与数组结合使用。这减少了您必须编写的代码量并使代码更加通用。假设您想使用6x6阵列而不是5x5阵列。然后你必须修改到目前为止你写的整个代码。当使用for循环时,情况并非如此。

我修改了你的程序,它打印出2D数组的值,计算每行和col的总和,并打印出来。这已经使用for循环实现。我添加了尽可能多的评论,我认为是必要的;如果你不理解某一部分,只要问:)

gcc -Wall main.c

生成的控制台输出

#include<stdio.h> // include this to use printf

#define NUMROWS 5
#define NUMCOLS 5


int main(void)
{
    int val[NUMROWS][NUMCOLS] = { // initialize a 2D-array this way {{row1}, {row2}, ..}
        {8,3,9,0,10},
        {3,5,17,1,1},
        {2,8,6,23,1},
        {15,7,3,2,9},
        {6,14,2,6,0}
    };

    int sumRow[NUMROWS] = { 0 }; // initialize with 0   
    int sumCol[NUMCOLS] = { 0 }; // initialize with 0

    int i; // i = row
    int j; // j = col
    for(i = 0; i < NUMROWS; i++) { // iterate through the array and print out the values and generate the sumRow and sumCol
        printf("Enter row %d:", i+1);
        for(j = 0; j < NUMCOLS; j++) { // loop through a row 
            sumRow[i] += val[i][j]; // update the sumRow value for this row
            sumCol[j] += val[i][j]; // update the sumCol value for this col
            printf(" %2d", val[i][j]); // print out the current value of row i and col j
        }
        printf(" => %2d\n", sumRow[i]); // print the sumRow for the current row
    }
    printf("            ");
    for(j = 0; j < NUMCOLS; j++) { // loop through the col
        printf(" %2d", sumCol[j]); // print the sumCol for the current col
    }

    return (0);
}