问题描述
- 关于数组元素初始化有一些小问题
-
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++)
解决方案二:
1.定义了一个3*3的数组,你输入个4*4的?想干嘛?
2.还没看
解决方案三:
int array[3][3],数组下标从0开始,到2;
for (i = 0;i <=3 ;i++)改为:
for (i = 0;i <3 ;i++)
时间: 2024-11-03 04:59:16