Filling a 2d array using rand() function

I have a 2 dimensional array. I need to use the rand() function to initialize the array with random numbers in between 10 and 75. This is what I have so far. I know this is incorrect, how do I tell it that I want the random numbers to be in between 10 and 75?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
using namespace std;
int table[10][5];
int row = 0;
int col = 0;

int main()
{

	for (row = 0; row < 10; row++)
	{
		for (col = 0; col < 5; col++)
		{
			table[row][col] = rand();
			cout << table[row][col] << endl;
		}
	}
}
The general formula is (rand() % (high - low + 1)) + low for a random number between low and high.
Ok thanks, I did that but when I run my program it is still all one column. It does not display as 10 rows by 5 columns. Any idea why that is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int table[10][5];
int row = 0;
int col = 0;

int main()
{

	for (row = 0; row < 10; row++)
	{
		for (col = 0; col < 5; col++)
		{
			table[row][col] = (rand() % (75 - 10 + 1)) + 10;
			cout << table[row][col] << endl;
		}
	}
}
You are only writing one column. Every time you output a value, you also write a newline.

If you want it tabulated, only output newlines at the end of a row. (In which loop, then, do you think you should output the newline?)

Also, don't forget to write a space or tab or something between columns.

Hope this helps.
Topic archived. No new replies allowed.