Generate a question set containing 6 questions at each set using a C program with random numbers 1-16
where the condition is that two adjacent question set does not contain any same question.
For Example:---
Set[1]--> 12 03 11 10 08 04
Set[2]--> 01 14 02 07 05 09
Set[3]--> 16 04 13 08 03 11
Set[4]--> 14 10 01 07 09 12
Set[5]--> 05 13 03 06 16 15
Set[6]--> 14 12 08 01 07 02
I have done it by using 2d matrix. Done in Code Blocks. Compiled in gcc Compiler. You can comment your doubts and you also can suggest to solve it in other way.
CODE:
For Example:---
Set[1]--> 12 03 11 10 08 04
Set[2]--> 01 14 02 07 05 09
Set[3]--> 16 04 13 08 03 11
Set[4]--> 14 10 01 07 09 12
Set[5]--> 05 13 03 06 16 15
Set[6]--> 14 12 08 01 07 02
I have done it by using 2d matrix. Done in Code Blocks. Compiled in gcc Compiler. You can comment your doubts and you also can suggest to solve it in other way.
CODE:
#include<stdio.h>
void main()
{
int n,arr[16],temp[6][6];
int i,j,k,p,l,index;
for(i=0;i<16;i++)
{
arr[i]=i+1;
printf(" %d ",arr[i]);
}
printf("\n");
n=16;
srand(time(NULL));
for(k=0;k<6;k++)
{for(i=0;i<6;i++)
{
index=rand()%n;
temp[k][i]=arr[index];
for(j=index;j<n;j++)
{
arr[j]=arr[j+1];
}
n=n-1;
if(i==5 && k>0)
{
for(j=0;j<6;j++)
{
arr[j+4]=temp[k-1][j];
}
n=10;
}
}
}
for(l=0;l<6;l++)
{ printf("\nSet[%d]-->",l+1);
for(p=0;p<6;p++)
printf(" %.2d ",temp[l][p]);
}
}
No comments:
Post a Comment