Formatting an Array of Random Numbers

I'm writing a program that generates 50 random integers (in the range of 0 to 100) into an array. The thing I'm having trouble with is formatting the array of 50 random integers into a block with 10 numbers per line in the output console. I only got my program to output the 50 randomly generated numbers. I provided my code and the expected code below. If anyone could give me some hints on formatting the array as shown below, I would appreciate it.

My Code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int array[50];
int r;

int main()
{
	cout << "Array of 50 Random Numbers:";
	cout << endl;

	srand (time(0));

	for (int i = 0; i < 50; i++)
    {
        r = rand() % 100;


		 if (r != r - 1)
			array[i] = r;

		 else
		 {
			r = rand() % 100;
            array[i] = r;
		 }

	}

	for (int i = 0; i < 50; i++)
    {
		cout << array[i] << " ";
	}
	cout << endl;

	return 0;
}


My Output:
1
2
Array of 50 Random Numbers:
95 96 88 17 74 14 4 83 93 18 23 39 73 76 99 76 57 21 10 44 42 23 68 17 31 22 23 56 90 98 79 68 99 75 75 82 4 50 96 95 18 42 28 9 56 99 54 13 81 86


Expected output:
1
2
3
4
5
6
Array of 50 Random Numbers:
7 44 31 58 99 86 5 69 94 69 
45 45 68 45 95 58 11 33 78 66 
22 35 98 67 46 36 59 74 45 91 
61 53 87 92 63 38 31 68 8 25 
89 5 23 57 50 18 68 13 51 98

Last edited on
I'm having trouble with formatting the array of 50 random integers into a block with 10 numbers per line in the output console


When printing the array (line 34), one option is to use 2x for loops:
1
2
3
4
5
6
7
8
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout << array[i * 10 + j] << " ";
		}
		cout << '\n';
	}

5 * 10 == 50.

Another way is to use the % operator.
1
2
3
4
5
6
	for (int i = 0; i < 50; i++)
	{
		cout << array[i] << " ";
		if (i % 10 == 9)
			cout << '\n';
	}

0 % 10 == 0
1 % 10 == 1
...
9 % 10 == 9
10 % 10 == 0
11 % 10 == 1
etc.
% (mod) gives the remainder of the division.
Last edited on
You could either use another loop, or use a remainder to solve your problem. (Ganado explains it quite well)

Also if (r != r - 1) will always be false, and your compiler absolutely should be able to warn you about it. (but I tested it and it didnt for gcc or vc++... maybe I mixed up static analyzers with compiler warnings, or this is just a weird situation).
Last edited on
Thank you Ganado and poteto for your help!

This is my new code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int array[50];
int r;

int main()
{
	cout << "Array of 50 Random Numbers:";
	cout << endl;

	srand (time(0));

	for (int i = 0; i < 50; i++)
    {
        r = rand() % 100;
		cout << r << " ";
		if (i % 10 == 9)
            cout << endl;
    }

	return 0;
}


My new output:
1
2
3
4
5
6
Array of 50 Random Numbers:
86 36 29 47 12 32 43 54 73 91
74 78 54 4 47 4 97 64 76 42
78 82 13 47 57 33 76 72 14 2
66 20 21 30 90 40 99 98 29 30
80 89 47 34 30 34 77 5 58 31

Last edited on
...then outputs the most common integer in the array and how many times it appears
How may I see at first glance the most common integer and how many times it appears in your new output?
Topic archived. No new replies allowed.