Beruflich Dokumente
Kultur Dokumente
Corrigés des TP en C
Table des matières
Algorithmique Avancée et Programmation - Corrigés des TP en C............................................... 1
Série d'exercices n°1 .............................................................................................................................. 1
Exercice 2 ....................................................................................................................................... 1
Exercice 3 ....................................................................................................................................... 2
Exercice 4 ....................................................................................................................................... 3
Exercice 5 ....................................................................................................................................... 4
Exercice 6 ....................................................................................................................................... 5
Exercice 7 ....................................................................................................................................... 6
Exercice 8 ....................................................................................................................................... 7
Série d'exercices n°2 .............................................................................................................................. 8
Exercice 1 ....................................................................................................................................... 8
Exercice 2 ..................................................................................................................................... 12
Exercice 3 ..................................................................................................................................... 14
Exercice 4 ..................................................................................................................................... 18
Exercice 5 ..................................................................................................................................... 19
Série d'exercices n°3 ............................................................................................................................ 21
Exercice 1 ..................................................................................................................................... 21
Exercice 2 ..................................................................................................................................... 23
Exercice 3 ..................................................................................................................................... 24
#include <stdio.h>
int main() {
int a;
char p[20],n[20];
printf("Quelle est ton année d'étude ?");
scanf("%d",&a);
scanf("%s %s",&p,&n);
Exercice 3
#include <stdio.h>
int main() {
scanf("%d", &a1);
scanf("%d", &a2);
scanf("%d", &a3);
max = a1;
mil = a2;
} else {
mil = a1;
max = a2;
min = a3;
} else {
int perm;
perm = mil;
mil = a3;
min = perm;
} else {
min = mil;
mil = max;
max = a3;
printf("Les 3 nombres dans l'ordre croissant sont : %d %d %d", min, mil, max);
Exercice 4
#include <stdio.h>
#include <stdlib.h>
int main() {
int nbVal = 0;
scanf("%f", ¬e);
nbVal++;
max = note;
min = note;
} else {
printf("La note saisie n'a pas été prise en compte (hors de [0;20])\n");
scanf("%f", ¬e); // placé à la fin de la boucle pour ne pas avoir à gérer -1 comme une note
printf("%d notes ont été entrées. La note la plus élevée (%f) et la note la plus basse (%f) ont été
retirées.\n", nbVal, max, min);
Exercice 5
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d",&taille);
printf("*");
printf("\n");
Exercice 6
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d",&taille);
printf(" ");
printf("*");
}
printf("\n");
Exercice 7
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int n, i, est_premier;
while (nvNbre=='O') {
scanf("%d",&n);
est_premier = 1;
if ((n % i) == 0)
est_premier = 0;
break;
}
if (est_premier==1) {
fflush(stdin); // à utiliser avant de scanner un %c au clavier, sans quoi c'est le passage à la ligne
précédent qui sera lu
scanf("%c",&nvNbre);
Exercice 8
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
double epsilon=1e-15, x;
printf("x=? ");
scanf("%lf",&x);
double sinx=x,ajout=x;
int nbpassages=1;
while (fabs(ajout)>=epsilon){
sinx=sinx+pow(-1,nbpassages)*ajout;
nbpassages++;
}
printf("On trouve : sin(x)=%.17g\nLa bibliothèque : sin(x)=%.17g\n",sinx,sin(x));
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
printf("Tableau : [ ");
printf("%d ",tab[i]);
printf("]\n");
int i=0;
if(tab[i]%2==0){
printf("%d ",tab[i]);
i++;
printf("]\n");
}
int i=0,s=0;
s+=tab[i];
i++;
return (float)s/taille;
int min=tab[0];
if (tab[i]<min){
min=tab[i];
return min;
// Effacement du tableau
int tmp;
tmp=tab[taille-i-1];
tab[taille-i-1]=tab[i];
tab[i]=tmp;
tab2[taille-i-1]=tab[i];
int* tab2=malloc(taille*sizeof(int));
tab2[taille-i-1]=tab[i];
return tab2;
int main() {
int taille=10;
int tab[taille];
tab[i]=rand()%11;
}
afficher(tab,taille);
printf("\n");
afficherPair(tab,taille);
printf("\n");
float m=calculerMoyenne(tab,taille);
printf("Moyenne : %f\n",m);
int tabb[taille];
inverserCopieTableauV1(tab,tabb,taille);
printf("Inversion v1 - ");
afficher(tabb,taille);
printf("\n");
int* tabb2;
tabb2=inverserCopieTableauV2(tab,taille);
printf("Inversion v2 - ");
afficher(tabb2,taille);
printf("\n");
free(tabb2);
inverserTableau(tab,taille);
afficher(tab,taille);
}
Exercice 2
#include <stdio.h>
#include <stdlib.h>
char abc[]="abcdefghijklmnopqrstuvwxyz";
char abcdecale[]="defghijklmnopqrstuvwxyzabc";
char *codee;
for(int i=0;i<taille;i++){
for(int j=0;j<26;j++){
if (phrase[i]==abc[j]){
codee[i]=abcdecale[j];
break;
else
codee[i]=phrase[i];
return codee;
char abcdecale[]="abcdefghijklmnopqrstuvwxyz";
char abc[]="defghijklmnopqrstuvwxyzabc";
char *codee;
codee = malloc(taille * sizeof(char));
for(int i=0;i<taille;i++){
for(int j=0;j<26;j++){
if (phrase[i]==abc[j]){
codee[i]=abcdecale[j];
break;
else
codee[i]=phrase[i];
return codee;
for(int i=0;i<taille;i++){
else
codee[i]=phrase[i];
return codee;
}
char* dechiffreCesarBis(char* phrase, int deplacement){
for(int i=0;i<taille;i++){
else
codee[i]=phrase[i];
return codee;
int main() {
printf(chiffreCesar(phrase,taille));
printf("\n%s",dechiffreCesar(phrase,taille));
printf("\n%s",chiffreCesarBis(phrase,7));
printf("\n%s",dechiffreCesarBis(phrase,5));
Exercice 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void afficheMatrice(int** matrice, int taille1, int taille2){
printf("%d ",matrice[i][j]);
printf("\n");
void permutecolonnes(int** matrice, int taille1, int taille2, int colonne1, int colonne2){
int tmp;
tmp=matrice[i][colonne2];
matrice[i][colonne2]=matrice[i][colonne1];
matrice[i][colonne1]=tmp;
return mat2;
int main () {
srand(time(NULL));
int **mat;
mat[i][j]=0;
if (i==j){
mat[i][j]=1;
afficheMatrice(mat,taille,taille);
free(mat);
int taille2;
scanf("%d",&taille);
scanf("%d",&taille2);
int **mat2;
mat2[i] = malloc(taille2 * sizeof(int));//dans notre représentation, mat2[i] est une ligne, c'est un
tableau de taille2 entiers.
mat2[i][j]=rand()%10;
afficheMatrice(mat2,taille,taille2);
printf("Permutation\n");
permutecolonnes(mat2,taille,taille2,0,1);
afficheMatrice(mat2,taille,taille2);
printf("Transposition\n");
free(mat2[i]);
}
free(mat2);
Exercice 4
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int tab[100];
tab[k] = rand()%2;
if (tab[k] == 0) {
longueur++;
longueurmax = longueur;
indicemax = k;
} else longueur = 0;
printf("Tableau : [ ");
printf("%d ",tab[i]);
}
printf("]\n");
Exercice 5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int indicemax=0;
indicemax = i;
return indicemax;
for(int i=0;i<taille;i++){
}
}
int main() {
srand(time(NULL));
int taille=11;
int frequence[taille];
frequence[i]=0;
de1 = rand()%6 + 1;
de2 = rand()%6 + 1;
frequence[de1 + de2 - 2]++; //quand somme des dés = 2 incrémente en position 0, somme = 3
incrémente en position 1...
int tabtmp[taille];
for (i=0;i<taille;i++){
tabtmp[i]=frequence[i];
tritab(frequence, taille); // frequence est trié, tabtmp est une copie non triée de frequence
if(tabtmp[j]==frequence[i]){
#include <stdio.h>
#include <stdlib.h>
struct complexe {
float reel;
float imag;
};
scanf("%f+%fi",&result.reel,&result.imag);
return result;
}
void printfComplexe(struct complexe nbc) {
printf("%f + %f i",nbc.reel,nbc.imag);
result.reel=nbc1.reel+nbc2.reel;
result.imag=nbc1.imag+nbc2.imag;
return result;
result.reel=nbc1.reel*nbc2.reel-nbc1.imag*nbc2.imag;
result.imag=nbc1.imag*nbc2.reel+nbc1.reel*nbc2.imag;
return result;
int main() {
nbcomp1=scanfComplexe();
nbcomp2=scanfComplexe();
printf("Somme : ");
printfComplexe(sommeComplexe(nbcomp1,nbcomp2));
printf("\nProduit : ");
printfComplexe(produitComplexe(nbcomp1,nbcomp2));
Exercice 2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double carre(double x) {
return x*x;
double x, integ;
for(x=min;x<=max;x+=pas){
integ+=((*f)(x)+(*f)(x+pas))*pas/2;
return integ;
int main() {
double (*f)(double)=sin;
f=carre;
Exercice 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void creation() {
char nomeleve[20];
char prenomeleve[20];
char nomfichier[20];
scanf("%s", &nomfichier);
while (1) {
fclose(fichier);
}
void recopiefichier(FILE* fichier1, FILE* fichier2) {//copie fichier1 (à partir de la position courante)
dans fichier2 (à partir de la position courante)
int nbcopies = 0;
char car;
while (1) {
car = fgetc(fichier1);
nbcopies++;
void remplissage() {
char nomeleve[20];
char prenomeleve[20];
int note;
char nomfichier[20];
scanf("%s", &nomfichier);
if (fichier == NULL) {
printf("Fichier inexistant\n");
return; // sortie de la fonction
while (fgetc(fichier) != '\n'); // on passe "Notes : " et les notes éventuellement déjà rentrées
fseek(fichier, -1, SEEK_CUR); // on remonte le "\n" pour se placer à la fin de la ligne des notes
recopiefichier(fichier, fichiercopie);
while (1) {
scanf("%d", ¬e);
fprintf(fichier, "\r\n");
// récupération de la sauvegarde
recopiefichier(fichiercopie, fichier);
fclose(fichier);
fclose(fichiercopie);
remove("fichiertmp.txt");
void affichage() {
char nomeleve[20] ;
char prenomeleve[20] ;
char nomlu[20] ;
char prenomlu[20] ;
char* notes;
int flag = 0;
char nomfichier[20];
scanf("%s", &nomfichier);
if (fichier == NULL) {
printf("Fichier inexistant\n");
fseek(fichier, 2, SEEK_CUR);
break;
if (flag == 0) {
fclose(fichier);
void moyenne() {
char nomfichier[20];
scanf("%s", &nomfichier);
if (fichier == NULL) {
printf("Fichier inexistant\n");
nbnotes = 0;
nbnotes++;
fclose(fichier);
int main() {
int choixmenu = 1;
while (choixmenu != 0) {
scanf("%d", &choixmenu);
switch (choixmenu) {
case 0:
break;
case 1: creation();
break;
case 2: remplissage();
break;
case 3: affichage();
break;
case 4: moyenne();
break;
break;