hello!
just started out here...
i need help with this program
i need to generate a 3*3 matrix having unique numbers in the range
1-9. I did this program using srand but i'm unable to get the correct output...i couldn't figure out the error...could someone plz help me??
here the program

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j,k,l=0,temp,count=0;
int b[9]={0,0,0,0,0,0,0,0,0};
srand(time(NULL));
for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
   do
    {
     temp=1+rand()%9;
     a[i][j]=temp;
      for(k=0;k<9;k++)
       {
	if(b[k]==temp)
	count++;
       }
    }while(count>0);
      b[l++]=temp;
  }
}
for(i=0;i<3;i++)
{
 for(j=0;j<3;j++)
   printf("%d\t",a[i][j]);
 printf("\n");
 }
 getch();
 }

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

hello!
just started out here...
i need help with this program
i need to generate a 3*3 matrix having unique numbers in the range
1-9. I did this program using srand but i'm unable to get the correct output...i couldn't figure out the error...could someone plz help me??
here the program

Why is it new people think we're psychic and know what the error you're having is? Don't you think knowing the problem would help with a solution?

All I can assume is you aren't checking to see if a number has already been randomly chosen and loading the same number twice.

i used another array to check if a number has already been chosen but it wasn't giving me unique numbers so i guess my code was wrong...but now i have the code figured out..
i used a do-while loop now to keep iterating till another number which isn't already chosen is selected...thanx anyway

#include<stdio.h>
#include<time.h>
int a[3][3],temp,i,j;
int b[10]={0,0,0,0,0,0,0,0,0,0};
srand(time(NULL));
for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
    do
    {
      temp=1+rand()%9;
      a[i][j]=temp;
    }while(b[temp]==1);
    b[temp]=1;
}
}
for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
   printf("%d",a[i][j]);
printf("\n");
}
}

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Another possible solution is to load your b array with the numbers 1-9. Use rand() to get an index into b and copy the value from b into a .

Now shift all the numbers in b down to overwrite the one just used, leaving only 8 numbers left. The next call to rand() you use 8 instead of 9. Keep doing that until you only have 1 left.

this is ur code

#include<iostream.h>
#include<conio.h>
int a[10]={0,0,0,0,0,0,0,0,0};
int com(int n,int count)
{
    int k=0;
    for(int m=0;m<=count;m++)
    {
            if(n==a[m])
            {
                       k=1;
                       break;
                       }
            if(n>9)
            {
                   k=1;
                   break;
                   }
            }
    if(k==0)
    {
            a[count]=n;
            }
    return k;
}

int main()
{
     int maxt[3][3],temp,i,j,count=0;
     //int a[10]={0,0,0,0,0,0,0,0,0};
     for(i=0;i<3;i++)
     {
                     for(j=0;j<3;j++)
                     {
                                     step1:
                                     temp=1+(rand()%10);
                                     int flag=com(temp,count);
                                     if(flag==1)
                                     goto step1;
                                     else
                                     {
                                         maxt[i][j]=temp;
                                         count++;
                                     }
                     }
                     }
     for(i=0;i<3;i++)
     {
                     for(j=0;j<3;j++)
                     {
                                     cout<<maxt[i][j]<<" ";
                                     }
                     cout<<endl;
                     }
     system("PAUSE");
     return 0;
     }

Edited 6 Years Ago by Nick Evan: Added code-tags

This question has already been answered. Start a new discussion instead.