Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- UPDATED: for flowchart, modify only the break part to non break
- flowchart example:
- drawn with: draw.io
- main: http://i.imgur.com/XVfrg0k.png
- printQuestion: http://i.imgur.com/ssA5laG.png
- the rest: http://i.imgur.com/klSapQB.png
- pseudocode example: https://pastebin.com/s9M3uRiN
- An example for maths quiz
- just for reference, please DO NOT USE THIS AS YOUR FINAL ASSIGNMENT
- by: Tan Wui Yang
- for: ISE class
- total line of codes: 123
- */
- #include <stdio.h> //for printf scanf etc
- #include <stdlib.h> //for rand()
- #include <time.h> //for time() for true random
- #pragma warning (disable:4996) //visual studio only
- //function declaration
- int printMainMenu();
- int printOptionMenu();
- int printLevelMenu();
- int printQuestion(int operationsType, int levelType);
- int finishedQuestion();
- int finishedQuestionCongrats();
- int getUserInput(int min, int max);
- void main() {
- //for true random
- srand(time(NULL));
- //variable declaration
- int userType, operationsType, levelType, correctQuestion, continueOrExit, i;
- //declare i outside for loop cause codeblock bug (?)
- //print main menu and get user input (stored in variable 'userType')
- userType = printMainMenu();
- if (userType == 1) {//user choose 1
- //do while loop
- do {
- system("cls"); //clear screen
- //print option menu and get user input (stored in variable 'operationsType')
- operationsType = printOptionMenu();
- //user input 4 (EXIT), then all this below will be skipped
- if (operationsType != 4) {
- //print level menu and get user input (stored in variable 'levelType')
- levelType = printLevelMenu();
- if (levelType != 4) {
- do {
- system("cls"); //clear screen
- //reset correctQuestion after finish do while loop
- correctQuestion = 0;
- //print 10 questions (from 0 to 9, which total of 10)
- for (i = 0; i < 10; i++) {
- //correctQuestion will +1 if user type correct answer
- correctQuestion += printQuestion(operationsType, levelType);
- }
- //eg of the for loop flow (Y = yes, N = no
- // when i = : 0 1 2 3 4 5 6 7 8 9 10
- //correctQuestion before: 0 1 2 2 2 3 4 4 5 5 5
- // is userAns == answer?: Y Y N N Y Y N Y N N N
- //printQuestion returned: 1 1 0 0 1 1 0 1 0 0 0
- // correctQuestion after: 1 2 2 2 3 4 4 5 5 5 5
- //Eg: You did 8(correctQuestion) out of 10 questions
- printf("You did %d out of 10 questions\n", correctQuestion);
- //for assignment specification: description V
- //if user perform well will ask if user want to go higher level
- //if user get 80% or above (which is has 8 correct questions or above)
- //and level is not Advanced (cause there's no higher level above Advanced)
- //then
- if (correctQuestion >= 8 && levelType < 3) {
- //print congrats and prompt user if user want to go higher level and menu after finish question
- //and get user input (stored in continueOrExit)
- continueOrExit = finishedQuestionCongrats();
- //if user wants to increase his level
- if (continueOrExit == 1) {
- //levelType increased by 1
- levelType++;
- }
- else {
- //continueOrExit - 1 to adjust the select same as normal one
- //COE = continueOrExit
- //finishedQuestionCongrats() COE COE-1 finishedQuestion() COE
- //(2) continue with same level 2 [1] -> (1) continue [1]
- //(3) Exit to main menu 3 [2] -> (2) Exit to main menu [2]
- continueOrExit--;
- }
- }
- else { //If user did not get 80% or above, or level is Advanced
- //print menu after finish question and get user input (stored in continueOrExit)
- continueOrExit = finishedQuestion();
- }
- //if user selected exit to main menu
- if (continueOrExit == 2) {
- //change levelType to 4 to match the condition for my first do while loop
- levelType = 4;
- }
- } while (continueOrExit == 1); //loop back if user want to continue
- }
- }
- } 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
- }
- }
- int printQuestion(int operationsType, int levelType) {
- //variable declaration
- int num1, num2, answer, userAns, range;
- //if levelType == 1, which is beginner, range given is 0 to 10
- if (levelType == 1) {
- range = 11;
- }
- //if levelType == 2, which is intermediate, range given is 0 to 100
- else if (levelType == 2) {
- range = 101;
- }
- //if levelType == 3, which is advanced, range given is 0 to 1000
- else if (levelType == 3) {
- range = 1001;
- }
- //generate random number at given range and store it to num1 and num2
- //A % B, % = modulus, which means it give remainder when A divided by B
- num1 = rand() % range;
- num2 = rand() % range;
- //if operationsType == 1, which is +
- if (operationsType == 1) {
- answer = num1 + num2;
- printf("%d + %d = ", num1, num2);
- }
- //if operationsType == 2, which is -
- else if (operationsType == 2) {
- answer = num1 - num2;
- printf("%d - %d = ", num1, num2);
- }
- //if operationsType == 3, which is *
- else if (operationsType == 3) {
- answer = num1 * num2;
- printf("%d * %d = ", num1, num2);
- }
- //scan user input, which is given is int, and store it to userAns
- scanf("%d", &userAns);
- //if userAns == answer
- //even though (answer == userAns) is correct, but the comparison is weird
- //eg: 5 = userAns or userAns = 5
- if (userAns == answer) {
- //tell the user he is correct
- printf("You answer is correct!\n");
- //return 1 if he is correct, for correctQuestion
- return 1;
- }
- else {
- //tell the user he is incorrect and give the answer
- printf("Your answer is incorrect, %d is the correct answer\n", answer);
- //return 0 if he is correct, for correctQuestion
- return 0;
- }
- }
- int printMainMenu() {
- //print the main menu
- printf("Welcome to math quiz\n");
- printf("(1) Start the math quiz\n");
- printf("(2) Exit the math quiz\n");
- //get user input at given range from 1 to 2, and return the user input
- return getUserInput(1, 2);
- }
- int printOptionMenu() {
- //print the arthimetic operations menu
- printf("Please choose a arthimetic operations: \n");
- printf("(1) +\n");
- printf("(2) -\n");
- printf("(3) *\n");
- printf("(4) exit\n");
- //get user input at given range from 1 to 4, and return the user input
- return getUserInput(1, 4);
- }
- int printLevelMenu() {
- //print the level option
- printf("Please choose a level: \n");
- printf("(1) Beginner\n");
- printf("(2) Intermediate\n");
- printf("(3) Advcanced\n");
- printf("(4) Back to arthimetic options\n");
- //get user input at given range from 1 to 4, and return the user input
- return getUserInput(1, 4);
- }
- int finishedQuestion() {
- //print after finished question
- printf("Choose an option: \n");
- printf("(1) Continue\n");
- printf("(2) Exit to menu\n");
- //get user input at given range from 1 to 2, and return the user input
- return getUserInput(1, 2);
- }
- int finishedQuestionCongrats() {
- //print after finished question when the user get 80% or above (correct 8 questions or above) and level is not advanced
- printf("You're such a genius, congratulations you have owned all the questions\n");
- printf("Choose an option: \n");
- printf("(1) To next level\n");
- printf("(2) Continue with same level\n");
- printf("(3) Exit to menu\n");
- //get user input at given range from 1 to 3, and return the user input
- return getUserInput(1, 3);
- }
- int getUserInput(int min, int max) {
- //variable declaration
- int userType;
- //do (get user input) while (user is giving invalid option)
- do {
- printf("Select an option: ");
- //rewind just in case some shit happens
- rewind(stdin);
- //scan user input, which is int, and store to userType
- scanf("%d", &userType);
- //if userType is within min to max
- //plargarism: this shit is copied
- //eg: min = 1, max = 4
- //will print error and ask for input again if out of range
- if (userType < min || userType > max) {
- printf("Error! Invalid option");
- }
- } while (userType < min || userType > max);
- //keep do it while userType is out of range (either less than min, or more than max)
- //eg: userType within 1 to 4 are returned
- return userType;
- //eg of how the getUserInput will looks like
- //min = 0, max = 4
- //Select an option: 5
- //Error! Invalid option (cause 5 > 4)
- //select an option: -1
- //Error! Invalid option (cause -1 < 1)
- //select an option: a
- //Error! Invalid option (if input is text, it will give weird number like 13895084 or -31538989)
- //select an option: 2
- //return 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment