Scores Output + Skipped Attempts + Repeated Equations

Discussion in 'C' started by Eve Wein, Nov 21, 2020.

Tags:
1. Eve WeinNew Member

Joined:
Nov 20, 2020
Messages:
1
1
Trophy Points:
1
Gender:
Female
I have written a program in C language. The program is designed for primary school students in Grades 1-2. Two integers are randomly selected, the numbers are either added or subtracted to form an equation, and the students are asked to solve the equation. (I'm using Xcode v12.2 to code)

Code criteria:

a. Each round of the game consists of 10 questions that are not to be repeated. 10 marks for each question. Each round of games is required to be set anew and is not have the same questions as the previous rounds.

b. Only the addition and subtraction within 100 is allowed, the sum and difference of the two numbers should not exceed the range of 100, and is not to have a negative value.

c. For each question, players have 3 chances to enter their answers. 10 points are awarded for the first correct attempt, 7 points for the second attempt, 5 points for the third attempt, zero points if all 3 attempts failed. When an incorrect answer is entered, a message should appear prompting the student to re-enter the answer. Output the correct answer if all 3 attempts are used in the form "No, the answer is X".

d. At the end of the test, a message should appear prompting the player to choose "Show results", by entering the letter "S", "Play another round", by entering the letter "P", and "Quit", by entering the letter "Q". If the player entered "S" after one round of the game, output the player's score (marks/100). If the player chooses "Show results" after multiple rounds of the game, the output should include the player's highest score/%, lowest score/%, and the average score/% of all games played. If the player enters "Q", the program ends.

e. Include fault tolerance function, a simple menu interface, necessary notes. The program should also meet the requirements of modularization and structurization.

f. No graphical interface is required.

This is what I have tried:

Code:
```#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_TESTS 10

bool myread(const char* format, void* address) {
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}

struct struc {
int a;

int b;

int c;

};

int sj(int n)
{
int t;

t = rand() % n;

return t;
}

void ctm_i(struct struc* t)
{
t->a = sj(101);

t->c = sj(4);

if (t->c == 1) {
t->b = sj(101 - (t->a));

t->add = (t->a) + (t->b);
}

else {
t->b = sj((t->a) + 1);

t->add = (t->a) - (t->b);
}

}

void tcm_i(struct struc* t, int n)
{

printf(
" ***********************************************************************"
"*********\n");

printf(
" ......................................................................."
".........\n");

printf(" Question %d\n\n", n + 1);

printf(" You have 3 attempts for this question\n\n");

if (t->c == 1)
printf(" %d+%d= ", t->a, t->b);

else
printf(" %d-%d= ", t->a, t->b);

{

printf("\n Very good, you earned 10 marks\n\n");

}

else {
printf("\n Sorry, you have 2 attempts remaining\n\n");

{

printf("\n Good, you earned 7 marks\n\n");

}

else {
printf("\n Sorry, you have 1 attempt remaining\n\n");

{

printf("\n Not bad, you earned 5 marks\n\n");

}

else {

printf("\n Failure, 0 mark\n\n");

printf("\n The correct answer is %d\n\n", t->add);
}
}
}

printf(
" ......................................................................."
".........\n");

printf(
" ***********************************************************************"
"*********\n");
}

int main()
{
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;

int i, j, g = 0;

char x;

struct struc test[MAX_TESTS];

srand((unsigned)time(NULL));

printf(
" ***********************************************************************"
"************\n");

printf(
" ......................................................................."
"............\n");

printf(
" ****************************Welcome!****************************\n\n");

printf(" ...........This program is for students Grades 1-2............\n");

printf("\n Description:\n");

printf(
"（1）Computer randomly selects 10 questions, each question is worth 10 "
"points, the end of the test shows the student score；\n");

printf(
"（2）Only addition and subtraction within 100 is allowed. The sum or "
"difference of two numbers do not exceed the range of 0-100, negative "
"numbers are not included；\n");

printf("（3）There are 3 attempts for each question.；\n");

printf(
"（4）For each question, 10 points will be awarded for the first "
"successful attempt, 7 points for the second attempt, and 5 points for "
"the third attempt；\n");

printf(
"（5）At the end of the game, enter 'P' to start a new game, 'S' to show the results, and 'Q' to quit.;\n\n");

printf(
" ......................................................................."
".........\n");

printf(
" ***********************************************************************"
"*********\n");

for (i = 0; i < MAX_TESTS; i++)

{
ctm_i(&test[i]);

for (j = 0; j < i; j++)

if (test[i].a == test[j].a && test[i].b == test[j].b &&
test[i].c == test[j].c)

ctm_i(&test[i]);
}

printf(" Are you ready? Please click on any key to continue: ");

getchar();

for (;;) {
rounds++;

for (i = 1; i <= 5; i++) {
printf(
" *******************************************************************"
"**"
"***********\n");

printf(
" ..................................................................."
".."
"...........\n");
}

for (i = 0; i < MAX_TESTS; i++) tcm_i(&test[i], i);

printf(" End\n\n");

bool done = false;
bool unsure = true;

while (unsure) {
unsure = false;
puts("Enter 'S' to show results");
puts("Enter 'P' to play another round");
puts("Enter 'Q' to quit");
char choice;
if (choice == 'Q')
done = true;
else if (choice == 'S') {
if (rounds == 1)
printf("Final score: %d/100\n", g);
else {
puts("Whoops! Looks like highest/lowest have not been adjusted!");
printf("Highest score: %d/100\n", highest);
printf("Lowest score: %d/100\n", lowest);
}
getchar();
} else if (choice == 'P') {
// Do nothing?
} else {
puts("Invalid input!");
unsure = true;
}
}
if (done) break;
}
}
```
I am encountering 3 problems with my output:

1. For the first game, the first question immediately skipped over the first attempt and automatically assumed that the player entered a wrong answer, which left the player at only two attempts.

2. I am unable to compile a code to give the scores as required in d, as I have no idea how to calculate the scores gained from the round/rounds the player has played:

3. I have questions repeated from both rounds 1 & 2, which is not allowed as detailed in a:
Round 1:

Round 2:

Any help or suggestions would be greatly appreciated.

Attached Files:

File size:
29 KB
Views:
386
File size:
41.8 KB
Views:
389
File size:
95.2 KB
Views:
395
File size:
113.3 KB
Views:
395
sarasara likes this.

Joined:
Jan 19, 2021
Messages:
5