Rendu
This commit is contained in:
BIN
Exercice3/a.out
Executable file
BIN
Exercice3/a.out
Executable file
Binary file not shown.
151
Exercice3/trispecial.c
Normal file
151
Exercice3/trispecial.c
Normal file
@@ -0,0 +1,151 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int racineCarree(int nombre){
|
||||
|
||||
if (nombre < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( nombre == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i <= nombre; i++) {
|
||||
if( i * i == nombre){
|
||||
return i;
|
||||
}
|
||||
if ( i * i > nombre ) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
int racinenonentierre(int *tab, int taille) {
|
||||
|
||||
int compte;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < taille; i++) {
|
||||
if (racineCarree(tab[i]) == -1) {
|
||||
compte++;
|
||||
}
|
||||
}
|
||||
|
||||
return compte;
|
||||
|
||||
}
|
||||
|
||||
int sommetableau(int *tab, int taille) {
|
||||
|
||||
int somme = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < taille; i++){
|
||||
somme += tab[i];
|
||||
}
|
||||
|
||||
return somme;
|
||||
|
||||
}
|
||||
|
||||
int sommeracinecarree( int *tab, int taille) {
|
||||
|
||||
int somme = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < taille; i++){
|
||||
|
||||
int racine = racineCarree(tab[i]);
|
||||
|
||||
if (racine != -1) {
|
||||
somme += racine;
|
||||
}
|
||||
}
|
||||
|
||||
return somme;
|
||||
}
|
||||
|
||||
|
||||
void TriSpecial(int *tab, int *resultat, int taille) {
|
||||
|
||||
int nbRacinesNonEntieres = racinenonentierre(tab, taille);
|
||||
|
||||
int estPair = (nbRacinesNonEntieres % 2 == 0);
|
||||
|
||||
if (estPair) {
|
||||
|
||||
int somme = sommetableau(tab, taille);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < taille; i++) {
|
||||
|
||||
if (i % 2 == 0) {
|
||||
resultat[i] = tab[i];
|
||||
} else {
|
||||
resultat[i] = somme * tab[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
int sommeRacines = sommeracinecarree(tab, taille);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < taille; i++) {
|
||||
|
||||
if (i % 2 == 0) {
|
||||
resultat[i] = racineCarree(tab[i]);
|
||||
|
||||
} else {
|
||||
resultat[i] = sommeRacines * tab[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void afficherTableau(int *tab, int taille) {
|
||||
printf("[");
|
||||
int i;
|
||||
|
||||
for (i = 0; i < taille; i++) {
|
||||
printf("%d", tab[i]);
|
||||
|
||||
if (i < taille - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
|
||||
int tab1[] = {3, 5, 25, 16};
|
||||
int taille1 = 4;
|
||||
int resultat1[4];
|
||||
|
||||
printf("Entrée : ");
|
||||
afficherTableau(tab1, taille1);
|
||||
printf("\n");
|
||||
|
||||
int nbNonEntieres1 = racinenonentierre(tab1, taille1);
|
||||
|
||||
if (nbNonEntieres1 % 2 == 0) {
|
||||
printf("Somme du tableau : %d\n", sommetableau(tab1, taille1));
|
||||
} else {
|
||||
printf("Somme des racines : %d\n", sommeracinecarree(tab1, taille1));
|
||||
}
|
||||
|
||||
TriSpecial(tab1, resultat1, taille1);
|
||||
|
||||
printf("Résultat : ");
|
||||
afficherTableau(resultat1, taille1);
|
||||
printf("\n");
|
||||
}
|
Reference in New Issue
Block a user