TP : tableaux : implantation de fonctions classiques#

#include <iostream>
#include <vector>
using namespace std;

Exercice: fonctions max et min#

On a vu en TD comment calculer le maximum d’un tableau d’entiers. On va maintenant aborder des variantes, comme calculer le maximum d’un tableau de chaînes de caractères. On appellera nos fonctions monMax et monMin afin d’éviter des conflits avec les fonctions préexistantes de C++.

Comme les exemples suivants l’illustrent, la comparaison de deux chaînes de caractères se fait – en C++ comme dans la plupart des langages – selon l’ordre lexicographique (celui du dictionnaire):

string("alice") < string("bob")
string("alice") < string("alain")
  1. Implanter la fonction monMax dont la documentation est donnée ci-dessous, qui prend en entrée un tableau d’entiers et renvoie le plus grand.
    Indication: Si besoin, inspirez vous de la fonction mystere de l’exercice 2 du TD (mais le mieux est de savoir le faire seul).

/** Maximum d un tableau d entiers
 * @param t un tableau d entiers
 * @return l élément le plus grand du tableau
 **/
int monMax(vector<int> t) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
  • Vérifier que la fonction se comporte comme prévu :

monMax( { 5, -1, 3, 7, -4, 8, 4 } )
CHECK( monMax( { 5 }) == 5 );
CHECK( monMax({ 5, -1, 3, 7, -4, 8, 4 }) == 8 );
  1. Écrire la documentation puis implanter la fonction monMin qui prend en entrée un tableau de chaînes de caractères et renvoie la plus petite pour l’ordre lexicographique :

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
string monMin(vector<string> t) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
monMin( { "bonjour", "allo", "xylophone", "fonction", "schtroumpf" } )
  • Le test précédent était manuel. Transformer le en test automatique équivalent, en utilisant CHECK :

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
CHECK( monMin( { "coucou"}) == "coucou" );
CHECK( monMin( { "ok", "bonjour", "quarante-deux", "info-111"}) == "bonjour" );

Exercice#

Compléter chacune des fonctions suivantes:

  • Si il n’y a pas de documentation, l’écrire;

  • Si la fonction est vide, l’implanter;

  • S’il n’y a pas de tests automatiques, en rajouter en utilisant CHECK.

Fonction estPositif :#

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
bool estPositif(vector<float> tab) {
    for(int i=0; i < tab.size(); i++){
        if ( tab[i] < 0. ) {
            return false;
        }
    }
    return true;
 }

Tests:

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE

Fonction incremente#

La fonction suivante renvoie un tableau. En principe on devrait mettre vector<int> comme type de retour. Pour contourner un bug dans cling (corrigé dans la version de développement), on va mettre le type tableau à la place, après avoir défini ce type comme un alias pour vector<int>.

using tableau = vector<int>;
/** Fonction incremente
 * @param t un tableau d entiers
 * @return un tableau d entiers obtenu en incrémentant de 1 chaque valeur de t
 **/
tableau incremente(vector<int> t) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}

Tests:

CHECK( incremente({5,1,3}) == vector<int>({6,2,4}) )

Fonction egale#

/** Teste si deux tableaux sont égaux (même longueur, mêmes valeurs)
 * @param t1: un tableau d entiers
 * @param t2: un tableau d entiers
 * @return true si les deux tableaux sont égaux, false sinon
 **/
bool egale(vector<int> t1, vector<int> t2) {
    if( t1.size() != t2.size() ) {
        return false;
    }
    for ( int i=0; i < t1.size(); i++ ) {
        if ( t1[i] != t2[i] ) {
            return false;
        }
    }
    return true;
}

Tests:

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE

Fonction produitTableau#

Implanter la fonction documentée ci-dessous.
Indication: Inspirez-vous de la fonction somme du cours.

/** renvoie le produit de tous les éléments d un tableau d entiers
 * @param t un tableau d entiers
 * @return le résultat du calcul de t[0] * t[1] * t[2] * ... 
 **/
// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
produitTableau({2, 3, 4}) // doit valoir 24
produitTableau({0, 10, 100}) // doit valoir 0

Tests:

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE

Fonction plusDeVrai#

/** Teste si un tableau contient plus de `true` que de `false`
 * @param tab un tableau de booleen
 * @return true si le tableau contient plus de `true`, `false` sinon
 **/
bool plusDeVrai(vector<bool> tab) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}

Tests:

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE

Fonction contient#

La fonction contient prend en argument un tableau et un entier et renvoie vrai si le tableau contient l’entier, faux sinon.

// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE

Tests:

CHECK(     contient( { 5, -1, 8, 4 }, 8) );
CHECK( not contient( { 1, 2 }, 42)       );