wuiyang

Maths Quiz Example DO NOT COPY UPDATED

Aug 10th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.67 KB | None | 0 0
  1. /*
  2. UPDATED: for flowchart, modify only the break part to non break
  3. flowchart example:
  4. drawn with: draw.io
  5. main: http://i.imgur.com/XVfrg0k.png
  6. printQuestion: http://i.imgur.com/ssA5laG.png
  7. the rest: http://i.imgur.com/klSapQB.png
  8.  
  9. pseudocode example: https://pastebin.com/s9M3uRiN
  10.  
  11. An example for maths quiz
  12. just for reference, please DO NOT USE THIS AS YOUR FINAL ASSIGNMENT
  13. by: Tan Wui Yang
  14. for: ISE class
  15. total line of codes: 123
  16. */
  17.  
  18. #include <stdio.h> //for printf scanf etc
  19. #include <stdlib.h> //for rand()
  20. #include <time.h> //for time() for true random
  21. #pragma warning (disable:4996) //visual studio only
  22.  
  23. //function declaration
  24.  
  25. int printMainMenu();
  26. int printOptionMenu();
  27. int printLevelMenu();
  28. int printQuestion(int operationsType, int levelType);
  29. int finishedQuestion();
  30. int finishedQuestionCongrats();
  31. int getUserInput(int min, int max);
  32.  
  33. void main() {
  34.     //for true random
  35.     srand(time(NULL));
  36.  
  37.     //variable declaration
  38.     int userType, operationsType, levelType, correctQuestion, continueOrExit, i;
  39.     //declare i outside for loop cause codeblock bug (?)
  40.  
  41.     //print main menu and get user input (stored in variable 'userType')
  42.     userType = printMainMenu();
  43.  
  44.     if (userType == 1) {//user choose 1
  45.  
  46.         //do while loop
  47.         do {
  48.             system("cls"); //clear screen
  49.  
  50.             //print option menu and get user input (stored in variable 'operationsType')
  51.             operationsType = printOptionMenu();
  52.  
  53.             //user input 4 (EXIT), then all this below will be skipped
  54.             if (operationsType != 4) {
  55.                 //print level menu and get user input (stored in variable 'levelType')
  56.                 levelType = printLevelMenu();
  57.                 if (levelType != 4) {
  58.                     do {
  59.                         system("cls"); //clear screen
  60.  
  61.                         //reset correctQuestion after finish do while loop
  62.                         correctQuestion = 0;
  63.  
  64.                         //print 10 questions (from 0 to 9, which total of 10)
  65.                         for (i = 0; i < 10; i++) {
  66.                             //correctQuestion will +1 if user type correct answer
  67.                             correctQuestion += printQuestion(operationsType, levelType);
  68.  
  69.                         }
  70.                         //eg of the for loop flow (Y = yes, N = no
  71.                         //             when i = : 0  1  2  3  4  5  6  7  8  9  10
  72.                         //correctQuestion before: 0  1  2  2  2  3  4  4  5  5  5
  73.                         // is userAns == answer?: Y  Y  N  N  Y  Y  N  Y  N  N  N
  74.                         //printQuestion returned: 1  1  0  0  1  1  0  1  0  0  0
  75.                         // correctQuestion after: 1  2  2  2  3  4  4  5  5  5  5
  76.  
  77.  
  78.                         //Eg: You did 8(correctQuestion) out of 10 questions
  79.                         printf("You did %d out of 10 questions\n", correctQuestion);
  80.  
  81.  
  82.                         //for assignment specification: description V
  83.                         //if user perform well will ask if user want to go higher level
  84.  
  85.                         //if user get 80% or above (which is has 8 correct questions or above)
  86.                         //and level is not Advanced (cause there's no higher level above Advanced)
  87.                         //then
  88.                         if (correctQuestion >= 8 && levelType < 3) {
  89.                             //print congrats and prompt user if user want to go higher level and menu after finish question
  90.                             //and get user input (stored in continueOrExit)
  91.                             continueOrExit = finishedQuestionCongrats();
  92.  
  93.                             //if user wants to increase his level
  94.                             if (continueOrExit == 1) {
  95.                                 //levelType increased by 1
  96.                                 levelType++;
  97.                             }
  98.                             else {
  99.                                 //continueOrExit - 1 to adjust the select same as normal one
  100.                                 //COE = continueOrExit
  101.                                 //finishedQuestionCongrats()    COE  COE-1     finishedQuestion()     COE
  102.                                 //(2) continue with same level   2    [1]  ->  (1) continue           [1]
  103.                                 //(3) Exit to main menu          3    [2]  ->  (2) Exit to main menu  [2]
  104.                                 continueOrExit--;
  105.                             }
  106.                         }
  107.                         else { //If user did not get 80% or above, or level is Advanced
  108.                             //print menu after finish question and get user input (stored in continueOrExit)
  109.                             continueOrExit = finishedQuestion();
  110.                         }
  111.                        
  112.                         //if user selected exit to main menu
  113.                         if (continueOrExit == 2) {
  114.                             //change levelType to 4 to match the condition for my first do while loop
  115.                             levelType = 4;
  116.                         }
  117.                     } while (continueOrExit == 1); //loop back if user want to continue
  118.                 }
  119.             }
  120.         } while (levelType == 4 && operationsType != 4); //loop back if user want to back to main menu, if user want exit program at main menu then will skip loop
  121.     }
  122. }
  123.  
  124. int printQuestion(int operationsType, int levelType) {
  125.     //variable declaration
  126.     int num1, num2, answer, userAns, range;
  127.  
  128.     //if levelType == 1, which is beginner, range given is 0 to 10
  129.     if (levelType == 1) {
  130.         range = 11;
  131.     }
  132.     //if levelType == 2, which is intermediate, range given is 0 to 100
  133.     else if (levelType == 2) {
  134.         range = 101;
  135.     }
  136.     //if levelType == 3, which is advanced, range given is 0 to 1000
  137.     else if (levelType == 3) {
  138.         range = 1001;
  139.     }
  140.  
  141.     //generate random number at given range and store it to num1 and num2
  142.     //A % B, % = modulus, which means it give remainder when A divided by B
  143.     num1 = rand() % range;
  144.     num2 = rand() % range;
  145.  
  146.     //if operationsType == 1, which is +
  147.     if (operationsType == 1) {
  148.         answer = num1 + num2;
  149.         printf("%d + %d = ", num1, num2);
  150.     }
  151.     //if operationsType == 2, which is -
  152.     else if (operationsType == 2) {
  153.         answer = num1 - num2;
  154.         printf("%d - %d = ", num1, num2);
  155.     }
  156.     //if operationsType == 3, which is *
  157.     else if (operationsType == 3) {
  158.         answer = num1 * num2;
  159.         printf("%d * %d = ", num1, num2);
  160.     }
  161.  
  162.     //scan user input, which is given is int, and store it to userAns
  163.     scanf("%d", &userAns);
  164.  
  165.     //if userAns == answer
  166.     //even though (answer == userAns) is correct, but the comparison is weird
  167.     //eg: 5 = userAns   or   userAns = 5
  168.     if (userAns == answer) {
  169.         //tell the user he is correct
  170.         printf("You answer is correct!\n");
  171.  
  172.         //return 1 if he is correct, for correctQuestion
  173.         return 1;
  174.     }
  175.     else {
  176.         //tell the user he is incorrect and give the answer
  177.         printf("Your answer is incorrect, %d is the correct answer\n", answer);
  178.  
  179.         //return 0 if he is correct, for correctQuestion
  180.         return 0;
  181.     }
  182. }
  183.  
  184. int printMainMenu() {
  185.     //print the main menu
  186.     printf("Welcome to math quiz\n");
  187.     printf("(1) Start the math quiz\n");
  188.     printf("(2) Exit the math quiz\n");
  189.  
  190.     //get user input at given range from 1 to 2, and return the user input
  191.     return getUserInput(1, 2);
  192. }
  193.  
  194. int printOptionMenu() {
  195.     //print the arthimetic operations menu
  196.     printf("Please choose a arthimetic operations: \n");
  197.     printf("(1) +\n");
  198.     printf("(2) -\n");
  199.     printf("(3) *\n");
  200.     printf("(4) exit\n");
  201.  
  202.     //get user input at given range from 1 to 4, and return the user input
  203.     return getUserInput(1, 4);
  204. }
  205.  
  206. int printLevelMenu() {
  207.     //print the level option
  208.     printf("Please choose a level: \n");
  209.     printf("(1) Beginner\n");
  210.     printf("(2) Intermediate\n");
  211.     printf("(3) Advcanced\n");
  212.     printf("(4) Back to arthimetic options\n");
  213.  
  214.     //get user input at given range from 1 to 4, and return the user input
  215.     return getUserInput(1, 4);
  216. }
  217.  
  218. int finishedQuestion() {
  219.     //print after finished question
  220.     printf("Choose an option: \n");
  221.     printf("(1) Continue\n");
  222.     printf("(2) Exit to menu\n");
  223.  
  224.     //get user input at given range from 1 to 2, and return the user input
  225.     return getUserInput(1, 2);
  226. }
  227.  
  228. int finishedQuestionCongrats() {
  229.     //print after finished question when the user get 80% or above (correct 8 questions or above) and level is not advanced
  230.     printf("You're such a genius, congratulations you have owned all the questions\n");
  231.     printf("Choose an option: \n");
  232.     printf("(1) To next level\n");
  233.     printf("(2) Continue with same level\n");
  234.     printf("(3) Exit to menu\n");
  235.  
  236.     //get user input at given range from 1 to 3, and return the user input
  237.     return getUserInput(1, 3);
  238. }
  239.  
  240. int getUserInput(int min, int max) {
  241.     //variable declaration
  242.     int userType;
  243.  
  244.     //do (get user input) while (user is giving invalid option)
  245.     do {
  246.         printf("Select an option: ");
  247.  
  248.         //rewind just in case some shit happens
  249.         rewind(stdin);
  250.  
  251.         //scan user input, which is int, and store to userType
  252.         scanf("%d", &userType);
  253.  
  254.         //if userType is within min to max
  255.         //plargarism: this shit is copied
  256.         //eg: min = 1, max = 4
  257.        
  258.         //will print error and ask for input again if out of range
  259.         if (userType < min || userType > max) {
  260.             printf("Error! Invalid option");
  261.         }
  262.     } while (userType < min || userType > max);
  263.     //keep do it while userType is out of range  (either less than min, or more than max)
  264.    
  265.     //eg: userType within 1 to 4 are returned
  266.     return userType;
  267.  
  268.     //eg of how the getUserInput will looks like
  269.     //min = 0, max = 4
  270.     //Select an option: 5
  271.     //Error! Invalid option   (cause 5 > 4)
  272.     //select an option: -1
  273.     //Error! Invalid option   (cause -1 < 1)
  274.     //select an option: a
  275.     //Error! Invalid option   (if input is text, it will give weird number like 13895084 or -31538989)
  276.     //select an option: 2
  277.     //return 2;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment