Reputation: 1
I have 5 arrays which I am trying to populate using a loop. I want to ask user for 4 entries for the first array (which I do have a loop for), and then loop to the second, third, fourth, and fifth array.
I need these arrays to be separate, one dimensional arrays.
I can get the info for the first array, but since they all have different names, I cannot figure out how to get the loop to progress to the other arrays.
Here's what I have which gets the info for the first array only...I could just repeat everything 5 times using the different array names, but it seems like there should be a way to use a loop.
#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;
}
Upvotes: 0
Views: 980
Reputation: 3645
If sizes of all arrays are equal I suggest you to use 2-dimensional array for this case.
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;
}
Upvotes: 0
Reputation: 726599
You should define a function to loop through the input for a single array, and call that function for each of your four arrays:
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
}
Now you can call these methods from the main()
passing Array1
, Array2
, etc., like this:
handleInput(Array1, 3);
handleInput(Array2, 4);
handleInput(Array3, 4);
handleOutput(Array1, 3);
handleOutput(Array2, 4);
handleOutput(Array3, 4);
Upvotes: 1
Reputation: 1071
Can you not add the following lines after cin >> Array1[countArray];
Array2[countArray] = Array1[countArray];
Array3[countArray] = Array1[countArray];
Array4[countArray] = Array1[countArray];
Array5[countArray] = Array1[countArray];
That sould fill them all with the same values at the same indexes.
Upvotes: 0