理解数组和循环答案

时间:2012-12-14 03:27:54

标签: c

你能否完成所有步骤,为什么答案是9,0和3。

这打印9.如何?

int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
        sum=sum+g[i][j];
}
printf("The value is:%d", sum);

打印0.如何?

sum = 1;
for (i=0;i<=2;i++)
{
    for(j=0;j<=1;j++)
        sum=sum*g[i][j];
}
printf("The value is:%d", sum);`

这打印3.如何?

sum = 0;
for (i=0;i<=2;i++)
    sum=sum+g[i][1];
printf("The value is:%d", sum);

2 个答案:

答案 0 :(得分:0)

要了解其工作原理,请尝试以下方法:

int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
     for(j=0;j<=2;j++)
     {
         sum=sum+g[i][j];
         printf("i=%d, j=%d, sum=%d", i, j, sum);
     }
}
printf("The value is:%d", sum);

答案 1 :(得分:0)

试试这个......

int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
     for(j=0;j<=2;j++)
     {
         sum=sum+g[i][j];
         printf("i=%d, j=%d, g[%d][%d]=%d, sum=%d", i, j, i, j, g[i][j], sum);
     }
}
printf("The value is:%d", sum);