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;
}