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

Migrate the project to GitLab

The project before the pratical work is imported without any changes.
Add informations about the makefile and configurations files for vscode.
parent e061460a
No related branches found
No related tags found
No related merge requests found
# Created by https://www.toptal.com/developers/gitignore/api/c,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=c,visualstudiocode
### C ###
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide
# Support for Project snippet scope
!.vscode/*.code-snippets
# End of https://www.toptal.com/developers/gitignore/api/c,visualstudiocode
# Custom gitignore for project
histo
.vscode/launch.json
.vscode/tasks.json
\ No newline at end of file
# progseq-dynamic_memory_allocation
# Tableaux unidimensionnels et allocation dynamique de mémoire
6e travail pratique du cours de programmation séquentielle, 1er année (2021-2022).
- **Class** : Programmation séquentielle en C
- **Creation date** : 9 novembre 2021
- **Description** : 6e travail pratique
Tableaux unidimensionnels et allocation dynamique de mémoire.
\ No newline at end of file
## Makefile configuration
### Compile the project
> `make`
Use this command to compile the project.
### Clean 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"
}
```
histo.c 0 → 100644
#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];
// Fill the array with random values
fill_array_with_random_values(array, array_size);
// Find the lowest value in the array
int lowest_value = find_lowest_value_in_array(array, array_size);
printf("Lowest value : %d\n", lowest_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]);
// 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);
return 0;
}
\ No newline at end of file
makefile 0 → 100644
LIB=-lm
CC=gcc -Wall -Wextra
histo:unidimensional_array.o histo.o
gcc $^ -o $@ $(LIB)
unidimensional_array.o: unidimensional_array.c unidimensional_array.h
$(CC) -c $< $(LIB)
histo.o: histo.c
$(CC) -c $< $(LIB)
clean:
rm -f *.o histo
\ No newline at end of file
/* Author : Dario GENGA
* Date : 12.10.2021
* Description : Manipulate an unidimensional array
*/
#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;
printf("Size of the array : \n");
scanf("%ld", &array_size);
return 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;
srand(time(0));
for (size_t i = 0; i < array_size; i++)
{
int random_value = (rand() % (max_value + 1));
array[i] = random_value;
}
}
int find_lowest_value_in_array(int array[], size_t array_size) {
int lowest_value;
for (size_t i = 0; i < array_size; i++) {
if (i == 0) {
lowest_value = array[i];
} else if (array[i] < lowest_value) {
lowest_value = array[i];
}
}
return lowest_value;
}
int find_index_highest_value_in_array(int array[], size_t array_size) {
int highest_value;
size_t index_highest_value = 0;
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;
}
}
return index_highest_value;
}
void swap(int *x, int *y)
{
int tmp = *x;
*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
/* Author : Dario GENGA
* Date : 02.11.2021
* Description : Manipulate an unidimensional array
*/
#ifndef _UNIDIMENSIONAL_ARRAY_H
#define _UNIDIMENSIONAL_ARRAY_H
#include <stdio.h>
#define PERCENT_OF_MAX_ARRAY_VALUE 10
size_t ask_array_size();
void fill_array_with_random_values(int array[], size_t array_size);
int find_lowest_value_in_array(int array[], size_t array_size);
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment