Random matrix

I am trying to create a random 3x3 matrix and print it out in a text file using basic functions. My code below will not compile and I can not figure out why. Any help is appreciated. Thank you.

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
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//nxn Matrix = 3x3
const int ROWS = 3;
const int COLS = 3;

//Function Declarations
double random();
void fillMatrix(double[][COLS]);
void printMatrix(double[][COLS]);

int main()
{
	double matrix[ROWS][COLS];
	srand(time(NULL)); 
	
	fillMatrix(matrix);
	printMatrix(matrix);
	
	return 0;
	
}
///////////////////////////////////////////////////////////////////////////
//Functions
double random()
{
	double r = rand() % 20 +1;
	return r;
}

///////////////////////////////////////////////////////////////////////////
void fillMatrix(double newMatrix[][COLS])
{
	
	for (int i=0; i<ROWS; i++)
	{
		for (int j=0; j<COLS; i++)
		{
			newMatrix [i][j] = random();
		}
	}
}
///////////////////////////////////////////////////////////////////////////
void printMatrix(double newMatrix[][COLS])
{
	for (int i=0; i<ROWS; i++)
	{
		for (int j=0; j<COLS; i++)
		{
			cout << newMatrix [i][j] << "  ";
		}
	}
}
The only thing I see is that
1
2
3
4
for (int i=0; i<ROWS; i++)
    {
        for (int j=0; j<COLS; i++)
        {

In the second for loop, it should be j++, not i++.

(You have this twice -- once on line 41 and once on line 52.)

Other than that, it compiles and runs fine for me (although the matrix is displayed all on one line instead of in a 3x3 grid).
I forgot to add "cout << endl;" between the two for loops. Thank you for spotting those two mistakes.
Topic archived. No new replies allowed.