Formatting the output of an array

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
Well, you can use std::setw (requires include <iomanip>) to give each column the same width. Then do some checking of the value of the loop counter to identify multiples of 10. At each multiple of 10 (apart from 0) output a newline.

Since you need effectively the same code twice, first for the even then for the odd array, you could make the output into a separate function.
 
    showArray(evenArray, a);

1
2
3
4
5
6
7
8
9
10
void showArray(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << setw(5) << array[i];
        if ( (i+1)%10 == 0 )
            cout << '\n';
    }
    cout << endl;    
}

Thank you, worked like a charm.
worked like a charm

Well, I hope it wasn't seen as some mysterious incantation, the code itself is not that hard - but actually it can still misbehave. Depending on how many items are in the array, it may generate either a single newline or two newlines at the end.

Here's an alternative of more or less the same code which behaves more predictably:
1
2
3
4
5
6
7
8
9
10
void showArray(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        if ( i>0 &&  i%10 == 0 )
            cout << '\n';
        cout << setw(5) << array[i];
    }
    cout << endl;        
}

Topic archived. No new replies allowed.