RT
最近寫的幾個小程序中幾乎所有的數組元素初始化都有問題,求大神看看是哪裡有問題
//求一個4*4二維數組的對角線上的值的和
#include<stdio.h>
int main (void)
{
int array[3][3],sum = 0;
int i = 0,j = 0;
printf("Please enter the numbers(16 numbers inside):\n");
for (i = 0;i <=3 ;i++){
for(j = 0;j <= 3;j++)
scanf("%d",&array[i][j]);
}
printf("\n");
for (i = 0;i <=3 ;i++){
for(j = 0;j <= 3;j++)
printf("%d ",array[i][j]);
printf("\n");
}
for (i = 0, j = 0;i <= 3;i++,j++)
sum += array[i][j];
printf("sum = %d",sum);
return 0;
}
這是第一個程序及運行結果
//做一個20元素數組,將偶數位下標相加,奇數位下標相乘
#include<stdio.h>
int main (void)
{
double array[19],son = 1,sen = 0;
int i = 0;
printf("Please enter the numbers(20 numbers inside):\n");
for (i = 0;i <= 19;i++)
scanf("%lf",&array[i]);
for (i = 0;i <= 19;i++)
printf("%.0lf ",array[i]);
printf("\n");
for (i = 0;i <= 19;i += 2)
sen += array[i];
for (i = 1;i <= 19;i += 2)
son *= array[i];
printf("The sum of even numbers is %.2lf.\n",sen);
printf("The product of odd numbers is %.2lf.\n",son);
return 0;
}
這是第二個程序及運行結果
不知道為什麼每個數組最後一個元素都有問題
求解
int array[3][3],數組下標從0開始,到2;
for (i = 0;i <=3 ;i++)改為:
for (i = 0;i <3 ;i++)