0

本の中で彼は、ネストされたループまたは1つのループのいずれかで可能であると述べました

ユーザーは、プログラムが累積合計を 2 番目の配列に設定するために、8 つの double の数値を提供する必要があります。

これはコードです:

#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 の初心者であり、改善方法を知ることが重要です。

4

1 に答える 1

0

あなたの質問がネストされたループまたは1つのループのいずれかを使用することであると仮定します.uが入力を取得するときに、1つのループでこれを行うことができます

for (index = 0; index < ASIZE; index++)//adding the numbers to the first array 
    {
        scanf("%lf", &array1[index]);
        cal += array1[index];
        array2[index] = cal;
    }

実際、同じforループで両方の配列を出力できます。

于 2013-01-25T17:44:43.223 に答える