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 ,,,

Recommended Answers

Create an array of questions and use the random number as an index for the array.

Jump to Post

>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 …
Jump to Post

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.

Jump to Post

All 9 Replies

Create an array of questions and use the random number as an index for the array.

thank u...

but i still don't know how to use array ...i know functions only ....if u can write some of the code ...

>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 */

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

}
commented: Use code tags. +0

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.

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 ...

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

it runs .... :)

thannnnnnk u very much

and sorry for inconvenience....

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.