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

Removed not required code for specification

This concern the display of the array, some calculation and also
removed the configuration for Visual Studio Code who is no longer used.
parent e8f13815
No related branches found
No related tags found
No related merge requests found
...@@ -15,72 +15,3 @@ Use this command to compile the project. ...@@ -15,72 +15,3 @@ Use this command to compile the project.
> `make clean` > `make clean`
Use this command to clean the project. 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"
}
```
/* Author : Dario GENGA
* Date : 15.11.2021
* Description : Manipulate an unidimensional array with dynamic memory allocation
*/
#include "unidimensional_array.h" #include "unidimensional_array.h"
int main() { int main() {
// Ask the user the size of the array // Ask the user the size of the array
size_t array_size = ask_array_size(); 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 the array with random values
fill_array_with_random_values(array, array_size); fill_array_with_random_values(array, array_size);
...@@ -17,23 +20,7 @@ int main() { ...@@ -17,23 +20,7 @@ int main() {
size_t index_highest_value = find_index_highest_value_in_array(array, array_size); size_t index_highest_value = find_index_highest_value_in_array(array, array_size);
swap(&array[index_highest_value], &array[array_size - 1]); swap(&array[index_highest_value], &array[array_size - 1]);
// Calculate the average value of the array free(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);
return 0; return 0;
} }
\ No newline at end of file
LIB=-lm LIB=-lm
CC=gcc -Wall -Wextra CC=gcc -Wall -Wextra -g
histo:unidimensional_array.o histo.o histo:unidimensional_array.o histo.o
gcc $^ -o $@ $(LIB) gcc $^ -fsanitize=address -o $@ $(LIB)
unidimensional_array.o: unidimensional_array.c unidimensional_array.h unidimensional_array.o: unidimensional_array.c unidimensional_array.h
$(CC) -c $< $(LIB) $(CC) -c $< $(LIB)
......
/* Author : Dario GENGA /* Author : Dario GENGA
* Date : 12.10.2021 * Date : 15.11.2021
* Description : Manipulate an unidimensional array * Description : Manipulate an unidimensional array with dynamic memory allocation
*/ */
#include "unidimensional_array.h" #include "unidimensional_array.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h> #include <time.h>
#include <math.h>
#define PERCENT_OF_MAX_ARRAY_VALUE 10
size_t ask_array_size() { size_t ask_array_size() {
size_t array_size = 0; size_t array_size = 0;
...@@ -18,7 +15,7 @@ size_t ask_array_size() { ...@@ -18,7 +15,7 @@ size_t ask_array_size() {
} }
void fill_array_with_random_values(int array[], size_t 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)); srand(time(0));
...@@ -63,113 +60,3 @@ void swap(int *x, int *y) ...@@ -63,113 +60,3 @@ void swap(int *x, int *y)
*x = *y; *x = *y;
*y = tmp; *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
/* Author : Dario GENGA /* Author : Dario GENGA
* Date : 02.11.2021 * Date : 15.11.2021
* Description : Manipulate an unidimensional array * Description : Manipulate an unidimensional array with dynamic memory allocation
*/ */
#ifndef _UNIDIMENSIONAL_ARRAY_H #ifndef _UNIDIMENSIONAL_ARRAY_H
#define _UNIDIMENSIONAL_ARRAY_H #define _UNIDIMENSIONAL_ARRAY_H
#include <stdio.h> #include <stdio.h>
#define PERCENT_OF_MAX_ARRAY_VALUE 10 #include <stdlib.h>
size_t ask_array_size(); size_t ask_array_size();
...@@ -17,13 +17,4 @@ int find_index_highest_value_in_array(int array[], size_t 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); 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 #endif
\ 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