Skip to content
Snippets Groups Projects
Commit 20b66b75 authored by dario.genga's avatar dario.genga
Browse files

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.
parent 021b9568
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment