Array

Hi. Im trying to display a 5 X 5 table with random numbers. I'm having trouble because it doesn't display 5x5 table. Please help? Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>
#include<ctime>
using namespace std;

void main ()
{
	int num[5][5];
	int row,col;
	srand(time(NULL));

	for (row=0; row<5; row++)
	{
		for (col=0; col<5; col++)
		{
			num[row][col] = rand()%100;
			cout<<num[row][col]<<"\t";
			cout<<"\n";
		}
	}
}
Line 17 adds a newline. It adds one after each value. Sounds like you would be happy with just one per row?
Try it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<ctime>
using namespace std;

int main()
{
	int num[5][5];
	int row,col;
	srand(static_cast<unsigned int>(0));

	for (row=0; row<5; row++)
	{
		for (col=0; col<5; col++)
		{
			num[row][col] = rand()%100;
			cout<<num[row][col]<<"\t";
		}
		cout<<"\n";
	}
	return 0;
}
Last edited on
Thank you :) Deeply appreciated
Topic archived. No new replies allowed.