OOP Lab Basic Questions Assignment
OOP Lab Basic Questions Assignment
Enrollment #: 01-134181-067
Class: BSCS-2A
Objective
The objective of this lab is to assess the problem solving capabilities of the students using the
Exercise 1
Write a program for a simple arithmetic quiz. The program should first ask the
user the number of questions to ask and the difficulty level. The three levels are
as follows:
After having done all the questions, the program displays the total number of
correct and wrong answers as well as the percentage of correct answers.
Hints:
a + rand() % b produces a random number between a and a+b (a+b is not included).
Use srand(time(0)) once in your program to allow different questions in every run of the
program.
The type of question (add, subtract, multiply) can also be chosen using the rand() function.
CODE:
#include <iostream>
#include <conio.h>
#include<time.h>
#include<cstdlib>
using namespace std;
int main()
{
int correct = 0, wrong = 0, choice, limit;
float total;
srand(time(0));
cout << " Please specify the level of difficulty that you can handle ? ";
cin >> choice;
cout << endl;
//Now with the help of the below code, we will prompt the user to enter answers to
questions that will be generated randomly out of the specified pool of numbers. The
difficulty level will be user-specified and the user will also be able to choose between
1,2 and 3-digit questions. The statements will keep track of both correct and wrong
answers and that will be used to further calculate the relevant percentages.
switch (choice)
{
case 1:
cout<<endl << " Number of questions ? ";
cin >> limit;
cout << endl;
for (int i = 0; i < limit; i++)
{
int num1, num2, ans,a,b,c;
num1 = rand() % 10;
num2 = rand() % 10;
int m = (rand() % 3) + 1;
if (m == 1)
{
cout<< num1 << " + " << num2 << " = ";
cin >> ans;
a = num1 + num2;
if (ans == a)
correct++;
else wrong++;
}
else if (m == 2)
{
cout << num1 << " - " << num2 << " = ";
cin >> ans;
b = num1 - num2;
if (ans == b)
correct++;
else wrong++;
}
else if (m == 3)
{
cout << num1 << " * " << num2 << " = ";
cin >> ans;
c = num1 * num2;
if (ans == c)
correct++;
else
wrong++;
}
}
break;
case 2:
cout << " Number of questions ? ";
cin >> limit;
cout << endl;
for (int i = 0; i < limit; i++)
{
int num1, num2, ans,a,b,c;
num1 = (rand() % 100) + 10;
num2 = (rand() % 100) + 10;
int m = (rand() % 3) + 1;
if (m == 1)
{
cout << num1 << " + " << num2 << " = ";
cin >> ans;
a = num1 + num2;
if (ans == a)
correct++;
else wrong++;
}
else if (m == 2)
{
cout << num1 << " - " << num2 << " = ";
cin >> ans;
b = num1 - num2;
if (ans == b)
correct++;
else wrong++;
}
else if (m == 3)
{
cout << num1 << " * " << num2 << " = ";
cin >> ans;
c = num1 * num2;
if (ans == c)
correct++;
else wrong++;
}
}
break;
case 3:
cout << " Number of questions ? ";
cin >> limit;
cout << endl;
for (int i = 0; i < limit; i++)
{
int num1, num2, ans,a,b,c;
num1 = (rand() % 1000) + 100;
num2 = (rand() % 1000) + 100;
int m = (rand() % 3) + 1;
if (m == 1)
{
cout << num1 << " + " << num2 << " = ";
cin >> ans;
a = num1 + num2;
if (ans == a)
correct++;
else wrong++;
}
else if (m == 2)
{
cout << num1 << " - " << num2 << " = ";
cin >> ans;
b = num1 - num2;
if (ans == b)
correct++;
else wrong++;
}
else if (m == 3)
{
cout << num1 << " * " << num2 << " = ";
cin >> ans;
c= num1 * num2;
if (ans == c)
correct++;
else wrong++;
}
}
break;
}
//Now we will calculate and display the number of correct and wrong answers
provided by the user. We will also calculate the percentage of each one of them.
cout <<endl<< " No. of questions answered CORRECTLY = " << correct;
cout << endl;
cout <<endl<<" No. of questions answered WRONG = " << wrong;
total = correct + wrong;
float c = (correct / total) * 100;
float w = (wrong / total) * 100;
cout << endl;
cout <<endl<< " % CORRECT answers = " << c;
cout << endl;
cout <<endl<< " % WRONG answers = " << w;
cout << endl << endl;
system("pause");
return 0;
}
Exercise 2
Write a program where the computer thinks of a secret number between 0 and
9. Two players then guess the number by providing an integer value one by one.
The program terminates as soon as one of the player guesses the hidden value.
If none of the players could guess the value in 10 attempts, computer wins.
CODE:
#include<iostream>
#include<time.h>
#include<cstdlib>
using namespace std;
int main()
{
int roulette, limit = 0;
srand(time(0));
roulette = (rand() % 10) + 1;
cout << " WELCOME TO THE GAME. GET YOUR THINKING CAPS ON AND GET READY TO HAVE FUN
"<<endl <<endl;
while (limit <= 10)
{
int first = 0, second = 0;
cout << "It's Player 1's turn to make a guess. Please enter = \t";
cin >> first;
if (first == roulette)
{
cout << "Player 1 guessed it right. CONGATULATIONS. What a lucky
guess!";
break;
}
else
cout << "It's Player 2's turn to make a guess. Please enter = \t";
cin >> second;
if (roulette == second)
{
cout << "Player 2 guessed it right. CONGATULATIONS. What a lucky
guess!\n";
break;
}
limit++;
}
system("pause");
return 0;
}
Do the aforementioned Exercises and get them checked by your instructor. If you are unable
to complete the task in the lab session, deposit this journal alongwith your programs (printed
1. Exercise 1
2. Exercise 2
+++++++++++++++++++++++++
Object Oriented Programming Page 2