在书中他说可以使用嵌套循环或 1 循环
用户应为程序提供 8 个双精度数,以在第二个数组中设置累计总数。
这是代码:
#include <stdio.h>
#define ASIZE 8
int main()
{
int index = 0, x, index2;
double cal;
double array1[ASIZE], array2[ASIZE];
printf("Please enter 8 numbers:\n");
for (index = 0; index < ASIZE; index++)//adding the numbers to the first array
{
scanf("%lf", &array1[index]);
}
for (x = 0,index2 = 0,index = 0; x < ASIZE; x++, index2++)//adding the second array the elements
{
cal += array1[index++];
array2[index2] = cal;
}
printf("the first array numbers are:\n");//printing the first array numbers
for (index = 0; index < ASIZE; index++)
{
printf("%.1lf ", array1[index]);
}
printf("\n");
printf("\n");
printf("the second array numbers are:\n");//printing the second array
for (index2 = 0; index2 < ASIZE; index2++)
{
printf("%.1lf ", array2[index2]);
}
}
我是 C 的初学者,知道如何变得更好对我来说很重要。