From 0cba14b20a25bed1da88f7e5804be7fea1f028ff Mon Sep 17 00:00:00 2001 From: fdlt3914 Date: Fri, 10 Feb 2023 05:27:03 +0000 Subject: [PATCH] refactoring: simplified randomisation in easy_math --- src/main/quizproject.c | 50 ++++++++++++++++++------------------------ src/main/quizproject.h | 1 + 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/main/quizproject.c b/src/main/quizproject.c index 6270889..51f18ab 100644 --- a/src/main/quizproject.c +++ b/src/main/quizproject.c @@ -27,6 +27,24 @@ int* randomNumber() { return arr; } +int* randomNumber1() { + srand(time(NULL)); + int k = 0, p; + static int arr[32]; + while(k<32){ + int num =rand()%32; + for (p = 0; p < k; p++){ + if(arr[p]==num){ + break; + } + } + if(p==k){ + arr[k++]=num; + } + } + return arr; +} + //------------Math_quiz_begin------------ @@ -115,21 +133,8 @@ void math_choose_question(int num1) { }; - srand(time(NULL)); - int k = 0, p; - static int arr[32]; - while(k<32) { - int num = rand() % 32; - for (p = 0; p < k; p++) { - if (arr[p] == num) { - break; - } - } - if (p == k) { - arr[k++] = num; - } - } - + int* arr = randomNumber1(); + int num2 = arr[num1]; printf("%s", choose[num2]); } @@ -170,20 +175,7 @@ int math_answer(int num1) { 80, 10 }; - srand(time(NULL)); - int k = 0, p; - static int arr[32]; - while(k<32) { - int num = rand() % 32; - for (p = 0; p < k; p++) { - if (arr[p] == num) { - break; - } - } - if (p == k) { - arr[k++] = num; - } - } + int* arr = randomNumber1(); int num2 = arr[num1]; return ans[num2]; diff --git a/src/main/quizproject.h b/src/main/quizproject.h index 0dfdafa..c90cc7c 100644 --- a/src/main/quizproject.h +++ b/src/main/quizproject.h @@ -151,6 +151,7 @@ void math_quiz_option(); void math_solution(char [][10], int); void goodbye_message(); void final_score(); +int* randomNumber1(); //-------brain_whiz_header_end-------------