Skip to content
Snippets Groups Projects
Select Git revision
  • 021b956877da1c13723ab339d2b2c7c0e4bb6bb6
  • main default protected
  • develop
3 results

histo.c

Blame
  • histo.c 2.97 KiB
    /* Author : Dario GENGA
     * Date : 15.11.2021
     * Description : Manipulate an unidimensional array with dynamic memory allocation
     */
    #include "unidimensional_array.h"
    #include "time.h"
    
    int main() {
        srand(time(NULL));
        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();
        int *array = malloc(array_size * sizeof(int));
    
        // Fill the array with random values
        fill_array_with_random_values(array, array_size);
    
        // Print the array
        printf("Original array :\n");
        print_array(array, array_size);
    
        // Shuffle the array
        shuffle_array(array, array_size);
        printf("Array after shuffle :\n");
        print_array(array, array_size);
    
        // Perform a cyclic permutation
        perform_cyclic_permutation(array, array_size, cycle_number);
        printf("Array after cyclic permutation :\n");
        print_array(array, array_size);
    
        // Permute smallest value with the last value
        permute_lowest_value_with_last_value(array, array_size);
        printf("Array after swapping the smallest value with the last one :\n");
        print_array(array, array_size);
    
        // Descending sort the array by using the insertion algorithm
        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);
    
        // 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);
    
        // 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);
        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);
    
        // Convert the first array to an array of double
        double *converted_array = convert_int_array_to_double(array, array_size);
        printf("Array converted to double :\n");
        print_array_of_double(converted_array, array_size);
    
        // Free the memory
        free(array);
        free(second_array);
        free(result_sum_array);
        free(result_mul_array);
    
        return 0;
    }