Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Forum » x-Forum » Question

Repeat C program and output the scores obtained by user

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.

This is my code:


        #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");
        }

Code criteria not fulfilled:

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.


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. 




        

Posted by EveWein 2020-11-19 22:15:00

No Ans Posted

Please login to comment

Log In


Ask New Question