Matrix printing problem lml

I Want to print n values in each line in between brackets. The first loop works except for the brackets. The second one does what i want but it doesnt print in order. I am so close to figure it out but i think i need a lil help here. Wait am i even printing it in order? lol.

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

void print(int* a, int n);
int main()
{
	int n;
	cout << "Enter n for nxn Matrix: ";
	cin >> n;
	
	int a[n*n];
	srand(time(NULL));
	for(int i=0;i<n*n;i++)
	{
        a[i]=rand()%9;
    }

    cout<<"["; //First loop
    for(int i=0;i<n*n;i++){
        cout<<a[i]<<" ";
        if(i%n==n-1) cout<<"]"<<endl<<"[";
    }
    cout << "\n\n";

	print(a, n); //Second Print(order messed up)
	
	return 0;
}
void print(int* a, int n)
{
	int j, i;
    for(i=0;i<n;i++)
	{
		cout<<"[";
        for(j=0;j<n-1;j++)
			cout<<a[j*i]<<" , ";
		cout<<a[j*i]<<"]\n";
        if(i%n==n-1) cout<<endl;
    }
}
Topic archived. No new replies allowed.