188 lines
2.9 KiB
Plaintext
188 lines
2.9 KiB
Plaintext
|
------ TP16 : Fonctions -----
|
||
|
|
||
|
1.
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
|
||
|
|
||
|
void triangle(int hauteur) {
|
||
|
int i;
|
||
|
int j;
|
||
|
for (i = 0; i != hauteur; i++) {
|
||
|
for (j = 0; j != i+1; j++) {
|
||
|
putchar('*');
|
||
|
}
|
||
|
putchar('\n');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void carre(int hauteur) {
|
||
|
int i;
|
||
|
int j;
|
||
|
for (i = 0; i != hauteur; i++) {
|
||
|
putchar('*');
|
||
|
}
|
||
|
putchar('\n');
|
||
|
for (j = 0; j != hauteur-2; j++) {
|
||
|
for (i = 0; i != hauteur; i++) {
|
||
|
if (i == 0 || i == hauteur-1) {
|
||
|
putchar('*');
|
||
|
}
|
||
|
else {
|
||
|
putchar(' ');
|
||
|
}
|
||
|
}
|
||
|
putchar('\n');
|
||
|
}
|
||
|
for (i = 0; i != hauteur; i++) {
|
||
|
putchar('*');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void menu(void) {
|
||
|
char selection;
|
||
|
int hauteur;
|
||
|
printf("\n__________\n t) Triangle\n c) Carré\n q) Quitter\nVotre choix ? ");
|
||
|
scanf("%c", &selection);
|
||
|
getchar();
|
||
|
if (selection == 'q') {
|
||
|
return;
|
||
|
}
|
||
|
printf("\nHauteur ?");
|
||
|
scanf("%d", &hauteur);
|
||
|
getchar();
|
||
|
if (selection == 't') {
|
||
|
triangle(hauteur);
|
||
|
menu();
|
||
|
return;
|
||
|
}
|
||
|
if (selection == 'c') {
|
||
|
carre(hauteur);
|
||
|
menu();
|
||
|
}
|
||
|
else {
|
||
|
menu();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(void) {
|
||
|
menu();
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
2.
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
# include <time.h>
|
||
|
# define TAILLE_TABLEAU 10
|
||
|
|
||
|
|
||
|
void remplissage(int* tab, int tab_len) {
|
||
|
int i;
|
||
|
for (i = 0; i != tab_len; i++) {
|
||
|
tab[i] = (rand() % 100) - 50;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void affichage(int* tab, int tab_len) {
|
||
|
int i;
|
||
|
for (i = 0; i != tab_len; i++) {
|
||
|
printf("+-----");
|
||
|
}
|
||
|
printf("+\n");
|
||
|
for (i = 0; i != tab_len; i++) {
|
||
|
if (tab[i] < 10 && tab[i] >= 0) {
|
||
|
printf("| %d ", tab[i]);
|
||
|
}
|
||
|
else if (tab[i] < -9) {
|
||
|
printf("| %d ", tab[i]);
|
||
|
}
|
||
|
else {
|
||
|
printf("| %d ", tab[i]);
|
||
|
}
|
||
|
}
|
||
|
printf("|\n");
|
||
|
for (i = 0; i != tab_len; i++) {
|
||
|
printf("+-----");
|
||
|
}
|
||
|
printf("+\n");
|
||
|
}
|
||
|
|
||
|
|
||
|
void remplissage_inverse(int* tab, int* tab_inverse, int tab_len) {
|
||
|
int i;
|
||
|
for (i = 0; i != tab_len; i++) {
|
||
|
tab_inverse[i] = tab[tab_len-i-1];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(void){
|
||
|
int tab[TAILLE_TABLEAU];
|
||
|
int tab_inverse[TAILLE_TABLEAU];
|
||
|
srand(time(NULL));
|
||
|
remplissage(tab, TAILLE_TABLEAU);
|
||
|
affichage(tab, TAILLE_TABLEAU);
|
||
|
remplissage_inverse(tab, tab_inverse, TAILLE_TABLEAU);
|
||
|
affichage(tab_inverse, TAILLE_TABLEAU);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
3. La fonction ne fait pas son travail car elle ne modifie la variable a que dans le bloc
|
||
|
de la fonction. Il faut donc la renvoyer et assigner à la variable x la valeur renvoyée
|
||
|
par la fonction zero() :
|
||
|
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
|
||
|
|
||
|
double zero(double a) {
|
||
|
a = 0.0;
|
||
|
return a;
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(void) {
|
||
|
double x=37.5;
|
||
|
printf("avant : %f\n", x);
|
||
|
x = zero(x);
|
||
|
printf("après : %f\n", x);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
|
||
|
4.
|
||
|
|
||
|
PAS FINI
|
||
|
|
||
|
# include <stdio.h>
|
||
|
# include <stdlib.h>
|
||
|
|
||
|
|
||
|
void inverse_variables(int a, int b) {
|
||
|
int temp;
|
||
|
temp = a;
|
||
|
a = &b;
|
||
|
b = &temp;
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(void) {
|
||
|
int x, y;
|
||
|
x = 12;
|
||
|
y = 5;
|
||
|
printf("avant : x = %d y = %d\n", x, y);
|
||
|
inverse_variables(x, y);
|
||
|
printf("avant : x = %d y = %d\n", x, y);
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|