Array display in columns

I need help to "always" display an array right aligned in only 5 columns no matter how big the array is and still have setw(5). I have it randomly generating 100 numbers right now to test. This is a project for class and I have been stuck. Thanks



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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<ctime>
#include"genericSLB.h"

using namespace std;

void	displayArray(const int[], const int)     //Displays content in an array


int main()
{

	const int SIZE = 100;
	int numbers[SIZE];
	int numbers2[SIZE] = {};

	srand(time(NULL));
	for (int i = 0; i < SIZE; i++)
		numbers[i] = rand() % 101;

	cout << "\n\nArray Display\n\n";
	displayArray(numbers, SIZE);
	cout << endl;

	copyArray(numbers2, numbers, SIZE);

	cout << "\n\nArray2 Display\n\n";
	displayArray(numbers2, SIZE);
	cout << endl << endl;




	system("pause");
	return 0;
}



void displayArray(const int length[], const int size)
{

			for (int i = 0; i < size; i++)
				cout << right << setw(5) << length[i];
	
}
Do you mean you want five items per line? Then you need to output a newline '\n' after every five items.
Yes five items per line. So how would I do something like that? Would I add a while loop after the for loop before my cout
You already have a loop which is counting (with i as the loop counter). You could make use of that, by using the % operator to find each multiple of 5. Or you could use a separate counter. But you don't need another loop. Put the code inside your existing loop. Just use an if statement to test when a newline is required.
Thanks that helped alot. I'll try it right now.
Topic archived. No new replies allowed.