0

Hi everybody,

I bought the book "Deitel C++ How to Program, edition 5", and i am doing the exercises of the chapter 4, the exercise 4.28 ask you to write a program that print the following figure:

* * * * * * *
 * * * * * * *
* * * * * * *
* * * * * * *
 * * * * * * *
* * * * * * *
 * * * * * * *
* * * * * * *

You must use only three output streams:

cout << "* ";
cout << " ";
cout << endl;

I wrote the program, but i think i did it by a hard way.

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
   int counter = 8; // Contador para o loop
   int hcounter = 1; // Contador para linhas horizontais
   int test = 1; // Variavel para controle da linha 4, onde o contador tem que ser alterado

   while ( counter > 1 ) // Enquanto contador maior que 1
   {

      if ( counter == 5 && test == 1 ) 
      {
         counter = 6;
         test = 0;
      }

      if ( counter % 2 != 0 )
         cout << " ";

      while ( hcounter <= 7 )
      {
         cout << "* ";
         hcounter++;
      }

      cout << endl;

      hcounter = 1;
      if ( counter == 6 && test == 0 )
         counter = 5;
      else
         counter--;

   }

   return 0;

}

This code works, but i think i am having difficult with the programming logic, what can you recommend me?

Thanks and sorry the bad english.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by alwaysLearning0
0

Practice as much as possible.

And think simple.

You just have to give output "*" and " " in front of "*" for row 2, 5 and 7

for(i = 1; i <= 8; i++){

        //For row 2 or 5 or 7, we will give output  a space
         if( i == 2 || i == 5 || i == 7)
                cout << " "
          for(j = 1; j <= 7; j++)
                cout << "*"

         cout << endl;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.