ajout controle
This commit is contained in:
92
Exercice3.c
Normal file
92
Exercice3.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
int racineCarree(int n){
|
||||
if(n < 0){
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(int i = 0; i <= n; i++){
|
||||
if((i*i) == n){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int somme(int tab[], int taille) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < taille; i++) {
|
||||
sum += tab[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
int sommeRacines(int tab[], int taille) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < taille; i++) {
|
||||
int r = racineCarree(tab[i]);
|
||||
if (r != -1) {
|
||||
sum += r;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void TriSpecial(int tab[], int taille, int resultat[]){
|
||||
int racinesNonEntieres = 0;
|
||||
|
||||
for (int i = 0; i < taille; i++) {
|
||||
if (racineCarree(tab[i]) == -1) {
|
||||
racinesNonEntieres++;
|
||||
}
|
||||
}
|
||||
|
||||
if (racinesNonEntieres % 2 == 0) {
|
||||
int sum = somme(tab, taille);
|
||||
for (int i = 0; i < taille; i++) {
|
||||
if (i % 2 == 0) {
|
||||
resultat[i] = tab[i];
|
||||
} else {
|
||||
resultat[i] = sum * tab[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int sRacines = sommeRacines(tab, taille);
|
||||
for (int i = 0; i < taille; i++) {
|
||||
if (i % 2 == 0) {
|
||||
resultat[i] = racineCarree(tab[i]);
|
||||
} else {
|
||||
resultat[i] = sRacines * tab[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(void) {
|
||||
int Ex3T1[] = {3, 5, 25, 16};
|
||||
int Ex3T2[] = {36, 9, 100, 2, 3, 7};
|
||||
int Ex3Taille = 4;
|
||||
int Ex3Taille2 = 6;
|
||||
|
||||
int resultat1[Ex3Taille];
|
||||
int resultat2[Ex3Taille2];
|
||||
|
||||
TriSpecial(Ex3T1, Ex3Taille, resultat1);
|
||||
for(int i = 0; i < Ex3Taille; i++){
|
||||
printf("%d ", resultat1[i]);
|
||||
}
|
||||
printf("\n");
|
||||
TriSpecial(Ex3T2, Ex3Taille2, resultat2);
|
||||
for(int i = 0; i < Ex3Taille2; i++){
|
||||
printf("%d ", resultat2[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
Reference in New Issue
Block a user