Your SlideShare is downloading. ×
Assignment 3
Assignment 3
Assignment 3
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
193
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Solution Assignment 3//A quiz game application that generates random number questions and calculates theresult 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 randomlyint _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 end = time(NULL); elapsed = end - start;
  • 2. if (opp ==+) //calculating the answer and saving the answer temporarily to check whether users 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 user cout<<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 b b = 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 b b = rand () % x; a = (a * e) / (e-1); //used time to make questions vary for everytime the user attempts the quiz b = (b * e) / (e-1); } } a=a; b=b;}
  • 3. charquestgen ()//generates randomly whether a +, a - or a * operator would be usedin the quiz question{ int ran = 0; charopp; ran = rand () % 3; switch (ran) { case 0: opp = -; break; case 1: opp = +; break; default: opp = *; } return opp;}

×