Skip to content
Snippets Groups Projects
Commit 85cb4e90 authored by abivarma.kandiah's avatar abivarma.kandiah
Browse files

Finished

parent 87410466
No related branches found
No related tags found
No related merge requests found
/build/*.o /build/*
/build/main !/build/.gitkeep
\ No newline at end of file \ No newline at end of file
...@@ -2,15 +2,24 @@ CC = gcc ...@@ -2,15 +2,24 @@ CC = gcc
FLAGS = -Wextra -Wall -fsanitize=address -g FLAGS = -Wextra -Wall -fsanitize=address -g
LIB = -lm LIB = -lm
EXECUTABLE = build/main EXECUTABLE = build/main
TESTEXE = build/main_100_times
SRC = src/ SRC = src/
BUILD = build/ BUILD = build/
all : $(EXECUTABLE) $(TESTEXE)
$(EXECUTABLE) : $(BUILD)/main.o $(BUILD)/tab_uni_malloc.o $(EXECUTABLE) : $(BUILD)/main.o $(BUILD)/tab_uni_malloc.o
$(CC) $^ -o $@ $(FLAGS) $(LIB) $(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 $(BUILD)/main.o : $(SRC)/main.c $(SRC)/tab_uni_malloc.c
$(CC) -c $< $(FLAGS) $(LIB) -o $@ $(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 $(BUILD)/tab_uni_malloc.o : $(SRC)/tab_uni_malloc.c
$(CC) -c $^ $(FLAGS) $(LIB) -o $@ $(CC) -c $^ $(FLAGS) $(LIB) -o $@
...@@ -18,5 +27,9 @@ run : $(EXECUTABLE) ...@@ -18,5 +27,9 @@ run : $(EXECUTABLE)
clear 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 $^ rm $^
...@@ -22,6 +22,14 @@ ...@@ -22,6 +22,14 @@
*/ */
void PrintTableau(int64_t *tab, int64_t tab_len); 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 @brief Fill a given table with random numbers
@param [int] *tab, input table @param [int] *tab, input table
...@@ -89,6 +97,33 @@ void TriInsertionDescent(int64_t *tab, int64_t tab_len); ...@@ -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); 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 #endif
\ No newline at end of file
...@@ -53,9 +53,33 @@ int main() ...@@ -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); printf("Il y a %ld d'élements plus petit que %ld dans le tableau. \n", FindHowManySmaller(tab,size, input_value),input_value);
//9. //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 the Malloc
free(tab); free(tab);
free(tab_two);
free(tab_three);
free(tab_four);
free(tab_five);
return 0; return 0;
} }
\ No newline at end of file
/*
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
...@@ -28,6 +28,22 @@ void PrintTableau(int64_t *tab, int64_t tab_len) ...@@ -28,6 +28,22 @@ void PrintTableau(int64_t *tab, int64_t tab_len)
printf(" }.\n"); 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 @brief Fill a given table with random numbers
@param [int] *tab, input table @param [int] *tab, input table
...@@ -80,7 +96,7 @@ void Swap(int64_t *n1, int64_t *n2) ...@@ -80,7 +96,7 @@ void Swap(int64_t *n1, int64_t *n2)
void RandomSwapTab(int64_t *tab, int64_t tab_len) void RandomSwapTab(int64_t *tab, int64_t tab_len)
{ {
srand(time(NULL)); 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]); 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) ...@@ -164,3 +180,50 @@ int64_t FindHowManySmaller(int64_t *tab, int64_t tab_len, int64_t val)
} }
return compteur; 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment