diff --git a/histo.c b/histo.c index 1fd0d5db18d6685eff21f54a313ec433875296da..fc8716333f28dd912f4fd241d63ebd4dca5a2a5d 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