TP15 Fonctions

This commit is contained in:
HORVILLE 2021-10-18 16:28:46 +02:00
parent ee2b132b8e
commit 58b961133b
5 changed files with 149 additions and 0 deletions

40
APL1.1/TP15/decoupage.c Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
void drawTriangle() {
for (int i = 0; i <= SIZE; i++) {
for (int i2 = SIZE-i; i2 > 0; i2--) printf(" ");
for (int i2 = 0; i2 < i*2-1; i2++) printf("* ");
printf("\n");
}
}
void drawSquare() {
for (int x = 0; x < SIZE; x++) {
for (int y = 0; y < SIZE; y++) {
printf("* ");
}
printf("\n");
}
}
int showMenu() {
int choice;
puts("Que voulez vous dessiner ?");
puts("1. Carré");
puts("2. Triangle");
scanf("%d", &choice);
return choice;
}
int main(int argc, char * argv[]) {
int choice = showMenu();
if(choice == 1) drawSquare();
else drawTriangle();
return EXIT_SUCCESS;
}

17
APL1.1/TP15/echange.c Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
#include<stdlib.h>
void swap(int* x, int* y) {
int z = *x;
*x = *y;
*y = z;
}
int main(int argc, char * argv[]) {
int x = 10, y = 3;
printf("x = %d; y = %d\n", x, y);
swap(&x, &y);
printf("x = %d; y = %d\n", x, y);
return EXIT_SUCCESS;
}

54
APL1.1/TP15/miroir.c Normal file
View File

@ -0,0 +1,54 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 10
void filltable(int table[]) {
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
table[i] = rand() % 100;
srand(rand());
}
}
void showtable(int table[]) {
for (int i = 0; i < SIZE; i++) printf("+----");
printf("+\n");
for (int i = 0; i < SIZE; i++) printf("| %2d ", table[i]);
printf("|\n");
for (int i = 0; i < SIZE; i++) printf("+----");
printf("+\n");
}
/* Sans la fonction de l'exo 4
void reversetable(int table[SIZE]) {
int normaltable[SIZE];
for (int i = 0; i < SIZE; i++) normaltable[i] = table[i];
for (int i = 0; i < SIZE; i++) table[i] = normaltable[SIZE-1-i];
} */
//Avec la fonction de l'exo 4
void swap(int* x, int* y) {
int z = *x;
*x = *y;
*y = z;
}
void reversetable(int table[SIZE]) {
for (int i = 0; i < SIZE/2; i++) swap(&table[i], &table[SIZE-i-1]);
}
int main(int argc, char * argv[]) {
int table[SIZE];
filltable(table);
showtable(table);
reversetable(table);
showtable(table);
return EXIT_SUCCESS;
}

23
APL1.1/TP15/produit.c Normal file
View File

@ -0,0 +1,23 @@
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char * argv[]) {
long val = 1;
int success = 0;
for (int i = 1; i < argc; i++) {
char * pointer;
long valeur = strtol(argv[i], &pointer, 10);
if (!(pointer == argv[i])) {
val *= valeur;
success++;
}
}
if (argc > 1 && success > 0) printf("Valeur : %ld\n", val);
else puts("Veuillez indiquer des entiers valides lors de l'exécution.");
return EXIT_SUCCESS;
}

15
APL1.1/TP15/zero.c Normal file
View File

@ -0,0 +1,15 @@
#include<stdio.h>
#include<stdlib.h>
void zero(double* a) {
*a = 0.0;
}
int main(int argc, char * argv[]) {
double x=37.5;
printf("avant : %f\n", x);
zero(&x);
printf("après : %f\n", x);
return EXIT_SUCCESS;
}