3x3 Matrix with non repeating numbers

I want a 3x3 matrix with digits 1 to 9 without any digits being repeated.
So i coded as follows(using Borland 5.5 Compiler) :-

#include <iostream.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>

int mat[9]={1,2,3,4,5,6,7,8,9};

int nmat[3][3];
void main()
{
randomize();
int k,l,ctr=0,ctr1=0;
int i,j;
while(ctr<=7)
{
for(i=ctr;i<3;i++)
{
for(j=ctr1;j<3;j++)
{
nmat[i][j]=mat[k=random(9)];
for(l=k;l<8;l++)
{
mat[l]=mat[l+1];
}
//ctr1++;
}
//ctr++;
}

}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<nmat[i][j]<<" ";
}
cout<<endl;
}
}

Help !!
closed account (zb0S216C)
How about this:

 
int Matrix[3][3] = {{1,2, 3}, {4, 5, 6}, {7, 8, 9}};

Wazzak
1
2
3
4
5
6
7
8
9
10
11
vector<vector<int> > a(3);
int x=1;
for (int i=0; i<3;  i++)
{
     for (int j=0; j<3; j++, x++)
     {
           a[i].push_back(x);
           }
      }
random_shuffle(a.begin(), a.end());
for (int i=0; i<3; i++) random_shuffle(a[i].begin(), a[i].end());

It should work partialy.
Topic archived. No new replies allowed.