0

ループを使用してデータを入力しようとしている5つのアレイがあります。最初の配列(ループがあります)の4つのエントリをユーザーに要求してから、2番目、3番目、4番目、5番目の配列にループします。

これらの配列は、個別の1次元配列である必要があります。

最初の配列の情報を取得できますが、名前がすべて異なるため、ループを他の配列に進める方法がわかりません。

これが私が持っているもので、最初の配列の情報のみを取得します...異なる配列名を使用してすべてを5回繰り返すことができますが、ループを使用する方法があるはずです。

 #include <iostream>
 #include <iomanip>
 #include <string>

 using namespace std;


 int main()
 {
 int Array1[4];
 int Array2[4];
 int Array3[4];
 int Array4[4];
 int Array5[4];
 int countArray = 1;

 cout <<" \nEnter 4 integers: \n\n";

 for (countArray; countArray <=4; countArray ++)
     cin >> Array1[countArray];


 // Need to get info in to Array1, followed by Array2, Array3, Array4, Array 5
 // Want ot use a loop to call the other arrays

 countArray = 1;
 cout << "\n\n";
 for (countArray = 1; countArray <=4; countArray ++)
     cout << Array1[countArray] << "  ";;
     cout << "\n\n";

 // Need to outpit info from Array1, followed by Array2, Array3, Array4, Array 5
 // Want ot use a loop to call the other arrays


system("PAUSE");

return 0;
   }
4

3 に答える 3

1

単一の配列の入力をループする関数を定義し、4 つの配列のそれぞれに対してその関数を呼び出す必要があります。

void handleInput(int array[], int count) {
    // Input the data into the array, up to the count index
}
void handleOutput(int array[], int count) {
    // Output the data from the array, up to the count index
}

これで、次のように、、 などからこれらのmain()メソッドを呼び出すことができます。Array1Array2

handleInput(Array1, 3);
handleInput(Array2, 4);
handleInput(Array3, 4);
handleOutput(Array1, 3);
handleOutput(Array2, 4);
handleOutput(Array3, 4);
于 2013-02-04T00:55:50.000 に答える
0

すべての配列のサイズが等しい場合、この場合は 2 次元配列を使用することをお勧めします。

int main()
{
    int Array[5][4];
    int countArray = 0;
    int countItem = 0;

    for (countArray = 0; countArray < 5; ++countArray) 
    {
        cout <<" \nEnter 4 integers: \n\n";
        for (countItem = 0; countItem < 4; ++countItem)
            cin >> Array[countArray][countItem];
    }

    // Need to get info in to Array1, followed by Array2, Array3, Array4, Array 5
    // Want ot use a loop to call the other arrays

    cout << "\n\n";
    for (countArray = 0; countArray < 5; ++countArray)
    {
        for (countItem = 0; countItem < 4; ++countItem)
            cout << Array[countArray][countItem] << "  ";
        cout << "\n\n";
    }

    // Need to outpit info from Array1, followed by Array2, Array3, Array4, Array 5
    // Want ot use a loop to call the other arrays

    return 0;
}
于 2013-02-04T05:28:41.107 に答える
0

cin >> Array1[countArray]; の後に次の行を追加できませんか。

Array2[countArray] = Array1[countArray];
Array3[countArray] = Array1[countArray];
Array4[countArray] = Array1[countArray];
Array5[countArray] = Array1[countArray];

それは、同じインデックスで同じ値でそれらをすべて埋めます。

于 2013-02-04T00:54:41.217 に答える