diff --git a/.gitignore b/.gitignore index b5e7c1bc91fb3ae8c5a0d3109e5cdae78207e0bd..1467b94352b754f79b68ff8cd4360b326318a474 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/build/*.o -/build/main \ No newline at end of file +/build/* +!/build/.gitkeep \ No newline at end of file diff --git a/Makefile b/Makefile index 13ce114b02e5670350dbfd155b1836dd856b0f17..fc8c7c6240deddcde1d17d373a15d70366eb6e21 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,24 @@ CC = gcc FLAGS = -Wextra -Wall -fsanitize=address -g LIB = -lm EXECUTABLE = build/main +TESTEXE = build/main_100_times SRC = src/ BUILD = build/ +all : $(EXECUTABLE) $(TESTEXE) + $(EXECUTABLE) : $(BUILD)/main.o $(BUILD)/tab_uni_malloc.o $(CC) $^ -o $@ $(FLAGS) $(LIB) +$(TESTEXE) : $(BUILD)/main_100_times.o $(BUILD)/tab_uni_malloc.o + $(CC) $^ -o $@ $(FLAGS) $(LIB) + $(BUILD)/main.o : $(SRC)/main.c $(SRC)/tab_uni_malloc.c $(CC) -c $< $(FLAGS) $(LIB) -o $@ +$(BUILD)/main_100_times.o : $(SRC)/main_100_times.c $(SRC)/tab_uni_malloc.c + $(CC) -c $< $(FLAGS) $(LIB) -o $@ + $(BUILD)/tab_uni_malloc.o : $(SRC)/tab_uni_malloc.c $(CC) -c $^ $(FLAGS) $(LIB) -o $@ @@ -18,5 +27,9 @@ run : $(EXECUTABLE) clear ./$^ -clean : $(EXECUTABLE) $(BUILD)/main.o $(BUILD)/tab_uni_malloc.o +test : $(TESTEXE) + clear + ./$^ + +clean : $(EXECUTABLE) $(TESTEXE) $(BUILD)/main.o $(BUILD)/tab_uni_malloc.o $(BUILD)/main_100_times.o rm $^ diff --git a/header/tab_uni_malloc.h b/header/tab_uni_malloc.h index 3141c930d0ffbc9860b50004420a23f7c228abd6..f7808f06e26f9267f1bea78c8f146fe1791d74c1 100644 --- a/header/tab_uni_malloc.h +++ b/header/tab_uni_malloc.h @@ -22,6 +22,14 @@ */ void PrintTableau(int64_t *tab, int64_t tab_len); +/** + @brief Print the given double table + @param [double] *tab, input table + @param [int] tab_len, table lenght + @return [void] +*/ +void PrintTableauDouble(double *tab, int64_t tab_len); + /** @brief Fill a given table with random numbers @param [int] *tab, input table @@ -89,6 +97,33 @@ void TriInsertionDescent(int64_t *tab, int64_t tab_len); */ int64_t FindHowManySmaller(int64_t *tab, int64_t tab_len, int64_t val); +/** + @brief Do the sum of two table then store it a third one + @param [int] *tab1, input table 1 + @param [int] *tab2, input table 2 + @param [int] *tabOut, Output table + @param [int] tab_len, table lenght + @return [void] +*/ +void SumOfTab(int64_t *tab1, int64_t *tab2, int64_t *tabOut, int64_t tab_len) ; +/** + @brief tab1 times N then store it in tabOut + @param [int] *tab1, input table 1 + @param [int] N + @param [int] *tabOut, Output table + @param [int] tab_len, table lenght + @return [void] +*/ +void TabTimesN(int64_t *tabIn, int64_t N, int64_t *tabOut, int64_t tab_len); + +/** + @brief Convert tabIn To Double + @param [int] *tabIn, input table + @param [double] *tabOut, Output table + @param [int] tab_len, table lenght + @return [void] +*/ +void ConvertTabToDouble(int64_t *tabIn, double *tabOut, int64_t tab_len); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6e6529baf819bf4bc1669342c3bb420e9057616b..ac4d88c1893d8b2361146812130395304a0a6b79 100644 --- a/src/main.c +++ b/src/main.c @@ -53,9 +53,33 @@ int main() printf("Il y a %ld d'élements plus petit que %ld dans le tableau. \n", FindHowManySmaller(tab,size, input_value),input_value); //9. - + int64_t *tab_two = malloc(sizeof(int64_t) * size); + int64_t *tab_three = malloc(sizeof(int64_t) * size); + FillTabWithIndex(tab_two, size); + RandomSwapTab(tab_two, size); + PrintTableau(tab, size); + PrintTableau(tab_two, size); + SumOfTab(tab, tab_two, tab_three, size); + PrintTableau(tab_three, size); + //10. + int64_t *tab_four = malloc(sizeof(int64_t) * size); + printf("\nEntrez une valeur : \n"); + scanf(" %ld", &input_value); + TabTimesN(tab_three, input_value, tab_four, size); + PrintTableau(tab_four, size); + + //11. + double *tab_five = malloc(sizeof(double) * size); + ConvertTabToDouble(tab_four, tab_five, size); + PrintTableauDouble(tab_five, size); + //Free the Malloc free(tab); + free(tab_two); + free(tab_three); + free(tab_four); + free(tab_five); + return 0; } \ No newline at end of file diff --git a/src/main_100_times.c b/src/main_100_times.c new file mode 100644 index 0000000000000000000000000000000000000000..a0b203d831cf3a193c3c216bda7305794b8c117b --- /dev/null +++ b/src/main_100_times.c @@ -0,0 +1,73 @@ +/* + Autheur : Abivarman KANDIAH + Date : 10/11/2021 + Fichier : main.c + Descritpion : Manipulation de tableau avec alloc dynamique +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <math.h> +#include <time.h> +#include "../header/tab_uni_malloc.h" + +int main() +{ + printf("Tableaux unidimensionnels et allocation dynamique de mémoire \n"); + srand(time(NULL)); + for (int64_t i = 0; i < 100; i++) + { + //1. + u_int64_t size = rand() % 1000 + 1; + + // Allocation dynamique + int64_t *tab = malloc(sizeof(int64_t) * size); + + //2. + FillTabWithIndex(tab, size); + RandomSwapTab(tab, size); + + //3. + //PrintTableau(tab, size); + + //4. + RandomSwapTab(tab, size); + //5. + CyclicSwapTab(tab, size, 2); + + //6. + Swap(&tab[FindLowIndex(tab, size)], &tab[size-1]); + + //7. + TriInsertionDescent(tab, size); + + //8. + int64_t input_value = rand() % size; + int64_t numberSmaller = FindHowManySmaller(tab,size, input_value); + + //9. + int64_t *tab_two = malloc(sizeof(int64_t) * size); + int64_t *tab_three = malloc(sizeof(int64_t) * size); + FillTabWithIndex(tab_two, size); + RandomSwapTab(tab_two, size); + SumOfTab(tab, tab_two, tab_three, size); + + //10. + int64_t *tab_four = malloc(sizeof(int64_t) * size); + input_value = rand() % 100 ; + TabTimesN(tab_three, input_value, tab_four, size); + + //11. + double *tab_five = malloc(sizeof(double) * size); + ConvertTabToDouble(tab_four, tab_five, size); + + //Free the Malloc + free(tab); + free(tab_two); + free(tab_three); + free(tab_four); + free(tab_five); + } + return 0; +} \ No newline at end of file diff --git a/src/tab_uni_malloc.c b/src/tab_uni_malloc.c index 8607f46a33c3ce16f4ae3858300bc0f1e7024a99..739217666d357b488c69dbc6498eee2b7588b26f 100644 --- a/src/tab_uni_malloc.c +++ b/src/tab_uni_malloc.c @@ -28,6 +28,22 @@ void PrintTableau(int64_t *tab, int64_t tab_len) printf(" }.\n"); } +/** + @brief Print the given double table + @param [double] *tab, input table + @param [int] tab_len, table lenght + @return [void] +*/ +void PrintTableauDouble(double *tab, int64_t tab_len) +{ + printf("{ "); + for(int64_t i=0;i<tab_len;i++) + { + printf(" %lf", tab[i]); + } + printf(" }.\n"); +} + /** @brief Fill a given table with random numbers @param [int] *tab, input table @@ -80,7 +96,7 @@ void Swap(int64_t *n1, int64_t *n2) void RandomSwapTab(int64_t *tab, int64_t tab_len) { srand(time(NULL)); - for (int64_t i = 0; i < tab_len * 1000; i++) + for (int64_t i = 0; i < tab_len * 10; i++) { Swap(&tab[rand() % tab_len], &tab[rand() % tab_len]); } @@ -164,3 +180,50 @@ int64_t FindHowManySmaller(int64_t *tab, int64_t tab_len, int64_t val) } return compteur; } + +/** + @brief Do the sum of two table then store it a third one + @param [int] *tab1, input table 1 + @param [int] *tab2, input table 2 + @param [int] *tabOut, Output table + @param [int] tab_len, table lenght + @return [void] +*/ +void SumOfTab(int64_t *tab1, int64_t *tab2, int64_t *tabOut, int64_t tab_len) +{ + for (int64_t i = 0; i < tab_len; i++) + { + tabOut[i] = tab1[i] + tab2[i]; + } +} + +/** + @brief tab1 times N then store it in tabOut + @param [int] *tabIn, input table 1 + @param [int] N + @param [int] *tabOut, Output table + @param [int] tab_len, table lenght + @return [void] +*/ +void TabTimesN(int64_t *tabIn, int64_t N, int64_t *tabOut, int64_t tab_len) +{ + for (int64_t i = 0; i < tab_len; i++) + { + tabOut[i] = tabIn[i] * N; + } +} + +/** + @brief Convert tabIn To Double + @param [int] *tabIn, input table + @param [double] *tabOut, Output table + @param [int] tab_len, table lenght + @return [void] +*/ +void ConvertTabToDouble(int64_t *tabIn, double *tabOut, int64_t tab_len) +{ + for (int64_t i = 0; i < tab_len; i++) + { + tabOut[i] = (double)tabIn[i]; + } +} \ No newline at end of file