SlideShare ist ein Scribd-Unternehmen logo
1 von 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
             end = time(NULL);
             elapsed = end - start;
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 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;
}
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;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in GolangOliver N
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?勇浩 赖
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th StudyChris Ohk
 
Concurrency in go
Concurrency in goConcurrency in go
Concurrency in goborderj
 
Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADDharmalingam Ganesan
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Yandex
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrencyjgrahamc
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Schedulermatthewrdale
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency명신 김
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practiceGuilherme Garnier
 
NativeBoost
NativeBoostNativeBoost
NativeBoostESUG
 
Oops pramming with examples
Oops pramming with examplesOops pramming with examples
Oops pramming with examplesSyed Khaleel
 

Was ist angesagt? (20)

Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 
Concurrency in go
Concurrency in goConcurrency in go
Concurrency in go
 
GoでKVSを書けるのか
GoでKVSを書けるのかGoでKVSを書けるのか
GoでKVSを書けるのか
 
user2015 keynote talk
user2015 keynote talkuser2015 keynote talk
user2015 keynote talk
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOAD
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
Golang Channels
Golang ChannelsGolang Channels
Golang Channels
 
What is recursion?
What is recursion? What is recursion?
What is recursion?
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
Mnistauto 5
Mnistauto 5Mnistauto 5
Mnistauto 5
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Scheduler
 
About Go
About GoAbout Go
About Go
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practice
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
Oops pramming with examples
Oops pramming with examplesOops pramming with examples
Oops pramming with examples
 

Ähnlich wie Assignment 3

java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docxjava compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docxpriestmanmable
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docxkatherncarlyle
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثةجامعة القدس المفتوحة
 
I wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdfI wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdfrishteygallery
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScriptniklal
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docxMARRY7
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoThe Software House
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 

Ähnlich wie Assignment 3 (20)

java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docxjava compilerCompiler1.javajava compilerCompiler1.javaimport.docx
java compilerCompiler1.javajava compilerCompiler1.javaimport.docx
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
verilog code
verilog codeverilog code
verilog code
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Javascript
JavascriptJavascript
Javascript
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
#define ENABLE_COMMANDER#define ENABLE_REPORTER#include c.docx
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
I wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdfI wrote the following change it to having a header, main and cpp fi.pdf
I wrote the following change it to having a header, main and cpp fi.pdf
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
Tork03 LT
Tork03 LT Tork03 LT
Tork03 LT
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Lab Question
Lab QuestionLab Question
Lab Question
 

Mehr von Ayesha Bhatti (20)

Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Chap 5 project management
Chap 5 project managementChap 5 project management
Chap 5 project management
 
Assignment
AssignmentAssignment
Assignment
 
Aibo
AiboAibo
Aibo
 
A2
A2A2
A2
 
11 topic 9 ooa
11 topic 9 ooa11 topic 9 ooa
11 topic 9 ooa
 
10 architectural design
10 architectural design10 architectural design
10 architectural design
 
10 architectural design (1)
10 architectural design (1)10 architectural design (1)
10 architectural design (1)
 
8 system models
8 system models8 system models
8 system models
 
8 system models (1)
8 system models (1)8 system models (1)
8 system models (1)
 
8 bop
8 bop8 bop
8 bop
 
8 bop (1)
8 bop (1)8 bop (1)
8 bop (1)
 
7 foreign policy process
7 foreign policy process7 foreign policy process
7 foreign policy process
 
7 foreign policy process (1)
7 foreign policy process (1)7 foreign policy process (1)
7 foreign policy process (1)
 
5 cold war era 1
5 cold war era 15 cold war era 1
5 cold war era 1
 
3 national power
3 national power3 national power
3 national power
 
1 introduction
1 introduction1 introduction
1 introduction
 
1 introduction (1)
1 introduction (1)1 introduction (1)
1 introduction (1)
 
A1
A1A1
A1
 
Project final
Project finalProject final
Project final
 

Assignment 3

  • 1. 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 end = time(NULL); elapsed = end - start;
  • 2. 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 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 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; }