QUESTION 1
Returns 1 if a, b and c are equal and 0 if they aren't.
QUESTION 2
a) Nothing is returned if n != 0. Add a return statement to the code in that
segment.
b) Result is not returned. Add return result;
c) int randomNumber should be called as such randomNumber(srand(n)). A number n
must be passed to srand. There is no need to declare int for randomNumber
QUESTION 3
a)
int main()
{
srand(time(0));
int rand_index = rand() % 5;
cout << "Your random number is: " << rand_index*3;
return 0;
}
b)
int main()
{
srand(time(0));
int rand_index = rand() % 6;
cout << "Your random number is: " << 3 + rand_index*2;
return 0;
}
c)
int main()
{
srand(time(0));
int rand_index = rand() % 4;
cout << "Your random number is: " << 6 + rand_index*4;
return 0;
}
QUESTION 4
void add_to_sum(int counter, double& sum){
sum += 1.0/counter;
}
double calc_total(int n, double& sum){
for (int i=1; i<=n; i++){
add_to_sum(i, sum);
}
return sum;
}
int main()
{
double sum=0;
int n;
cout << "Enter the number N: ";
cin >> n;
cout << "The result is: " << calc_total(n, sum);
return 0;
}
QUESTION 5
int flip(){
int item = rand()%2;
return item;
}
int main()
{
srand(time(0));
int heads=0, tails=0;
for(int i=0; i<100; i++){
int value = flip();
if (value == 0){
tails++;
}else{
heads++;
}
}
cout << "The total number of heads is " << heads << " and the the total number
of tails is " << tails << ".";
return 0;
}
QUESTION 6
int generateNum(){
int item = 1+ rand()%1000;
return item;
}
int generateRange(int segment, int range){
int sentinel = 1, placing;
while(sentinel != 0){
if (segment == 20){
return 0;
}else if (segment == 0){
return range;
}else{
placing = 1+ rand()%range;
if (placing+segment <= 20 && segment-(range-placing) >=0 ){
sentinel = 0;
}
}
}
return placing;
}
int main()
{
srand(time(0));
int sentinel = 1;
while(sentinel != 0){
int guess, randomNum, range;
string play;
randomNum = generateNum();
for(int i=10; i>0; i--){
int checker = 1;
int segment = randomNum/50;
range = 2*i;
int placing = generateRange(segment, range);
while (checker != 0){
if (i == 10){
cout << "Guess a number between 0 and 1000: ";
cin >> guess;
if (guess > 1000 || guess < 0){
cout << "The number you entered is out of range!" << endl;
}else{
checker = 0;
}
}else{
cout << endl << "Guess a number between " <<
(segment+placing)*50 << " and " << (segment-(range-placing))*50 << ": ";
cin >> guess;
if (guess > (segment+placing)*50 || guess < (segment-(range-
placing))*50){
cout << "The number you entered is out of range!" << endl;
}else{
checker = 0;
}
}
if (guess == randomNum){
cout << "You have correctly guessed the number!" << endl;
break;
}
}
if (guess != randomNum){
cout << "You lost, the number is " << randomNum << "!" << endl;
}
cout << "Would you like to play again? (y/n): ";
cin >> play;
if (play == "y"){
sentinel = 1;
}else{
sentinel = 0;
}
}
return 0;
}
QUESTION 7
int gcd(int x, int y){
if (y == 0) {
return x;
}
return gcd(y, x % y);
}
QUESTION 8
void generate_question(int& a, int& b){
a = rand() % 10;
b = rand() % 10;
}
double calculate_score(double correct, double answer_count){
return correct / answer_count;
}
int main()
{
int user_answer, a, b;
double correct=0, answer_count = 0;
while(1 > 0){
cout << "Welcome to Maths Practice App! Please answer the following 10
questions correctly." << endl << endl;
srand(time(0));
for (int i=1; i<11; i++){
int sentinel = 1;
if(answer_count == 10){
break;
}
generate_question(a, b);
while(sentinel != 0){
cout << "[Question " << i << "] What is " << a << "x" << b << "?
Answer: ";
cin >> user_answer;
answer_count++;
if(user_answer == a*b){
cout << "Very good!" << endl;
sentinel = 0;
correct++;
}else{
cout << "No. Please try again." << endl;
}
}
}
double score = calculate_score(correct, answer_count)*100;
cout << endl << "Your score is " << fixed << setprecision(2) << score <<
"%." << endl << endl;
if (score < 75){
cout << "Please ask your teacher for extra help.";
}else{
cout << "Congratulations! You are ready to go onto the next level!" <<
endl << endl;
}
correct=0;
answer_count=0;
return 0;
}