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.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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 ofCount before you start your loop?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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...
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Of course you can. You'll have to explain why you think you can't.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944