943,963 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4775
  • C RSS
Nov 8th, 2004
0

generate random question

Expand Post »
hi all...i am a not that much in programming ...

i would ask you how i can generate a multiple choice question chosen randomly from a list of questions with answers using functions in C program ...i know how to generate random numbers only ...

help me please ,,, as soon ,,,
Last edited by ams; Nov 8th, 2004 at 6:48 am. Reason: to complete HW
Similar Threads
ams
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ams is offline Offline
5 posts
since Nov 2004
Nov 8th, 2004
0

Re: generate random question

Create an array of questions and use the random number as an index for the array.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2004
0

Re: generate random question

thank u...

but i still don't know how to use array ...i know functions only ....if u can write some of the code ...
ams
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ams is offline Offline
5 posts
since Nov 2004
Nov 8th, 2004
0

Re: generate random question

>but i still don't know how to use array
So use a series of if statements, do you know how to use those? :rolleyes:
  1. int r = rand() % N;
  2.  
  3. if ( r == 0 )
  4. /* First question */
  5. else if ( r == 1 )
  6. /* Second question */
  7. ...
  8. else if ( r == N - 1 )
  9. /* Nth question */
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 9th, 2004
-1

Re: generate random question

i try these code that i want 2 questions appear randomly from 3 quetions ..but still don't work..

#include <stdio.h>
#include <stdlib.h>

int main()
{
int N,counter,i;
int r = rand() % N;

for(i=1;i<=2;i++)
{
if(r==0)
printf("2*2=\na)4 b)2 c)3 d)5\n");
if(getchar()=='a');
counter++;

else if(r==1)
printf("2*5=\na)3 b)10 c)4 d)2\n");
if(getchar()=='b');
counter++;

else if(r==2)
printf("2*9=\na)3 b)10 c)18 d)2\n");
if(getchar()=='c');
counter++;

}
printf("your score is %d\n", counter);

}
ams
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ams is offline Offline
5 posts
since Nov 2004
Nov 9th, 2004
0

Re: generate random question

Call rand every time you want a random number. If you only get one random number then you'll always have the same question. You would think this would be obvious. I guess I just expect too much from other people.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
0

Re: generate random question

I still can't solve it >>>

thank u ...and if u can give more help ...i'd be happy ..i try it ...but i can't generate random questions ....

thank u ...
ams
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ams is offline Offline
5 posts
since Nov 2004
Nov 10th, 2004
0

Re: generate random question

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int N = 3,counter,i;
  7.  
  8. for(i=0;i<10;i++)
  9. {
  10. int r = rand() % N;
  11.  
  12. if(r==0) {
  13. printf("2*2=\na)4 b)2 c)3 d)5\n");
  14. if(getchar()=='a')
  15. counter++;
  16. }
  17. else if(r==1) {
  18. printf("2*5=\na)3 b)10 c)4 d)2\n");
  19. if(getchar()=='b')
  20. counter++;
  21. }
  22. else if(r==2) {
  23. printf("2*9=\na)3 b)10 c)18 d)2\n");
  24. if(getchar()=='c')
  25. counter++;
  26. }
  27. getchar();
  28. }
  29. printf("your score is %d\n", counter);
  30.  
  31. return 0;
  32. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 10th, 2004
0

Re: generate random question

it runs ....

thannnnnnk u very much

and sorry for inconvenience....
ams
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ams is offline Offline
5 posts
since Nov 2004
Nov 10th, 2004
0

Re: generate random question

>and sorry for inconvenience....
No, it's my fault. I didn't look closely enough at the code to see all of your problems. If I had things would have gone much more quickly, and for that I appologize.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: can anyone tell me how we restart the system using C Programming
Next Thread in C Forum Timeline: Slow CPU causes damage to robot





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC