?
Solved

Loop

Posted on 2005-04-07
5
Medium Priority
?
375 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:cuong5985
  • 3
5 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13729278
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
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13729515
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
0
 

Author Comment

by:cuong5985
ID: 13738692
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

*
***
*****
*******
*********
***********
*********
*******
*****
***
*
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 13739219
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?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 13740770
>>>> This is output for your code:

Actually, I tested all three solutions I gave you

This is the output of the last code snippet:

Odd number? 11
*
**
***
****
*****
******
*******
********
*********
**********
***********
**********
*********
********
*******
******
*****
****
***
**
*

That is the output of the one before (first that uses nested loops)

Odd number? 11
*
**
***
****
*****
******
*******
********
*********
**********
***********
**********
*********
********
*******
******
*****
****
***
**
*

and that is the output of the code using std::string

Odd number? 11
*
**
***
****
*****
******
*******
********
*********
**********
***********
**********
*********
********
*******
******
*****
****
***
**
*

All three are equal and none of them gives the output you are posting here. What are you doing? Couldn't you copy/past ?

Ok, I saw (now) that the required solution suppresses all lines with an even number of stars. That indeed is a difference. The condition to check an integer on being odd is

  if ((i%2) == 1)

When taking solution 2 you could do the inner loops only if the loop counter is odd.

Or if you take the 'step' alternative: Simply add 2 and subtract 2 instead of 1. Is that really beyond your own capabalities or do you still refuse to overthink the solutions we gave you here?

The string variant is a little - very little - more difficult as you have to init the string with one star and add/subtract two stars at every run.

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

Is the output correct now?

Odd number? 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
 

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question