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

Add method to count elements in array

This method count the total of elements in the array that are smaler
than the specified value.
parent 1a483060
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@
int main() {
srand(time(NULL));
size_t cycle_number = 3;
size_t value = 0;
// Ask the user the size of the array
size_t array_size = ask_array_size();
int *array = malloc(array_size * sizeof(int));
......@@ -38,6 +39,12 @@ int main() {
sort_by_insertion_desc(array, array_size);
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);
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);
......
......@@ -97,6 +97,19 @@ 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) {
size_t total = 0;
for (size_t i = 0; i < array_size; i++)
{
if (array[i] < value) {
total++;
}
}
return total;
}
int find_index_highest_value_in_array(int* array, size_t array_size) {
int highest_value;
size_t index_highest_value = 0;
......
......@@ -23,6 +23,8 @@ void perform_cyclic_permutation(int* array, size_t array_size, size_t cycle_numb
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 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