DEV/DEV1.1/TP12/TP12_reponses.txt

232 lines
4.1 KiB
Plaintext
Raw Permalink Normal View History

2024-09-30 15:02:16 +02:00
------- TP12 : Tableaux -------
1.
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define TAILLE_TABLEAU 10
int main(void){
int tab[TAILLE_TABLEAU];
int i;
srand(time(NULL));
/* Remplissage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
tab[i] = (rand() % 100) - 50;
}
/* Affichage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
for (i = 0; i != TAILLE_TABLEAU; 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 != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
return EXIT_SUCCESS;
}
2.
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define TAILLE_TABLEAU 10
int main(void){
int tab[TAILLE_TABLEAU];
int i;
int maxi;
int indice_max;
srand(time(NULL));
/* Remplissage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
tab[i] = (rand() % 100) - 50;
}
/* Recherche du maximum */
maxi = tab[0];
indice_max = 0;
for (i = 0; i != TAILLE_TABLEAU; i++) {
if (tab[i] > maxi) {
maxi = tab[i];
indice_max = i;
}
}
/* Affichage du maximum */
for (i = 0; i != (indice_max*6) + 3; i++) {
printf(" ");
}
printf("|\n");
for (i = 0; i != (indice_max*6) + 3; i++) {
printf(" ");
}
printf("V\n");
/* Affichage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
for (i = 0; i != TAILLE_TABLEAU; 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 != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
return EXIT_SUCCESS;
}
3.
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define TAILLE_TABLEAU 10
int main(void){
int tab[TAILLE_TABLEAU];
int i;
int recherche;
int indice_recherche;
srand(time(NULL));
/* Remplissage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
tab[i] = (rand() % 100) - 50;
}
/* Affichage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
for (i = 0; i != TAILLE_TABLEAU; 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 != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
/* Recherche d'indice par l'utilisateur */
printf("Quelle valeur chercher ? ");
scanf("%d",&recherche);
indice_recherche = -1;
for (i = 0; i != TAILLE_TABLEAU; i++) {
if (tab[i] == recherche && indice_recherche == -1) {
indice_recherche = i;
}
}
printf("\n%d\n", indice_recherche);
return EXIT_SUCCESS;
}
4.
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define TAILLE_TABLEAU 10
int main(void){
int tab[TAILLE_TABLEAU];
int tab_inverse[TAILLE_TABLEAU];
int i;
srand(time(NULL));
/* Remplissage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
tab[i] = (rand() % 100) - 50;
}
/* Affichage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
for (i = 0; i != TAILLE_TABLEAU; 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 != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
/* Remplissage du tableau inverse */
for (i = 0; i != TAILLE_TABLEAU; i++) {
tab_inverse[i] = tab[TAILLE_TABLEAU-i-1];
}
/* Affichage du tableau */
for (i = 0; i != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
for (i = 0; i != TAILLE_TABLEAU; i++) {
if (tab_inverse[i] < 10 && tab_inverse[i] >= 0) {
printf("| %d ", tab_inverse[i]);
}
else if (tab_inverse[i] < -9) {
printf("| %d ", tab_inverse[i]);
}
else {
printf("| %d ", tab_inverse[i]);
}
}
printf("|\n");
for (i = 0; i != TAILLE_TABLEAU; i++) {
printf("+-----");
}
printf("+\n");
return EXIT_SUCCESS;
}