diff --git a/README.md b/README.md index 57ff576e635f004b698d22d80afa3081c680153e..f374d038428e936acf58897df06ab0726ee97dbf 100644 --- a/README.md +++ b/README.md @@ -15,72 +15,3 @@ Use this command to compile the project. > `make clean` Use this command to clean the project. - -## Visual Studio Code configuration - -You will find below the base configuration for the launch.json and tasks.json files in Visual Studio Code. Depending on your environment you will need to modify those files. - -### launch.json - -```json -{ - "version": "0.2.0", - "configurations": [ - { - "name": "gcc-9 - Build and debug active file", - "type": "cppdbg", - "request": "launch", - "program": "${fileDirname}/histo", - "args": [], - "stopAtEntry": false, - "cwd": "${fileDirname}", - "environment": [], - "externalConsole": false, - "MIMode": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ], - "preLaunchTask": "C/C++: gcc-9 build active file", - "miDebuggerPath": "/usr/bin/gdb" - } - ] -} -``` - -### tasks.json - -```json -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: gcc-9 build active file", - "command": "/usr/bin/gcc-9", - "args": [ - "-fdiagnostics-color=always", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}", - "-lm" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} -``` diff --git a/histo.c b/histo.c index f9dcfb540f391961574807aef818043bf5941835..0c0e596fac00b73cd5ab828ea3be93ef63d5c1a3 100644 --- a/histo.c +++ b/histo.c @@ -1,10 +1,13 @@ - +/* Author : Dario GENGA + * Date : 15.11.2021 + * Description : Manipulate an unidimensional array with dynamic memory allocation + */ #include "unidimensional_array.h" int main() { // Ask the user the size of the array size_t array_size = ask_array_size(); - int array[array_size]; + int *array = malloc(array_size * sizeof(int)); // Fill the array with random values fill_array_with_random_values(array, array_size); @@ -17,23 +20,7 @@ int main() { size_t index_highest_value = find_index_highest_value_in_array(array, array_size); swap(&array[index_highest_value], &array[array_size - 1]); - // Calculate the average value of the array - int average = get_average_in_array(array, array_size); - printf("Average : %d\n", average); - - // Calculate the variances of the elemenst in the array - int variance = get_variance_in_array(array, array_size); - printf("Variance : %d\n", variance); - - // Asc sort the array - sort_array_asc(array, array_size); - - // Find the median value - int median = get_median_value(array, array_size); - printf("Median : %d\n", median); - - // Check the equitability of the random number generator - create_histo(array, array_size); + free(array); return 0; } \ No newline at end of file diff --git a/makefile b/makefile index 30b672d01c22a17ceb50bbacd9d401fb283b78c8..8f33e37e904deb9abfb58a44736eb6e0b4ef013f 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,8 @@ LIB=-lm -CC=gcc -Wall -Wextra +CC=gcc -Wall -Wextra -g histo:unidimensional_array.o histo.o - gcc $^ -o $@ $(LIB) + gcc $^ -fsanitize=address -o $@ $(LIB) unidimensional_array.o: unidimensional_array.c unidimensional_array.h $(CC) -c $< $(LIB) diff --git a/unidimensional_array.c b/unidimensional_array.c index a6739ee7a8fb7ba3b252af4a18cab1278e861663..25a288fc988126d670ed960c3f6f02f3251df078 100644 --- a/unidimensional_array.c +++ b/unidimensional_array.c @@ -1,14 +1,11 @@ /* Author : Dario GENGA - * Date : 12.10.2021 - * Description : Manipulate an unidimensional array + * Date : 15.11.2021 + * Description : Manipulate an unidimensional array with dynamic memory allocation */ #include "unidimensional_array.h" #include <stdio.h> -#include <stdlib.h> #include <time.h> -#include <math.h> -#define PERCENT_OF_MAX_ARRAY_VALUE 10 size_t ask_array_size() { size_t array_size = 0; @@ -18,7 +15,7 @@ size_t ask_array_size() { } void fill_array_with_random_values(int array[], size_t array_size) { - size_t max_value = array_size / PERCENT_OF_MAX_ARRAY_VALUE; + size_t max_value = array_size - 1; srand(time(0)); @@ -63,113 +60,3 @@ void swap(int *x, int *y) *x = *y; *y = tmp; } - -int get_average_in_array(int array[], size_t array_size) { - int sum = 0, average = 0; - - for (size_t i = 0; i < array_size; i++) { - sum += array[i]; - } - average = sum / (int)array_size; - - return average; -} - -int get_variance_in_array(int array[], size_t array_size) { - int average = get_average_in_array(array, array_size); - int variance = 0; - - for (size_t i = 0; i < array_size; i++) { - int x = array[i] - average; - variance += pow(x, 2); - } - - variance = variance / (int)array_size; - return variance; -} - -void sort_array_asc(int array[], size_t array_size) -{ - int is_array_sorted = 0; - - while (is_array_sorted == 0) - { - is_array_sorted = 1; - - for (size_t i = 0; i < array_size; i++) - { - int previous_index = i - 1; - int current_value = array[i]; - - if (previous_index >= 0 && current_value < array[previous_index]) - { - // Swap the current value with the previous one - is_array_sorted = 0; - swap(&array[i], &array[previous_index]); - } - } - } -} - -int get_median_value(int array[], size_t array_size) { - int median = 0; - size_t index1 = (array_size - 1) / 2; - - if (array_size % 2 == 0) { - size_t index2 = (array_size / 2); - int val1 = array[index1]; - int val2 = array[index2]; - median = (val1 + val2) / 2; - } else { - median = array[index1]; - } - return median; -} - -void create_histo(int array[], size_t array_size) { - size_t histo_size = array_size / PERCENT_OF_MAX_ARRAY_VALUE + 1; // +1 because we didn't exclude the maximal value with the random - int histo[histo_size]; - - // Initialize the element of the histo - for (size_t i = 0; i < histo_size; i++) - { - histo[i] = 0; - } - - // Create the histo - for (size_t i = 0; i < array_size; i++) { - int value = array[i]; - histo[value] += 1; - } - - // Print horizontally the histo - printf("\nHorizontal histo :\n"); - for (size_t i = 0; i < histo_size; i++) - { - printf("%ld : %d\n", i, histo[i]); - } - - // Print vertically the histo - printf("\nVertical histo :\n"); - for (size_t i = 0; i < histo_size; i++) - { - printf("%ld", i); - } - printf("\n"); - - int index_highest_value = find_index_highest_value_in_array(histo, (size_t)histo_size); - int highest_value = histo[index_highest_value]; - - for (size_t i = 0; i < highest_value; i++) - { - for (size_t x = 0; x < histo_size; x++) - { - if (histo[x] <= i) { - printf(" "); - } else { - printf("*"); - } - } - printf("\n"); - } -} \ No newline at end of file diff --git a/unidimensional_array.h b/unidimensional_array.h index b0fb4ff8ef3e97a4ad3630b5d2508a7738305cb4..17cedaa196067e3492ef9c5bd8b0d87019ce25a8 100644 --- a/unidimensional_array.h +++ b/unidimensional_array.h @@ -1,11 +1,11 @@ /* Author : Dario GENGA - * Date : 02.11.2021 - * Description : Manipulate an unidimensional array + * Date : 15.11.2021 + * Description : Manipulate an unidimensional array with dynamic memory allocation */ #ifndef _UNIDIMENSIONAL_ARRAY_H #define _UNIDIMENSIONAL_ARRAY_H #include <stdio.h> -#define PERCENT_OF_MAX_ARRAY_VALUE 10 +#include <stdlib.h> size_t ask_array_size(); @@ -17,13 +17,4 @@ int find_index_highest_value_in_array(int array[], size_t array_size); void swap(int *x, int *y); -int get_average_in_array(int array[], size_t array_size); - -int get_variance_in_array(int array[], size_t array_size); - -void sort_array_asc(int array[], size_t array_size); - -int get_median_value(int array[], size_t array_size); - -void create_histo(int array[], size_t array_size); #endif \ No newline at end of file