I need to write a quiz program that generates random questions.
My group and I determined that we would probably need to make use of the random function, so we did our research and were able to write the following program

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
int j[10];

for(i=0;i<10;i++)
{
j[i]=rand()%10;
printf("%d \n",j[i]);
}
}

However, 2 problems were encountered.
We were unable to come up with a way to actually link that random number with a question so as to generate random questions
& we noticed that each time the program was run, it generated the same "random" numbers.
Is there some parameter we'd missed ?

I need to write a quiz program that generates random questions.
My group and I determined that we would probably need to make use of the random function, so we did our research and were able to write the following program

#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
int j[10];

for(i=0;i<10;i++)
{
j[i]=rand()%10;
printf("%d \n",j[i]);
}
}

However, 2 problems were encountered.
We were unable to come up with a way to actually link that random number with a question so as to generate random questions

Make your questions an array of questions. Then the random number can be the index of the question in question (sorry about that :))

& we noticed that each time the program was run, it generated the same "random" numbers.
Is there some parameter we'd missed ?

Yes, you missed the srand() function.


Also, learn now to format your code. You will need this as your programs get more complicated.

Thank you for your quick reply, WaltP . I was doing a bit of research on arrays but im not sure how you mean for me to incorporate this into the program..

Yes, you missed the srand() function.

Is it just to change rand() to srand ?

Also, learn now to format your code. You will need this as your programs get more complicated.

Thanks for this.

Thank you for your quick reply, WaltP . I was doing a bit of research on arrays but im not sure how you mean for me to incorporate this into the program..

char *question[] = {"Question #1",
                    "Query #2",
                    "Pregunta #3",
                      ...
                   };

Is it just to change rand() to srand ?

No. Look it up.

I did look it up, however, I still do not understand how it works exactly.
The only site that actually helped me, http://www.randombots.com/SRAND.HTM , still didn't get across to me how it actually works so I can manipulate it in my program.
Is it possible for me to use rand() as a variable directly ?
Such as,
switch (rand())

switch (rand())
      case 1:
      {
            Question 1
      }
      case 2:
            Question 2
      }
      {

And so on ?

The page you posted not only explains it, but gives you an example. Type the example in. Run it a few times. Change the srand() parameter to something else. Run it a few time.

In other words, if you don't know how something works, find an example (you did) and play with it until you understand it.

So I've played around with this code a few times.

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

int main ()
{
    time_t (t);
    time (&t);
    srand (t);
    int Highest, Lowest, Count=0;

    system("color fc");
    printf("\nRand Max: %d\n",RAND_MAX);
    printf("\nHighest Number: ");
    scanf("%d",&Highest);
    printf("\nLowest Number: ");
    scanf("%d",&Lowest);
    while(Count < 10)
    {
        printf("\t%d", rand() % Highest + Lowest);
     Count++;
    }

    return 0;
}

I now see how to successfully manipulate this code.
It doesn't seem to always work, but I suppose its fine. If it was possible to make it as foolhardy as it can be , that would be great.
I entered the Highest as 20 and the lowest as 1.
Also, I am sort of familiar with Arrays, but how is it that you label arrays so that it calls one with a questino ? I was searching but I can't find any web pages. Could you explain or provide me with one ?

So I've played around with this code a few times.


I now see how to successfully manipulate this code.
It doesn't seem to always work, but I suppose its fine.

Oh no, another "mediocre is good enough" programmer? Please change that attitude now. If a program doesn't work always, it's a bad program.

Would you like it if your bank used software that works most of the time?

I entered the Highest as 20 and the lowest as 1.

What's the purpose of entering the Highest and Lowest?

Also, I am sort of familiar with Arrays, but how is it that you label arrays so that it calls one with a questino ? I was searching but I can't find any web pages. Could you explain or provide me with one ?

Look again. Find an explanation about "C string arrays". I found a couple while barely looking.

By the way, what is the value of Count before you start your loop?

Oh no, another "mediocre is good enough" programmer? Please change that attitude now. If a program doesn't work always, it's a bad program.

Would you like it if your bank used software that works most of the time?

I suppose not. However, I did a bit more fooling around and I noticed that it does actually work properly. It doesn't work if Count is set to a smaller number than Highest... In that, it repeats numbers.

What's the purpose of entering the Highest and Lowest?

The way my question is actually set up, is that I have 3 topic I should present questions on. One question bank may have more questions than another, therefore by using Highest and Lowest I can increase the range of questions to choose from for each section.

Look again. Find an explanation about "C string arrays". I found a couple while barely looking.

On this forum ? ...

By the way, what is the value of Count before you start your loop?

10 . So 10 questions total for each section. As I said, Highest & Lowest variables increase the range of possible 10 questions to choose from.

What's the purpose of entering the Highest and Lowest?

The way my question is actually set up, is that I have 3 topic I should present questions on. One question bank may have more questions than another, therefore by using Highest and Lowest I can increase the range of questions to choose from for each section.

If you say so. It doesn't make sense to me, but as long as you understand it...


Look again. Find an explanation about "C string arrays". I found a couple while barely looking.

On this forum ? ...

Maybe, but not necessarily.

By the way, what is the value of Count before you start your loop?

10 . So 10 questions total for each section. As I said, Highest & Lowest variables increase the range of possible 10 questions to choose from.

Not in the code you posted it isn't.

Not in the code you posted it isn't

Sorry, the value of Count BEFORE I start my loop is 0 .
It increments to 10.
I initialized it as 0 .. while declaration

Sorry, the value of Count BEFORE I start my loop is 0 .
It increments to 10.
I initialized it as 0 .. while declaration

Ahhh, yes. So you did. Sorry...

Its ok . So I researched arrays, however I don't think it fits it well because I can't utilize the IF statement to check if the input answer to the question array is correct ...

Of course you can. You'll have to explain why you think you can't.

I was of the impression I couldn't successfully link an array in a way as to print a question, and then check if the answer provided to the question was correct... But my group members just proved me wrong . One wrote this code but we have stumbled onto another roadblock. The questions have a slight tendency to either:
Repeat
Print 1 less question
or Print 1 extra question

This is the *problematic* code

Ques = rand() % 11;
    Usednum[1] = Ques;
    A(Ques);

    for (x = 2; x <= 5; x++)
    {
        for (y = 1; y < x; y++)
        {
            Ques = rand() % 11;
            Usednum[x] = Ques;
            if (Usednum[y] == Usednum[x])
            {
                y = 0;
            }
        }

            A(Ques);
    }
}

And this is the code that is *sorta* responsible for printing the wrong amount of questions.

char Answer,Correct;
    int Score;

        if (Ques == 1)
        {
        Correct = 'a';
        printf("\nQuestion1\n");
        scanf(" %c",&Answer);
        if (Answer == Correct)
            {
                Score ++;
                printf ("\nThat is correct!\n");
            }
        else
                printf("\nThat is not correct sorry.\n");


            }
Be a part of the DaniWeb community

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