Scores Output + Skipped Attempts + Repeated Equations

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

Tags:
  1. Eve Wein

    Eve Wein New Member

    Joined:
    Nov 20, 2020
    Messages:
    1
    Likes Received:
    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 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);
     
      myread(" %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");
     
        myread(" %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");
     
          myread(" %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 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;
          myread("%c", &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.

    upload_2020-11-21_12-16-17.png
    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:

    upload_2020-11-21_12-16-32.png


    3. I have questions repeated from both rounds 1 & 2, which is not allowed as detailed in a:
    Round 1:
    upload_2020-11-21_12-16-55.png

    Round 2:

    upload_2020-11-21_12-17-2.png

    Any help or suggestions would be greatly appreciated.
     

    Attached Files:

    sarasara likes this.
  2. sarasara

    sarasara New Member

    Joined:
    Jan 19, 2021
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Location:
    tehran
    Home Page:
    https://www.mmsiz.com/
    really helpful thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice