Using c++ Write a CAI (Computer Assisted Instruction) program as discussed in cl
ID: 3899457 • Letter: U
Question
Using c++
Write a CAI (Computer Assisted Instruction) program as discussed in class to ask a student for the answer to 10 random 0 only Pick two random integers from assign to num1 and num2 and pick a random integer 0 and 1 for the the operation OP If OP s 0 it is addition, 1is of addition and subtraction using integers from 1 to 1 subtraction If it is a subtraction you must make sure that the first number Num1 is bigger than the second number Num2 if it is not then swap the numbers by calling a function Swap that you will define. Each correct problem is awarded 10 points Display the total score at the end of the problems Sample run What is 10+5-27 Incorrect 10+5-15 What is 9.4-75 Correctl (etc) Your total score is ?Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks.
//Code
#include<iostream>
#include<stdlib.h> //for random number generator
#include<time.h>//for seeding random number generator
using namespace std;
//method to swap two integers
void swap(int &a, int &b){
//swapping a and b using a temporary variable
int temp=a;
a=b;
b=temp;
}
//method to generate and return a random number between 1 and 10 (inclusive)
int generateRandomNumber(){
//generating a value between 1 and 10
int num=(rand()%10) +1;
return num;
}
//method to generate and return a random value under 2 (either 0 or 1)
int generateRandomOperation(){
//generating either 0 or 1
int num=rand()%2;
return num;
}
int main(){
srand(time(NULL));//seeding random number generator
int score=0;//initial score
int num_questions=10; //number of questions
//other required variables
int num1,num2,res, user_entry;
char op;
//looping for 10 times
for(int i=1;i<=num_questions;i++){
//generating numbers
num1=generateRandomNumber();
num2=generateRandomNumber();
//swapping if necessary
if(num1<num2){
swap(num1,num2);
}
//generating random operation
if(generateRandomOperation()==0){
//addition
op='+';
res=num1+num2; //storing result
}else{
//subtraction
op='-';
res=num1-num2;//storing result
}
//asking question
cout<<"Q("<<i<<") What is "<<num1<<op<<num2<<"?: ";
//getting answer
cin>>user_entry;
if(user_entry==res){
//right
cout<<"Correct!"<<endl;
score++;
}else{
//wrong
cout<<"Incorrect! "<<num1<<op<<num2<<"="<<res<<endl;
}
}
//displaying final score
cout<<"Your total score is "<<score<<endl;
return 0;
}
/*OUTPUT*/
Q(1) What is 7+3?: 10
Correct!
Q(2) What is 10+1?: 12
Incorrect! 10+1=11
Q(3) What is 5-3?: 2
Correct!
Q(4) What is 1-1?: 0
Correct!
Q(5) What is 8-2?: 6
Correct!
Q(6) What is 4+4?: 3
Incorrect! 4+4=8
Q(7) What is 8-7?: 1
Correct!
Q(8) What is 10-3?: 7
Correct!
Q(9) What is 4+1?: 5
Correct!
Q(10) What is 10-6?: 4
Correct!
Your total score is 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.