mardi 27 janvier 2015

Need to generate random number in 2-D array

I am making a program where i have to generate 2-D array of N*N with random number without repetition such that one element is an alphabet .


Also if the matrix size is 3 then element must be between 1 and 8 without repetition and a single alphabet .


similarly for matrix of size 4 , element must be between 1 and 15 without repetition and a single alphabet .


Can anyone help ?


[code]



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,j,b,c,n ;

char ar[100][100];
char temp,ch;
printf("enter the size of the matrix : ");
scanf("%d",&n);


for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{

scanf("%d",&ar[i][j]);
printf("\t");


}
}

ar[n-1][n-1] = 'E';

// for printing the array
for(i=0;i<n;i++)

{
printf("\n");
for(j=0;j<n;j++)

{



if(i==(n-1)&&j==(n-1))
{
printf("%c",ar[n-1][n-1]);
}
else
printf("%d",ar[i][j]);

printf("\t");



}
}

printf("\n Press 'w' for shifting up 's' for down 'a' for left and 'd' for right : \n");

while(1) //infinite loop
{

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

if(ar[i][j]=='E')
{
b = i; //storing row
c = j; // storing column
printf("\t");
ch = getch();

switch(ch)

{



case 'w':




if((i-1)>=0)
{
system("CLS");
temp = ar[i-1][j];
ar[i-1][j] = ar[i][j];
ar[i][j] = temp;

for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
if(i==(b-1)&&j==c)
{
printf("%c",ar[n-1][n-1]);
}
else
printf("%d",ar[i][j]);

printf("\t");

}
}
printf("\t");
}

else
{
printf("wrong move bro");
}



break;



case 'a':




if((j-1)>=0)
{
system("CLS");
temp = ar[i][j-1];
ar[i][j-1] = ar[i][j];
ar[i][j] = temp;

for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{

if(i==b&&j==(c-1))
{
printf("%c",ar[n-1][n-1]);
}
else
printf("%d",ar[i][j]);

printf("\t");


}
}
printf("\t");
}

else
{


printf("wrong move bro");
}



break;


case 's' :

if((i+1)<n)
{
system("CLS");
temp = ar[i+1][j];
ar[i+1][j] = ar[i][j];
ar[i][j] = temp;

for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{

if(i==(b+1)&&j==c)
{
printf("%c",ar[n-1][n-1]);
}
else
printf("%d",ar[i][j]);

printf("\t");


}
}
printf("\t");
}

else
{


printf("wrong move bro");
}
break;

case 'd' :

if((j+1)<n)
{
system("CLS");
temp = ar[i][j+1];
ar[i][j+1] = ar[i][j];
ar[i][j] = temp;

for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{

if(i==b&&j==(c+1))
{
printf("%c",ar[n-1][n-1]);
}
else
printf("%d",ar[i][j]);

printf("\t");


}
}
printf("\t");
}

else
{


printf("\n wrong move bro");

}
break;










} //switch end





} //if end



























} //for end


} //for end


} //while end


} // main end


[/code]





Aucun commentaire:

Enregistrer un commentaire