First commit
This commit is contained in:
169
IUT/Info1/TD/Exercices/10.5_tableau_boucle/10.5.md
Normal file
169
IUT/Info1/TD/Exercices/10.5_tableau_boucle/10.5.md
Normal file
@@ -0,0 +1,169 @@
|
||||
{Fonction 1
|
||||
R : copier les élements d’un tableau d’entiers dans un autre.
|
||||
E : tab_in un tableau d'entiers, taille_pratique_in un entier, taille_réele_out
|
||||
E/S : tab_out un tableau d'entiers
|
||||
S : Vide
|
||||
}
|
||||
copy : une fonction(
|
||||
tab_in : un tableau d'entiers,
|
||||
taille_pratique_in : un entier,
|
||||
tab_out : un tableau d'entiers,
|
||||
taille_réele_out : un entier
|
||||
) -> vide
|
||||
Lexique :
|
||||
i : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
Si taille_pratique_in <= taille_réele_out
|
||||
Alors i <- 0
|
||||
Tant que i < taille_pratique_in
|
||||
Faire tab_out[i] <- tab_in[i]
|
||||
i <- i + 1
|
||||
Fin
|
||||
|
||||
|
||||
|
||||
|
||||
{Fonction 2
|
||||
R : afficher les élements d’un tableau d’entiers sous la forme : {e1,e2,e3,...,en}
|
||||
E : tab un tableau d'entiers, taille_pratique un entier
|
||||
S : Vide
|
||||
}
|
||||
afficher_tableau : une fonction(
|
||||
tab : un tableau d'entiers,
|
||||
taille_pratique : un entier
|
||||
) -> vide
|
||||
Lexique :
|
||||
i : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
i <- 0
|
||||
Tant que i < taille
|
||||
Faire Ecrire(tab[i], ",") {On aura une virgule à la fin, mais l'enlever complexifierai inutilemment l'algorithme}
|
||||
i <- i + 1
|
||||
Fin
|
||||
|
||||
|
||||
|
||||
|
||||
{Fonction 3
|
||||
R : tester si deux tableaux sont identiques
|
||||
E : tab1 un tableau d'entiers, taille_pratique_1 un entier, tab2 un tableau d'entiers, taille_pratique_2 un entier
|
||||
S : vrai si les 2 tableau sont identiques
|
||||
}
|
||||
sont_identiques : une fonction(
|
||||
tab1 : un tableau d'entiers,
|
||||
tab2 : un tableau d'entiers,
|
||||
taille_pratique_1 : un entier,
|
||||
taille_pratique_2 : un entier
|
||||
) -> un booléen
|
||||
Lexique :
|
||||
i : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
Si taille_pratique_1 != taille_pratique_2
|
||||
Alors Retourner(Faux)
|
||||
i <- 0
|
||||
Tant que i < taille_pratique_1
|
||||
Faire Si tab1[i] != tab2[i]:
|
||||
Alors Retourner(Faux)
|
||||
Retourner(Vrai)
|
||||
Fin
|
||||
|
||||
|
||||
|
||||
|
||||
{Fonction 4
|
||||
R : calculer la moyenne des élements d’un tableau
|
||||
E : tab un tableau d'entiers, taille_pratique un entier
|
||||
S : un réel : la moyenne de tab
|
||||
}
|
||||
moyenne_tableau : une fonction(
|
||||
tab : un tableau de réels,
|
||||
taille_pratique : un entier
|
||||
) -> un réel
|
||||
Lexique :
|
||||
i : un entier
|
||||
somme : un réel
|
||||
Algorithme :
|
||||
Début
|
||||
i <- 0
|
||||
somme <- 0
|
||||
Tant Que i < taille_pratique
|
||||
Faire somme <- somme + tab[i]
|
||||
Retourner(somme/taille_pratique)
|
||||
Fin
|
||||
|
||||
|
||||
|
||||
|
||||
{Fonction 5
|
||||
R : remplacer les valeurs d’un tableau par sa somme cumulée
|
||||
E : tab le tableau d'entiers, taille_pratique un entier
|
||||
E/S : tab le tableau de réels
|
||||
S : vide
|
||||
}
|
||||
somme_cumulee : une fonction(
|
||||
tab : un tableau d'entiers
|
||||
taille_pratique : un entier
|
||||
) -> vide
|
||||
Lexique :
|
||||
i : un entier
|
||||
j : un entier
|
||||
somme : un réel
|
||||
Algorithme :
|
||||
Début
|
||||
i <- 0
|
||||
Tant Que i < taille_pratique
|
||||
j <- 0
|
||||
somme <- 0
|
||||
Tant Que j <= i
|
||||
Faire somme <- somme + tab[j]
|
||||
j <- j + 1
|
||||
tab[i] <- somme
|
||||
|
||||
i <- i + 1
|
||||
Fin
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Lexique :
|
||||
Saisie_entiers : une fonction(tab: un tableau d'entiers, taille_réele : un entier) -> un entier
|
||||
copy : une fonction(tab_in : un tableau d'entiers,taille_pratique_in : un entier,tab_out : un tableau d'entiers, taille_réele_out : un entier) -> vide
|
||||
afficher_tableau : une fonction(tab : un tableau d'entiers, taille_pratique : un entier ) -> vide
|
||||
sont_identiques : une fonction(tab1 : un tableau d'entiers, tab2 : un tableau d'entiers, taille_pratique_1 : un entier, taille_pratique_2 : un entier) -> un booléen
|
||||
moyenne_tableau : une fonction(tab : un tableau d'entiers, taille_pratique : un entier) -> un réel
|
||||
somme_cumulee : une fonction(tab : un tableau d'entiers, taille_pratique : un entier) -> vide
|
||||
|
||||
# Fonctions 1, 2, 3, 4 et 5
|
||||
tab1 : un tableau de 1000 entiers
|
||||
taille_réele_1 : la constante entière := 1000
|
||||
taille_pratique_1 : un entier
|
||||
|
||||
tab1_copie : un tableau de 1000 entiers
|
||||
taille_réele_copie_1 : la constante entière := 1000
|
||||
|
||||
tab2 : un tableau de 1000 entiers
|
||||
taille_réele_2 : la constante entière := 1000
|
||||
taille_pratique_2 : un entier
|
||||
|
||||
Algorithme :
|
||||
Début
|
||||
taille_pratique_1 <- Saisie_entiers(tab1)
|
||||
taille_pratique_copie_1 <- taille_pratique_1
|
||||
copy(tab1, taille_pratique_1, tab1_copie, taille_réele_copie_1)
|
||||
afficher_tableau(tab1_copie, taille_pratique_copie_1)
|
||||
|
||||
taille_pratique_2 <- Saisie_entiers(tab2)
|
||||
Ecrire(
|
||||
"tab1 et tab2 sont identiques : ",
|
||||
sont_identiques(tab1, tab2, taille_pratique_1, taille_pratique_2)
|
||||
)
|
||||
|
||||
Ecrire("La moyenne de tab1 est : ", moyenne_tableau(tab1, taille_pratique_1))
|
||||
|
||||
somme_cumulee(tab1_copie, taille_pratique_copie_1)
|
||||
afficher_tableau(tab1_copie, taille_pratique_copie_1)
|
||||
Fin
|
||||
122
IUT/Info1/TD/Exercices/10.5_tableau_boucle/10.5.txt
Normal file
122
IUT/Info1/TD/Exercices/10.5_tableau_boucle/10.5.txt
Normal file
@@ -0,0 +1,122 @@
|
||||
{Fonction 1
|
||||
R : copier les élements d’un tableau d’entiers dans un autre.
|
||||
E : tab_in un tableau d'entiers, taille_pratique_in un entier, taille_réele_out
|
||||
E/S : tab_out un tableau d'entiers
|
||||
S : Vide}
|
||||
copy : une fonction(tab_in : un tableau d'entiers,
|
||||
taille_pratique_in : un entier,
|
||||
tab_out : un tableau d'entiers,
|
||||
taille_réele_out : un entier) -> vide
|
||||
Lexique :
|
||||
i : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
Si taille_pratique_in <= taille_réele_out
|
||||
Alors i <- 0
|
||||
Tant que i < taille_pratique_in
|
||||
Faire tab_out[i] <- tab_in[i]
|
||||
i <- i + 1
|
||||
Fin
|
||||
{Fonction 2
|
||||
R : afficher les élements d’un tableau d’entiers sous la forme : {e1,e2,e3,...,en}
|
||||
E : tab un tableau d'entiers, taille_pratique un entier
|
||||
S : Vide}
|
||||
afficher_tableau : une fonction(tab : un tableau d'entiers,
|
||||
taille_pratique : un entier) -> vide
|
||||
Lexique :
|
||||
i : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
i <- 0
|
||||
Tant que i < taille
|
||||
Faire Ecrire(tab[i], ",") {On aura une virgule à la fin, mais l'enlever complexifierai inutilemment l'algorithme}
|
||||
i <- i + 1
|
||||
Fin
|
||||
{Fonction 3
|
||||
R : tester si deux tableaux sont identiques
|
||||
E : tab1 un tableau d'entiers, taille_pratique_1 un entier, tab2 un tableau d'entiers, taille_pratique_2 un entier
|
||||
S : vrai si les 2 tableau sont identiques}
|
||||
sont_identiques : une fonction(tab1 : un tableau d'entiers,
|
||||
tab2 : un tableau d'entiers,
|
||||
taille_pratique_1 : un entier,
|
||||
taille_pratique_2 : un entier) -> un booléen
|
||||
Lexique :
|
||||
i : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
Si taille_pratique_1 != taille_pratique_2
|
||||
Alors Retourner(Faux)
|
||||
i <- 0
|
||||
Tant que i < taille_pratique_1
|
||||
Faire Si tab1[i] != tab2[i]:
|
||||
Alors Retourner(Faux)
|
||||
Retourner(Vrai)
|
||||
Fin
|
||||
{Fonction 4
|
||||
R : calculer la moyenne des élements d’un tableau
|
||||
E : tab un tableau d'entiers, taille_pratique un entier
|
||||
S : un réel : la moyenne de tab}
|
||||
moyenne_tableau : une fonction(tab : un tableau de réels,
|
||||
taille_pratique : un entier) -> un réel
|
||||
Lexique :
|
||||
i : un entier
|
||||
somme : un réel
|
||||
Algorithme :
|
||||
Début i <- 0
|
||||
somme <- 0
|
||||
Tant Que i < taille_pratique
|
||||
Faire somme <- somme + tab[i]
|
||||
Retourner(somme/taille_pratique)
|
||||
Fin
|
||||
{Fonction 5
|
||||
R : remplacer les valeurs d’un tableau par sa somme cumulée
|
||||
E : tab le tableau d'entiers, taille_pratique un entier
|
||||
E/S : tab le tableau de réels
|
||||
S : vide}
|
||||
somme_cumulee : une fonction(tab : un tableau d'entiers
|
||||
taille_pratique : un entier) -> vide
|
||||
Lexique :
|
||||
i : un entier
|
||||
j : un entier
|
||||
somme : un réel
|
||||
Algorithme :
|
||||
Début i <- 0
|
||||
Tant Que i < taille_pratique
|
||||
Faire j <- 0
|
||||
somme <- 0
|
||||
Tant Que j <= i
|
||||
Faire somme <- somme + tab[j]
|
||||
tab[i] <- somme
|
||||
i <- i + 1
|
||||
Fin
|
||||
Lexique :
|
||||
Saisie_entiers : une fonction(tab: un tableau d'entiers, taille_réele : un entier) -> un entier
|
||||
copy : une fonction(tab_in : un tableau d'entiers,taille_pratique_in : un entier,tab_out : un tableau d'entiers, taille_réele_out : un entier) -> vide
|
||||
afficher_tableau : une fonction(tab : un tableau d'entiers, taille_pratique : un entier ) -> vide
|
||||
sont_identiques : une fonction(tab1 : un tableau d'entiers, tab2 : un tableau d'entiers, taille_pratique_1 : un entier, taille_pratique_2 : un entier) -> un booléen
|
||||
moyenne_tableau : une fonction(tab : un tableau d'entiers, taille_pratique : un entier) -> un réel
|
||||
somme_cumulee : une fonction(tab : un tableau d'entiers, taille_pratique : un entier) -> vide
|
||||
tab1 : un tableau de 1000 entiers
|
||||
taille_réele_1 : la constante entière := 1000
|
||||
taille_pratique_1 : un entier
|
||||
tab1_copie : un tableau de 1000 entiers
|
||||
taille_réele_copie_1 : la constante entière := 1000
|
||||
tab2 : un tableau de 1000 entiers
|
||||
taille_réele_2 : la constante entière := 1000
|
||||
taille_pratique_2 : un entier
|
||||
tab_1_et_2_identiques : un booléen
|
||||
moyenne_tab1 : un réel
|
||||
somme_cumulee_tab1_copie : un entier
|
||||
Algorithme :
|
||||
Début
|
||||
taille_pratique_1 <- Saisie_entiers(tab1)
|
||||
copy(tab1, taille_pratique_1, tab1_copie, taille_réele_copie_1)
|
||||
afficher_tableau(tab1_copie, taille_pratique_copie_1)
|
||||
taille_pratique_2 <- Saisie_entiers(tab2)
|
||||
tab_1_et_2_identiques <- sont_identiques(tab1, tab2, taille_pratique_1, taille_pratique_2)
|
||||
Ecrire(tab_1_et_2_identiques)
|
||||
moyenne_tab1 <- moyenne_tableau(tab1, taille_pratique_1)
|
||||
Ecrire(moyenne_tab1)
|
||||
somme_cumulee_tab1_copie <- somme_cumulee(tab_copie, taille_pratique_copie_1)
|
||||
Ecrire(somme_cumulee_tab1_copie)
|
||||
Fin
|
||||
39
IUT/Info1/TD/Exercices/10.5_tableau_boucle/main.cpp
Normal file
39
IUT/Info1/TD/Exercices/10.5_tableau_boucle/main.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "tableaux.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
const unsigned int taille_reelle1 = 1000;
|
||||
unsigned int taille_pratique1;
|
||||
unsigned int tab1[taille_reelle1];
|
||||
|
||||
const unsigned int taille_reelle_copie_1 = 1000;
|
||||
unsigned int taille_pratique_copie1;
|
||||
unsigned int tab1_copie[taille_reelle_copie_1];
|
||||
|
||||
const unsigned int taille_reelle2 = 1000;
|
||||
unsigned int taille_pratique2;
|
||||
unsigned int tab2[taille_reelle2];
|
||||
|
||||
taille_pratique1 = saisie_entiers(tab1, taille_reelle1);
|
||||
taille_pratique_copie1 = taille_pratique1;
|
||||
|
||||
copie(tab1, tab1_copie, taille_pratique1, taille_reelle_copie_1);
|
||||
afficher_tableau(tab1_copie, taille_pratique_copie1);
|
||||
|
||||
taille_pratique2 = saisie_entiers(tab2, taille_reelle2);
|
||||
cout << "tab1 et tab2 sont identiques : "
|
||||
<< sont_identiques(tab1, tab2, taille_pratique1, taille_pratique2)
|
||||
<< endl;
|
||||
|
||||
cout << "La moyenne de tab1 est : "
|
||||
<< moyenne_tableau(tab1, taille_pratique1)
|
||||
<< endl;
|
||||
|
||||
somme_cumulee(tab1_copie, taille_pratique_copie1);
|
||||
afficher_tableau(tab1_copie, taille_pratique_copie1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
103
IUT/Info1/TD/Exercices/10.5_tableau_boucle/tableaux.cpp
Normal file
103
IUT/Info1/TD/Exercices/10.5_tableau_boucle/tableaux.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "tableaux.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Fonction 0
|
||||
unsigned int saisie_entiers(
|
||||
unsigned int taille_reelle,
|
||||
unsigned int tab[]
|
||||
) {
|
||||
unsigned int i = 0;
|
||||
int saisie;
|
||||
cout << "Saisir jusqu'a " << taille_reelle << " entiers positifs. Terminer la saisie par -1" << endl;
|
||||
do {
|
||||
cin >> saisie;
|
||||
if (saisie != -1) {
|
||||
tab[i] = saisie;
|
||||
i++;
|
||||
}
|
||||
} while (saisie != -1 && i < taille_reelle);
|
||||
return i;
|
||||
}
|
||||
|
||||
// Fonction 1
|
||||
void copie(
|
||||
const unsigned int tab_in[],
|
||||
unsigned int tab_out[],
|
||||
unsigned int taille_pratique_in,
|
||||
unsigned int taille_reelle_out
|
||||
) {
|
||||
if (taille_pratique_in <= taille_reelle_out) {
|
||||
for (unsigned int i = 0; i < taille_pratique_in; i++) {
|
||||
tab_out[i] = tab_in[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fonction 2
|
||||
void afficher_tableau(
|
||||
const unsigned int tab[],
|
||||
unsigned int taille_pratique
|
||||
) {
|
||||
unsigned int i = 0;
|
||||
|
||||
cout << "{" << tab[i];
|
||||
|
||||
for (i = 1; i+1 < taille_pratique; i++) {
|
||||
cout << "," << tab[i];
|
||||
}
|
||||
|
||||
cout << "}";
|
||||
}
|
||||
|
||||
// Fonction 3
|
||||
bool sont_identiques(
|
||||
const unsigned int tab1[],
|
||||
const unsigned int tab2[],
|
||||
unsigned int taille_pratique1,
|
||||
unsigned int taille_pratique2
|
||||
) {
|
||||
if (taille_pratique1 != taille_pratique2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < taille_pratique1; i++) {
|
||||
if (tab1[i] != tab2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fonction 4
|
||||
float moyenne_tableau(
|
||||
const unsigned int tab[],
|
||||
unsigned int taille_pratique
|
||||
) {
|
||||
int somme = 0;
|
||||
|
||||
for (unsigned int i = 0; i < taille_pratique; i++) {
|
||||
somme += tab[i];
|
||||
}
|
||||
|
||||
return (float)somme/taille_pratique;
|
||||
}
|
||||
|
||||
// Fonction 5
|
||||
void somme_cumulee(
|
||||
unsigned int tab[],
|
||||
unsigned int taille_pratique
|
||||
) {
|
||||
int somme;
|
||||
|
||||
for (unsigned int i = 0; i < taille_pratique; i++) {
|
||||
somme = 0;
|
||||
for (unsigned int j = 0; j <= i; i++) {
|
||||
somme += tab[j];
|
||||
}
|
||||
tab[i] = somme;
|
||||
}
|
||||
}
|
||||
33
IUT/Info1/TD/Exercices/10.5_tableau_boucle/tableaux.h
Normal file
33
IUT/Info1/TD/Exercices/10.5_tableau_boucle/tableaux.h
Normal file
@@ -0,0 +1,33 @@
|
||||
unsigned int saisie_entiers(
|
||||
unsigned int tab[],
|
||||
unsigned int taille_reelle
|
||||
);
|
||||
|
||||
void copie(
|
||||
const unsigned int tab_in[],
|
||||
unsigned int tab_out[],
|
||||
unsigned int taille_pratique_in,
|
||||
unsigned int taille_reelle_out
|
||||
);
|
||||
|
||||
void afficher_tableau(
|
||||
const unsigned int tab[],
|
||||
unsigned int taille_pratique
|
||||
);
|
||||
|
||||
bool sont_identiques(
|
||||
const unsigned int tab1[],
|
||||
const unsigned int tab2[],
|
||||
unsigned int taille_pratique1,
|
||||
unsigned int taille_pratique2
|
||||
);
|
||||
|
||||
float moyenne_tableau(
|
||||
const unsigned int tab[],
|
||||
unsigned int taille_pratique
|
||||
);
|
||||
|
||||
void somme_cumulee(
|
||||
unsigned int tab[],
|
||||
unsigned int taille_pratique
|
||||
);
|
||||
Reference in New Issue
Block a user