Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / Function to ask a question bool ask _ question ( int max _ num ) { int num 1 = generate _ number

// Function to ask a question
bool ask_question(int max_num){
int num1= generate_number(max_num);
int num2= generate_number(max_num);
int answer, user_answer;
printf("What is %d +%d?", num1, num2);
scanf("%d", &user_answer);
answer = num1+ num2;
if (user_answer == answer){
printf("Correct!
");
return true;
} else {
printf("Incorrect!
");
return false;
}
}
int main(){
srand(time(NULL)); // Initialize random seed
int difficulty, lives, max_num, questions, score =0;
printf("Welcome!
");
printf("Select difficulty:
");
printf("1. Easy
");
printf("2. Medium
");
printf("3. Hard
");
// Prompt the user until a valid difficulty is selected
do {
printf("Enter your choice (1,2, or 3): ");
scanf("%d", &difficulty);
} while (difficulty <1|| difficulty >3);
// Set variables based on difficulty
switch (difficulty){
case 1:
lives =3;
max_num =10;
questions =5;
break;
case 2:
lives =2;
max_num =30;
questions =10;
break;
case 3:
lives =1;
max_num =60;
questions =15;
break;
}
// Loop through questions
for (int i =0; i < questions; i++){
printf("Question %d of %d. You have %s remaining.
", i +1, questions, (lives ==1)?"1 life" : "2 lives");
// Check if it's the challenge question
if (i == questions -1){
printf("Challenge question!
");
if (!ask_question(max_num *2)){
lives =0;
break;
}
} else {
if (!ask_question(max_num)){
lives--;
if (lives ==0){
printf("Out of lives, game over!
");
break;
}
} else {
score++;
}
}
}
// Calculate percentage
int percentage =(score *100)/ questions;
// Print test complete message
printf("Test complete!
");
printf("You scored %d/%d (%d%%).
", score, questions, percentage);
// Print grade
if (percentage >=80)
printf("Grade: High Distinction
");
else if (percentage >=70)
printf("Grade: Distinction
");
else if (percentage >=60)
printf("Grade: Credit
");
else if (percentage >=50)
printf("Grade: Pass
");
else
printf("Grade: Fail
");
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

Students also viewed these Databases questions