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

Add method to compute two array

Also removed the method to find the highest value in the array, who is
not needed for the exercice.
parent d5494240
No related branches found
No related tags found
No related merge requests found
......@@ -45,12 +45,21 @@ int main() {
scanf("%ld", &value);
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);
// Swap the highest value of the array with the last element of the array
size_t index_highest_value = find_index_highest_value_in_array(array, array_size);
swap(&array[index_highest_value], &array[array_size - 1]);
// Create a second array and compute it with the first one to a third array
int *second_array = malloc(array_size * sizeof(int));
int *result_sum_array = malloc(array_size * sizeof(int));
fill_array_with_random_values(second_array, array_size);
printf("Second array :\n");
print_array(second_array, array_size);
compute_two_array(array, second_array, result_sum_array, array_size);
printf("Result array :\n");
print_array(result_sum_array, array_size);
// Free the memory
free(array);
free(second_array);
free(result_array);
return 0;
}
\ No newline at end of file
......@@ -110,19 +110,10 @@ size_t count_elements_in_array_lower_than_value(int* array, size_t array_size, i
return total;
}
int find_index_highest_value_in_array(int* array, size_t array_size) {
int highest_value;
size_t index_highest_value = 0;
void compute_two_array(int* first_array, int* second_array, int* result_array, size_t array_size) {
for (size_t i = 0; i < array_size; i++) {
if (i == 0) {
highest_value = array[i];
} else if (array[i] > highest_value) {
highest_value = array[i];
index_highest_value = i;
}
result_array[i] = first_array[i] + second_array[i];
}
return index_highest_value;
}
void swap(int *x, int *y)
......
......@@ -13,8 +13,6 @@ void fill_array_with_random_values(int* array, size_t array_size);
int find_lowest_value_index_in_array(int* array, size_t array_size);
int find_index_highest_value_in_array(int* array, size_t array_size);
void permute_lowest_value_with_last_value(int* array, size_t array_size);
void shuffle_array(int* array, size_t array_size);
......@@ -25,6 +23,8 @@ void sort_by_insertion_desc(int* array, size_t array_size);
size_t count_elements_in_array_lower_than_value(int* array, size_t array_size, int value);
void compute_two_array(int* first_array, int* second_array, int* result_array, size_t array_size);
void print_array(int* array, size_t array_size);
void swap(int *x, int *y);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment