TP11 Tableaux

This commit is contained in:
HORVILLE 2021-10-05 16:17:21 +02:00
parent abf065d231
commit 463951927a
6 changed files with 259 additions and 0 deletions

41
APL1.1/TP11/circulation.c Normal file
View File

@ -0,0 +1,41 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define DECALAGE 4
void showtable(int tablesize, int* table) {
for (int i = 0; i < tablesize; i++) printf("+-----");
printf("+\n");
for (int i = 0; i < tablesize; i++) printf("| %3d ", table[i]);
printf("|\n");
for (int i = 0; i < tablesize; i++) printf("+-----");
printf("+\n");
}
int main(int argc, char * argv[]) {
int tab[10] = {};
srand(time(NULL));
int rand_v = rand();
for (int i = 0; i < 10; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
}
showtable(10, tab);
int tab2[10] = {};
for (int i = 0; i < 10; i++) {
int i2 = (i+DECALAGE) % 10;
tab2[i2] = tab[i];
}
showtable(10, tab2);
return EXIT_SUCCESS;
}

45
APL1.1/TP11/filtre.c Normal file
View File

@ -0,0 +1,45 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void showtable(int tablesize, int* table) {
for (int i = 0; i < tablesize; i++) printf("+-----");
printf("+\n");
for (int i = 0; i < tablesize; i++) printf("| %3d ", table[i]);
printf("|\n");
for (int i = 0; i < tablesize; i++) printf("+-----");
printf("+\n");
}
void showtable2(int tablesize, int* table) {
for (int i = 0; i < tablesize; i++) if (table[i] >= 0) printf("+-----");
printf("+\n");
for (int i = 0; i < tablesize; i++) if (table[i] >= 0) printf("| %3d ", table[i]);
printf("|\n");
for (int i = 0; i < tablesize; i++) if (table[i] >= 0) printf("+-----");
printf("+\n");
}
int main(int argc, char * argv[]) {
int tab[10] = {};
srand(time(NULL));
int rand_v = rand();
for (int i = 0; i < 10; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
}
showtable(10, tab);
printf("\n\n");
showtable2(10, tab);
return EXIT_SUCCESS;
}

49
APL1.1/TP11/maximum.c Normal file
View File

@ -0,0 +1,49 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, char * argv[]) {
int tab[10] = {};
srand(time(NULL));
int rand_v = rand();
int max = -50;
//Génération des valeurs aléatoires et détection du maximum.
for (int i = 0; i < 10; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
max = max < tab[i] ? tab[i] : max;
}
//Placement de la flèche à l'endroit du maximum
for (int i = 0; i <= 10; i++) {
if (tab[i] != max) {
printf(" ");
} else {
printf(" V\n");
break;
}
}
//Génération du tableau (visuellement)
for (int i = 0; i < 10; i++) {
printf("+-----");
}
printf("+\n");
for (int i = 0; i < 10; i++) {
printf("| %3d ", tab[i]);
}
printf("|\n");
for (int i = 0; i < 10; i++) {
printf("+-----");
}
printf("+\n");
return EXIT_SUCCESS;
}

40
APL1.1/TP11/miroir.c Normal file
View File

@ -0,0 +1,40 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void showtable(int tablesize, int* table) {
for (int i = 0; i < tablesize; i++) printf("+-----");
printf("+\n");
for (int i = 0; i < tablesize; i++) printf("| %3d ", table[i]);
printf("|\n");
for (int i = 0; i < tablesize; i++) printf("+-----");
printf("+\n");
}
int main(int argc, char * argv[]) {
int tab[10] = {};
srand(time(NULL));
int rand_v = rand();
for (int i = 0; i < 10; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
}
showtable(10, tab);
int reserversetab[10] = {};
for (int i = 0; i < 10; i++) {
reserversetab[i] = tab[9-i];
}
printf("\n\n");
showtable(10, reserversetab);
return EXIT_SUCCESS;
}

53
APL1.1/TP11/recherche.c Normal file
View File

@ -0,0 +1,53 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define TABSIZE 10
int main(int argc, char * argv[]) {
int tab[TABSIZE] = {};
srand(time(NULL));
int rand_v = rand();
for (int i = 0; i < TABSIZE; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
printf("+-----");
}
printf("+\n");
for (int i = 0; i < TABSIZE; i++) {
printf("| %3d ", tab[i]);
}
printf("|\n");
for (int i = 0; i < TABSIZE; i++) {
printf("+-----");
}
printf("+\n");
int value;
int index = -1;
printf("Veuillez entrer une valeur : ");
scanf("%d", &value);
for (int i = 0; i < TABSIZE; i++) {
if (tab[i] == value) {
index = i + 1;
break;
}
}
if (index != -1) {
printf("L'index n°%d contient votre nombre.\n", index);
} else {
printf("Le tableau ne contient pas votre nombre.\n");
}
return EXIT_SUCCESS;
}

31
APL1.1/TP11/remplissage.c Normal file
View File

@ -0,0 +1,31 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(int argc, char * argv[]) {
int tab[10] = {};
srand(time(NULL));
int rand_v = rand();
for (int i = 0; i < 10; i++) {
srand(rand_v);
tab[i] = rand() % 101 > 50 ? (-rand() % 100) + 50: (rand() % 100) - 50;
rand_v = rand();
printf("+-----");
}
printf("+\n");
for (int i = 0; i < 10; i++) {
printf("| %3d ", tab[i]);
}
printf("|\n");
for (int i = 0; i < 10; i++) {
printf("+-----");
}
printf("+\n");
return EXIT_SUCCESS;
}