From 20b66b756ce95f890b622edc66f2bd3a1eb35930 Mon Sep 17 00:00:00 2001 From: Dario Genga <dario.genga@etu.hesge.ch> Date: Mon, 15 Nov 2021 22:15:14 +0100 Subject: [PATCH] Add two execution mode for the program USER_MODE : The program will ask the user for various value. COMPUTER_MODE : The programm will use randoms values. --- histo.c | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/histo.c b/histo.c index 1fd0d5d..fc87163 100644 --- a/histo.c +++ b/histo.c @@ -4,14 +4,22 @@ */ #include "unidimensional_array.h" #include "time.h" +#define USER_MODE 0 +#define COMPUTER_MODE 1 +#define MAX_RANDOM_VALUE 10 -int main() { - srand(time(NULL)); +void execute(int mode) { size_t cycle_number = 3; size_t value = 0; size_t multiply_value = 0; - // Ask the user the size of the array - size_t array_size = ask_array_size(); + size_t array_size; + + if (mode == USER_MODE) { + // Ask the user the size of the array + array_size = ask_array_size(); + } else if (mode == COMPUTER_MODE){ + array_size = rand() % MAX_RANDOM_VALUE + 1; + } int *array = malloc(array_size * sizeof(int)); // Fill the array with random values @@ -41,9 +49,13 @@ int main() { printf("Array after insertion desc sort :\n"); print_array(array, array_size); - // Ask the user a value and then return the total of elements that are smaller - printf("Type a value : \n"); - scanf("%ld", &value); + if (mode == USER_MODE) { + // Ask the user a value and then return the total of elements that are smaller + printf("Type a value : \n"); + scanf("%ld", &value); + } else if (mode == COMPUTER_MODE) { + value = rand() % MAX_RANDOM_VALUE + 1; + } size_t elements_with_lower_value = count_elements_in_array_lower_than_value(array, array_size, value); printf("Number of elements with lower value : %ld\n", elements_with_lower_value); @@ -59,8 +71,12 @@ int main() { // Create a fourth array that will stock the multiplication between the first array and a value from the user int *result_mul_array = malloc(array_size * sizeof(int)); - printf("Multiply the first array with the following value : \n"); - scanf("%ld", &multiply_value); + if (mode == USER_MODE) { + printf("Multiply the first array with the following value : \n"); + scanf("%ld", &multiply_value); + } else if (mode == COMPUTER_MODE) { + multiply_value = rand() % MAX_RANDOM_VALUE + 1; + } multiply_array_with_value(array, array_size, result_mul_array, multiply_value); printf("Result array after multiplication :\n"); print_array(result_mul_array, array_size); @@ -75,6 +91,16 @@ int main() { free(second_array); free(result_sum_array); free(result_mul_array); +} + +int main() { + srand(time(NULL)); + printf("Starting user mode...\n"); + execute(USER_MODE); + printf("Starting computer mode...\n"); + for (size_t i = 0; i < 100; i++) { + execute(COMPUTER_MODE); + } return 0; } \ No newline at end of file -- GitLab