Assignment 3

3
Solution Assignment 3 //A quiz game application that generates random number questions and calculates the result and displays the total score. #include"stdafx.h" #include<iostream> #include<conio.h> #include<cstdlib> #include<cstdlib> #include<ctime> usingnamespacestd; void numgen (int, long, int&, int&);// function that generates 2 random numbers char questgen ();//function to generate '+', '-' and '*' operators randomly int _tmain(intargc, _TCHAR* argv[]) { time_t start = 0; time_t end = 0; time_t elapsed = 0; start = time(NULL); intdif, ques,res=0,a=0,b=0;// initializing vairables charopp; intx,ans,temp; cout<<"**************************WELCOME TO THE QUIZ GAME**************************"<<endl; cout<<endl; cout<<"How many questions would you like to attempt?? "<<endl; cin>>ques; //number of questions user would like to attempt cout<<"Choose a difficulty level?? [1,2 or 3] "<<endl; cin>>dif; //difficulty level of questions that user wud like to solve if (dif == 1) x = 6; elseif ( dif == 2) x = 70; elseif (dif == 3) x = 970; for (int j = 0; j <ques; j++) { end = time(NULL); elapsed = end - start; numgen(x,elapsed,a,b); //calling function numgen opp = questgen (); //calling fucntion quest gen cout<<a<<opp<<b<<endl; //displaying question to user cout<<"Answer is: "; cin>>ans;//taking input answer from user

Transcript of Assignment 3

Page 1: Assignment 3

SolutionAssignment 3

//A quiz game application that generates random number questions and calculates the result and displays the total score.

#include"stdafx.h"#include<iostream>#include<conio.h>#include<cstdlib>#include<cstdlib>#include<ctime>

usingnamespacestd;void numgen (int, long, int&, int&);// function that generates 2 random numberschar questgen ();//function to generate '+', '-' and '*' operators randomly

int _tmain(intargc, _TCHAR* argv[]){

time_t start = 0;time_t end = 0;time_t elapsed = 0;start = time(NULL); intdif, ques,res=0,a=0,b=0;// initializing vairablescharopp;intx,ans,temp;cout<<"**************************WELCOME TO THE QUIZ GAME**************************"<<endl;cout<<endl;cout<<"How many questions would you like to attempt?? "<<endl;cin>>ques; //number of questions user would like to attemptcout<<"Choose a difficulty level?? [1,2 or 3] "<<endl;cin>>dif; //difficulty level of questions that user wud like to solve

if (dif == 1)x = 6;elseif ( dif == 2)x = 70;elseif (dif == 3)x = 970;for (int j = 0; j <ques; j++){

end = time(NULL);elapsed = end - start;numgen(x,elapsed,a,b); //calling function numgenopp = questgen (); //calling fucntion quest gencout<<a<<opp<<b<<endl; //displaying question to usercout<<"Answer is: ";cin>>ans;//taking input answer from userend = time(NULL);elapsed = end - start;

Page 2: Assignment 3

if (opp =='+') //calculating the answer and saving the answer temporarily to check whether user's answer is correct or not{

temp = a + b;if (temp == ans)res = res + 1;

}

if (opp =='-'){

temp = a - b;if (temp == ans)res = res + 1;

}

if (opp =='*'){

temp = a * b;if (temp == ans)res = res + 1;

}}cout<<"Your Result Is: "<<res<<" Out Of "<<ques<<endl; //displaying

result to usercout<<endl;cout<<"*********************THANKYOU FOR

PLAYING**********************"<<endl;getch();return 0;

}

boid numgen (int x, long e, int& a, int& b){

a = rand () % x; //alloting random values to variable a and bb = rand () % x;a = a + e;b = b + e;if((a>x)&&(b>x)){

while((a>x)&&(b>x)){

a = rand () % x; //alloting random values to variable a and bb = rand () % x;a = (a * e) / (e-1); //used time to make questions vary for everytime the user attempts the quizb = (b * e) / (e-1);

}}a=a;b=b;

}

Page 3: Assignment 3

charquestgen ()//generates randomly whether a '+', a '-' or a '*' operator would be used in the quiz question{

int ran = 0;charopp;ran = rand () % 3;switch (ran){

case 0:opp = '-';break;case 1:opp = '+';break;default:opp = '*';

}return opp;

}