i am making a program in C where i ask the user 10 questions and i have a list of 20 questions. I want the machine to randomly pick and ask any 10 questions. Can anyone help me how to do that?
Asked
Active
Viewed 59 times
-7
-
4Post your efforts so far – P.W Dec 21 '18 at 09:44
-
3Shuffle the array randomly, then use the first 10 questions in the array. – Barmar Dec 21 '18 at 09:45
-
2StackOverflow is *not* a do-my-homework site. You may want to use some [PRNG](https://en.wikipedia.org/wiki/Pseudorandom_number_generator). Look into some [C reference](https://en.cppreference.com/w/c) site. Read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). For your next question here, give some [MCVE] – Basile Starynkevitch Dec 21 '18 at 09:45
-
1Generate random numbers. Use `% 20` to index into the array. Have a look at: https://stackoverflow.com/questions/822323/how-to-generate-a-random-int-in-c – babon Dec 21 '18 at 09:46
-
Possible duplicate of [Randomizing questions on a quiz and calculating percentage correct in C](https://stackoverflow.com/questions/29527960/randomizing-questions-on-a-quiz-and-calculating-percentage-correct-in-c) – kiner_shah Dec 21 '18 at 11:04
1 Answers
1
As an outline:
struct Q {
char *q;
int hasbeenselected;
} q[20];
int n;
//
// fill q with your questions and set hasbeenselected to 0;
srand(time(NULL));
for (int i=0; i<10; i++) {
do {
n= rand()%20;
} while (q[n].hasbeenselected==1);
// now ask question q[n].q
q[n].hasbeenselected= 1;
}

Paul Ogilvie
- 25,048
- 4
- 23
- 41