Im writing a program that is supposed to cout numbers in an array. However, the numbers cout on a single continuous line that I think looks really ugly, and was wondering if there is a way to format the output of the array? Ideally with 10 numbers per line.
Everything I do seems to either stop the array before its finished, have strings of 0s, or numbers that I have no idea where they came from.
Below is the function that my program is using.I figured it would be best to include the whole function for context.
It is supposed to gather data from a file and put it into an array, then take that array and divide the numbers into two separate arrays (one for even #'s, one for odd #'s). The function works, but the trouble Im having is at the lower half with the couts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
void oddOrEven(ifstream & infile, int evenArray[], int oddArray[], int array[])
{
int a = 0;
int b = 0;
for (int i = 0; infile >> array[i]; i++)
{
if (array[i] % 2 == 0)
{
evenArray[a] = array[i];
a++;
}
else
{
oddArray[b] = array[i];
b++;
}
}
cout << "Even Array" << endl;
for (int i = 0; i < a; i++)
{
cout << evenArray[i] << " ";
}
cout << endl;
cout << "Odd Array" << endl;
for (int i = 0; i < b; i++)
{
cout << oddArray[i] << " ";
}
}
|
With the code the way it is I get this:
Even Array
46 30 82 90 56 16 48 26 4 58 0 78 92 60 12 90 14 52 16 80 102 34 10 92 88 66 64 92 66 64
Odd Array
17 95 21 63 47 19 41 85 -9 71 79 51 95 79 95 61 89 63 39 5
When what I would like is more like this:
Even Array
46 30 82 90 56 16 48 26 4 58
0 78 92 60 12 90 14 52 16 80
102 34 10 92 88 66 64 92 66 64
Odd Array
17 95 21 63 47 19 41 85 -9 71
79 51 95 79 95 61 89 63 39 5