I am doing a quiz program using an array of structures to hold:
1. The Question Category(Char).
2. The Question of that Category(Char).
3. The correct answer (Integer).
4. A number of wrong answers (Integers).
Structure Questions={"Category","Question",Correct Answer,{Wrong Answers}}
I find that it is very easy to find information about Randomly generating numbers, but not for positions of output.
User starts game:
*There are 5 wrong answers to call upon: 2000, 1890, 3000, 5660, 2002*
General Knowledge Question:
What year is it?
A)2003 (the correct answer)
B)2000
C)1890
The next time the user plays the game and sees the question:
General Knowledge Question:
What year is it?
A)3000
B)2000
C)2003 (the correct answer)
So I want it randomly picking out of the list of wrong answers, whilst always showing the correct answer. Also the possible answers should randomly change position in A, B, C.
I want to randomly pick questions also, so that each category is represented once and so that a question isn't repeated a second time (is only shown once).
So, I want to create random positioning of output(the Structure's elements), not trying to generate a random value.
Do I have to use a random function on a pointer?
Any help if you can?
My code:
#include#includeusing namespace std;
typedef struct {
char *Category;
char *Question;
int Correct;
int WrongAnswers[4];
} Quiz;
void ShowQuestions(Quiz &q,int n);
int main()
{
int i;
char Choice;
Quiz Questions[]={
{"Maths","What is 9*9?",81,{2,4,78,56}},
{"General Knowledge","How many points on a compass?",8,{1,3}}
};
do{
for(i=0;i<3;i++)
{
ShowQuestions(Questions[i],i+1);
}
cout<<"Do you wish to play again? [Y or N]";
cin>>Choice;
if (Choice=='n')
{
return 0;
}
}while(Choice=='y');
return 0;
}
void ShowQuestions(Quiz &q,int n)
{
int i;
cout<<q.Category<<n<<":"<<q.Question<<"
";
cout<<q.Correct<<" "<<"
";
for(i=0;i<5;i++)
{
cout<<q.WrongAnswers[i]<<" ";
}
}
Comments
:
: 1. The Question Category(Char).
: 2. The Question of that Category(Char).
: 3. The correct answer (Integer).
: 4. A number of wrong answers (Integers).
:
: Structure Questions={"Category","Question",Correct Answer,{Wrong Answers}}
:
: I find that it is very easy to find information about Randomly generating numbers, but not for positions of output.
:
: User starts game:
:
: *There are 5 wrong answers to call upon: 2000, 1890, 3000, 5660, 2002*
:
: General Knowledge Question:
: What year is it?
: A)2003 (the correct answer)
: B)2000
: C)1890
:
: The next time the user plays the game and sees the question:
:
: General Knowledge Question:
: What year is it?
: A)3000
: B)2000
: C)2003 (the correct answer)
:
: So I want it randomly picking out of the list of wrong answers, whilst always showing the correct answer. Also the possible answers should randomly change position in A, B, C.
:
: I want to randomly pick questions also, so that each category is represented once and so that a question isn't repeated a second time (is only shown once).
:
: So, I want to create random positioning of output(the Structure's elements), not trying to generate a random value.
:
: Do I have to use a random function on a pointer?
:
: Any help if you can?
:
: My code:
:
: [code]
: #include
: #include
:
: using namespace std;
:
: typedef struct {
: char *Category;
: char *Question;
: int Correct;
: int WrongAnswers[4];
: } Quiz;
:
: void ShowQuestions(Quiz &q,int n);
:
: int main()
: {
:
: int i;
: char Choice;
:
: Quiz Questions[]={
: {"Maths","What is 9*9?",81,{2,4,78,56}},
: {"General Knowledge","How many points on a compass?",8,{1,3}}
: };
:
: do{
:
: for(i=0;i<3;i++)
: {
: ShowQuestions(Questions[i],i+1);
: }
:
: cout<<"Do you wish to play again? [Y or N]";
: cin>>Choice;
:
: if (Choice=='n')
: {
: return 0;
: }
:
: }while(Choice=='y');
:
: return 0;
: }
:
: void ShowQuestions(Quiz &q,int n)
: {
: int i;
:
: cout<<q.Category<<n<<":"<<q.Question<<"
";
: cout<<q.Correct<<" "<<"
";
:
: for(i=0;i<5;i++)
: {
:
: cout<<q.WrongAnswers[i]<<" ";
: }
:
: }
: [/code]
:
:
So, for the questions you want to output every question, but in a random order, and not reapeated? Ok. Here's one way to do this:
Lets say you have the array of questions
[code]Quiz Questions[5][/code]
and it has been properly initialized. You'll also need some kind of "helper" array that tracks which questions have been displayed.
[code]bool AlreadyDisplayed[5][/code]
all initialized to [blue]false[/blue] (because none have been outputted yet). Then, a loop to iterate through the number of questions.
[code]for(int i=0; i<5; ++i)[/code]
Then, the logic to output them (all together: )
[code]Quiz Questions[5][green]/*initialization*/[/green];
bool AlreadyDisplayed[5][green]/*initialization*/[/green];
int rnum = 0;
for(int i=0; i<5; ++i) {
rnum = (rand() % 5);
while(AlreadyDisplayed[rnum]) {
rnum = (rand() % 5);
}
DisplayFunction(Questions[rnum]);
AlreadyDisplayed[rnum] = true;
}[/code]
Basically, in English, this code gets a random number thats a legal array index (0 - 5) then checks to see if that entry in the Questions has been displayed. If so, it gets a new random number until an undisplayed one comes up. When that happens, the question is displayed, and the entry is flagged as already been displayed, to prevent further output.
As for outputting the choices, the logic is basically the same, but you probably won't iterate through every incorrect choice, and you need a way to make sure that the correct answer is displayed. But, I'm sure you can figure that out.
*note - code not compiled, may contain errors*
HTH
-Tony