DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   generate random question (http://www.daniweb.com/forums/thread13685.html)

ams Nov 8th, 2004 6:33 am
generate random question
 
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 ,,,

Narue Nov 8th, 2004 11:51 am
Re: generate random question
 
Create an array of questions and use the random number as an index for the array.

ams Nov 8th, 2004 3:14 pm
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 ...

Narue Nov 8th, 2004 4:48 pm
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:
int r = rand() % N;

if ( r == 0 )
  /* First question */
else if ( r == 1 )
  /* Second question */
...
else if ( r == N - 1 )
  /* Nth question */

ams Nov 9th, 2004 7:22 am
Re: generate random question
 
i try these code that i want 2 questions appear randomly from 3 quetions ..but still don't work.. :sad: :sad:

#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);

}

Narue Nov 9th, 2004 9:27 am
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.

ams Nov 10th, 2004 6:10 am
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 ...

Narue Nov 10th, 2004 10:13 am
Re: generate random question
 
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int N = 3,counter,i;

  for(i=0;i<10;i++)
  {
    int r = rand() % N;

    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++;
    }
    getchar();
  }
  printf("your score is %d\n", counter);

  return 0;
}

ams Nov 10th, 2004 12:55 pm
Re: generate random question
 
it runs .... :)

thannnnnnk u very much

and sorry for inconvenience....

Narue Nov 10th, 2004 1:05 pm
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.


All times are GMT -4. The time now is 11:35 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC