Click here to Skip to main content
14,733,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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.

I know I might use a while loop to repeat the program but I don't know where to place it.

Besides, I have no idea how to output the scores as required in criteria d, I have only managed to set the program to include certain words of encouragement for a designated range of scores.

Lastly, how do I include a fault tolerance function? After inputting the letter "a" into one of the equations, the program just ends. I need a message to prompt the user to input a number.

Any help would be greatly appreciated.

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct struc
{
    int a;
    int b;
    int c;
    int add;
    int grade;
};

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);
    }
    t->grade = 10;
}

void tcm_i(struct struc *t, int n)
{
    int ad;
    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);
    scanf(" %d", &ad);

    if (ad == t->add)
    {
        t->grade = 10;
        printf("\n Very good, you earned 10 marks\n\n");
    }
    else
    {
        printf("\n Sorry, you have 2 attempts remaining\n\n");
        scanf(" %d", &ad);
        if (ad == t->add)
        {
            t->grade = 7;
            printf("\n Good, you earned 7 marks\n\n");
        }
        else
        {
            printf("\n Sorry, you have 1 attempt remaining\n\n");
            scanf(" %d",&ad);
            if (ad == t->add)
            {
                t->grade = 5;
                printf("\n Not bad, you earned 5 marks\n\n");
            }
            else
            {
                t->grade = 0;
                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 i, j, g = 0;
    char x;
    struct struc test[10];

    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)For total scores above output"EXCELLENT",80-90"GOOD",70-80"AVERAGE",60-70"PASS",60"POOR"。\n");
    printf(" ................................................................................\n");
    printf(" ********************************************************************************\n");

    for (i = 0; i <= 9; 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: ");
    scanf("%c", &x);

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

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

    printf(" End");

    for (i = 0; i <= 9; i++)
        g = g + test[i].grade;

    if (g > 90)
        printf(" Excellent! You are a genius!\n\n");
    else if (g > 80)
        printf(" Not bad, keep it up!\n\n");
    else if (g > 70)
        printf(" Try harder next time!\n\n");
    else
        printf(" TRY AGAIN\n\n");
}
Posted
Updated 19-Nov-20 22:58pm
v3

As suggested, move some of your code from the main function to a dedicated one, then call it repeadtly.
Something like the following code
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>

struct struc
{
  int a;
  int b;
  int c;
  int add;
  int grade;
};

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);
  }
  t->grade = 10;
}

void tcm_i(struct struc *t, int n)
{
  int ad;
  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);
  scanf(" %d", &ad);

  if (ad == t->add)
  {
    t->grade = 10;
    printf("\n Very good, you earned 10 marks\n\n");
  }
  else
  {
    printf("\n Sorry, you have 2 attempts remaining\n\n");
    scanf(" %d", &ad);
    if (ad == t->add)
    {
      t->grade = 7;
    printf("\n Good, you earned 7 marks\n\n");
    }
    else
    {
      printf("\n Sorry, you have 1 attempt remaining\n\n");
      scanf(" %d",&ad);
      if (ad == t->add)
      {
        t->grade = 5;
        printf("\n Not bad, you earned 5 marks\n\n");
      }
      else
      {
        t->grade = 0;
        printf("\n Failure, 0 mark\n\n");
        printf("\n The correct answer is%d\n\n", t->add);
      }
    }
  }

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

int one_round()
{
  int i, j, g = 0;
  char x;
  struct struc test[10];
  for (i = 0; i <= 9; 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: ");
  scanf("%c", &x);

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

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

  printf(" End");

  for (i = 0; i <= 9; i++)
    g = g + test[i].grade;

  return g;
}


int main()
{

  int g_min, g_max, g_sum, round_counter;
  double g_avg;
  char c;
  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)For total scores above output"EXCELLENT",80-90"GOOD",70-80"AVERAGE",60-70"PASS",60"POOR"。\n");
  printf(" ................................................................................\n");
  printf(" ********************************************************************************\n");

  g_sum = 0;
  g_min = INT_MAX;
  g_max = INT_MIN;
  round_counter = 0;
  do
  {

    int g = one_round();

    ++round_counter;

    g_sum += g;
    if (g_min > g)
      g_min = g;
    if (g_max < g)
      g_max = g;

    do
    {
      printf(" Q - quit\n P - play another round\n S - show the results\n");
      scanf("%c", &c);
      if ( c == 'S')
      {
        if ( round_counter < 2)
        {
          printf("your grade is %d\n", g);
        }
        else
        {
          g_avg = (double) g_sum / round_counter;
          printf("your grade is %d, minimum %d, maximum %d, average %g\n", g, g_min, g_max, g_avg);
        }
      }
    }  while ( c != 'P' && c != 'Q');
  } while ( c != 'Q'  );

  return 0;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
   
Comments
maniacdancer 20-Nov-20 4:54am
   
Hi, thank you so much for your help!

However, when I input a non-number into an equation, the program just ends abruptly, without prompting the user to input a number, can you please look into this? Thank you.
CPallini 20-Nov-20 5:17am
   
See
http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
maniacdancer 20-Nov-20 7:39am
   
Thanks! Problem solved!
CPallini 20-Nov-20 9:08am
   
You are welcome.
Looks like you have done most of the work but now you have to debug your program. You should divide your code in more subfunctions and structs and more expressive names for functions and variables. By that you will find a good place for your while loop.

For that reason most IDE have a big toolset for debugging. Read this Debugger tutorial to get some knowledge how to do it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900