Using C, How do I randomly display values within an array considering that each of these values has a unique corresponding value without repeating any of these values displayed? Using C, my goal is to randomly display questions, one at a time, for a set amount of iterations. I created an array that holds the questions and their four possible answers. I also created an array that holds the correct answer for each question.
Thank you very much... You guys have been really helpful
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int random();
int l, qs[10];
int main ()
{
for (l=0;l<10;++l)
qs[l]=0;
srand(time(NULL));
char questions [] [50] ={"aa \n\na)\n\nb)\n\nc)\n\nd)",\"bb \n\na)\n\nb)\n\nc)\n\nd)", "cc \n\na)\n\nb)\n\nc)\n\nd)", \
"dd \n\na)\n\nb)\n\nc)\n\nd)", "ee \n\na)\n\nb)\n\nc)\n\nd)", \
"ff \n\na)\n\nb)\n\nc)\n\nd)", "gg \n\na)\n\nb)\n\nc)\n\nd)", \
"hh \n\na)\n\nb)\n\nc)\n\nd)", "ii \n\na)\n\nb)\n\nc)\n\nd)", \
"jj \n\na)\n\nb)\n\nc)\n\nd)"};
char answers [10] = {'a','b', 'b','d','c','b','d','b' ,'c', 'b'};
int i, j;
char ans;
int score = 0;
printf ("Read each question carefully and choose your best answer.");
for (i=1;i<6;i++)
{
j = random();
fflush(stdin);
clrscr();
printf ("\n %d %s \n\n", i, questions [j]);
printf ("\n Enter Answer: ");
do
{
ans = tolower(getchar());
}while ( (ans < 'a') || (ans > 'd'));
printf ("\nYou chose: %c ", ans);
if (ans == answers[j])
{score = score + 1;
printf ("\n\nCorrect. %d Mark/s", score);
}else{printf ("\n\nIncorrect. 0 Mark/s");}
printf("\n\nPress Enter for next question...");
getch ();
}
getch ();
return 0;
}
random ()
{
int k;
do
{k=rand()%10;
}while (qs[k]!=0);
qs[k]= 1;
return k;
}