TP : dessins géométriques ♣#

Exercice :

Dans cet exercice, nous utiliserons des affichages et aurons donc besoin de la bibliothèque C++ correspondante :

#include <iostream>
using namespace std;
  • Écrivez une fonction carre_plein qui, pour un entier positif non nul \(L\) donné, affiche sur la sortie standard un carré plein dont les côtés sont de longueur \(L\) caractères. Par exemple, pour \(L=5\), la fonction affichera :

    *****
    *****
    *****
    *****
    *****
    
void carre_plein(int L) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
carre_plein(5)
carre_plein(3)
  • Même question mais cette fois la fonction affiche un carré vide. Pour l’exemple précédent, cela donne :

    *****
    *   *
    *   *
    *   *
    *****
    
void carre_vide(int L) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
carre_vide(5)
carre_vide(2)
carre_vide(1)
carre_vide(0)
  • Transformez votre fonction pour qu’il soit simple de remplacer le caractère * par un autre caractère caractere :

void carre_vide(int L, string caractere) {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
carre_vide(4, "#")
  • Écrivez une fonction qui, pour un entier positif \(h\) donné, affiche le triangle isocèle  pointe en haut  dont le contour est décrit par des étoiles, de hauteur \(h\) et de base \(2h-1\). Par exemple, pour \(h=5\), la fonction affichera :

            *
           * *
          *   *
         *     *
        *********
    
void triangle(int h, string caractere="*") {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE

Vous noterez ci-dessous que l’on a donné une valeur par défaut pour caractere : *. Du coup, on peut utiliser les deux formes suivantes :

triangle(4)
triangle(5, "#")
triangle(2)
triangle(1)
triangle(0)
  • Écrivez une fonction qui affiche un losange. Par exemple, pour \(h=5\), la fonction affichera :

        *
       * *
      *   *
     *     *
    *       *
     *     *
      *   *
       * *
        *
    
void losange(int h, string caractere="*") {
    // REMPLACER CETTE LIGNE ET LA SUIVANTE PAR VOTRE RÉPONSE
    throw std::runtime_error("À faire");
}
// REMPLACER CETTE LIGNE PAR VOTRE RÉPONSE
losange(5)
losange(1)
losange(0)