Link to home
Start Free TrialLog in
Avatar of cuong5985
cuong5985

asked on

Loop

Topic
/////////////////////////////////////////////////////////////////////////////
Use nested loops to generate this output:

Enter an odd positive number for the base of the triangle: 11

*
***
*****
*******
*********
***********
*********
*******
*****
***
*
/////////////////////////////////////////////////////////////////
This is my program

#include <iostream.h>
#include <stdlib.h>

int main()
{
     int nSize = 0;
     int nCount = 1;
     int size;
     int i;

      cout << "Enter an odd number for the base of the tree: ";
      cin >> nSize;
      if (0 == size)
      {
          cout << "Exiting" << endl;
          // Handle errors how you see fit...
          exit(0);   // Will exit your program
      }
      while(0 == (nSize%2))
       {
          cout << "Enter an odd positive number for the base of the triangle: ";
          cin >> nSize;
       }
      for(i =0; i<nSize;i++)
      {
          (15-i, 5+i);
          for(int j=1; j<=2*i+1; j++)
          cout<<"*";              //print the x's
          cout<<endl;
      }

      system("PAUSE");
      return 0;
}

And this is the output from my program,
Enter an odd number for the base of the tree: 11
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
Press any key to continue . . .

It doesnt show exactly the same way that the program require, can someone teach me how to correct it. Thanks
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

replace
 
    for(i =0; i<nSize;i++)
      {
          (15-i, 5+i);
          for(int j=1; j<=2*i+1; j++)
          cout<<"*";              //print the x's
          cout<<endl;
      }

by

   string stars;
   int i;
   
   for (i = 0; i < nSize; i++)
   {
        stars += '*';          // add  '*'
        cout << stars << endl;  
   }    
   for (i = nSize-1; i > 0; --i)
   {
         stars.resize(i);     // remove '*'
         cout << stars << endl;
   }

Don't forget to add

#include <string>
using namespace std:

at top of your cpp

Regards, Alex
I saw that you have to use nested loops instead of a string. Ok.

   int i, j;
   for (i = 1; i <= nSize; i++)
   {
        for (j = 1; j <= i; j++)         // add  '*'
             cout << '*';  
        cout << endl;
   }    
   for (i = nSize-1; i > 0; --i)
   {
        for (j = 1; j <= i; j++)          // add  '*'
             cout << '*';  
        cout << endl;
   }

Note, I run the loops from 1 to 11 respectivly from 10 to 1, what isn't usual in C/C++ (normally runs from 0 to nSize-1). But this way, the loop counter 'i' always counts the number of asteriks (*) to output in the current line, what should it make easier for you to understand the loops. Did you see, that the inner loop is equal in bozh outer loops?

You also could use one loop for both, where we first count up to nSize and then down to 1 again:

    int i, j;
   int step = 1;
   for (i = 1;  i > 0; i += step)
   {
        for (j = 1; j <= i; j++)         // add  '*'
             cout << '*';  
        cout << endl;

        // set step to -1 if nSize was reached
        if (i >= nSize)   // >= prevents an infinite loop if nSize <= 0
           step = -1;
   }    

Regards, Alex
Avatar of cuong5985
cuong5985

ASKER

This is output for your code:
Enter an odd number for the base of the triangle: 11

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

Press any key to continue . . .

Is different from the solution which is
Enter an odd positive number for the base of the triangle: 11

*
***
*****
*******
*********
***********
*********
*******
*****
***
*
ok, it's different but how much will you need to modify the code get the desired result.
1. to print 2 more(or less) stars on each consecutive row.
2. need to take care of the peak - it should not be repeated.
are these hints good enough?
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial