CONTROLE_DEV51_FAUVET_Quali.../ex1/main.c

123 lines
2.9 KiB
C
Raw Normal View History

2024-12-03 12:30:00 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int racineCarree(int n){
for(int i = 0; i < n; i++) {
if (i*i == n) {
return i;
}
}
return -1;
}
int* RacineCarreeTab(int base_tab[], int tab_length) {
int* computedTab = (int*)malloc(tab_length * sizeof(int));
for (int i = 0; i < tab_length; i++) {
computedTab[i] = racineCarree(base_tab[i]);
}
return computedTab;
}
int* firstSort(int square_root_tab[], int tab_length, int tab_value) {
int* computedTab = (int*)malloc(tab_length * sizeof(int));
for (int i = 0; i < tab_length; i++) {
if(i%2 == 0) {
computedTab[i] = square_root_tab[i];
} else {
computedTab[i] = square_root_tab[i] * tab_value;
}
}
return computedTab;
}
int* secondSort(int square_root_tab[], int tab_length) {
int* computedTab = (int*)malloc(tab_length * sizeof(int));
for (int i = 0; i < tab_length; i++) {
if(i%2 == 0) {
computedTab[i] = racineCarree(square_root_tab[i]);
} else {
int summ = 0;
int* result = RacineCarreeTab(square_root_tab, tab_length);
for(int j = 0; j<tab_length; j++) {
summ = summ + result[j];
}
computedTab[i] = summ * square_root_tab[i];
}
}
return computedTab;
}
int* TriSpecial(int square_root_tab[], int tab_length){
int tab_value = 0, summ_error = 0;
for (int i = 0; i < tab_length; i++) {
tab_value = tab_value + square_root_tab[i];
if(square_root_tab[i] < 0) {
summ_error++;
}
}
if(summ_error % 2 == 0) {
return firstSort(square_root_tab, tab_length, tab_value);
} else {
return secondSort(square_root_tab, tab_length);
}
}
int main(int argc, char* argv[]){
/* Exo 1 A*/
/*int base_number = atoi(argv[1]);
int foundSquareRoot = racineCarree(base_number);
if (foundSquareRoot >= 0 ) {
printf("la racine carré de %d est = %d\n", base_number, foundSquareRoot);
} else {
printf("Aucune racine carrée trouvé pour : %d\n", base_number);
}*/
/* Exo 1 B*/
//int base_tab[5] = {1, 4, 9, 16, 25}; // VALEURS DU TABLEAU SI NON ALEATOIRE
//int tab_length = 5;
int tab_length = atoi(argv[1]); //TAILLE DU TABLEAU
int base_tab[tab_length]; //VALEURS DU TABLEAU
srand(time(NULL));
for(int j = 0; j < tab_length; j++) {
//MODIFIER ICI POUR DES PLUS PETITES VALEURS SI ALEATOIRE
base_tab[j] = rand();
}
int* result = RacineCarreeTab(base_tab, tab_length);
for (int i = 0; i < tab_length; i++) {
printf("%d\t", result[i]);
}
printf("\n");
free(result);
/*Exo 3
//int base_tab[5] = {1, 5, 9, 16, 25}; // VALEURS DU TABLEAU SI NON ALEATOIRE
//int tab_length = 5;
int tab_length = atoi(argv[1]); //TAILLE DU TABLEAU
int base_tab[tab_length]; //VALEURS DU TABLEAU
srand(time(NULL));
for(int j = 0; j < tab_length; j++) {
//MODIFIER ICI POUR DES PLUS PETITES VALEURS SI ALEATOIRE
base_tab[j] = rand();
}
int* sorted_tab = TriSpecial(RacineCarreeTab(base_tab, tab_length), tab_length);
printf("\n");
*/
}