3 Replies - 1533 Views - Last Post: 07 September 2010 - 05:06 AM Rate Topic: -----

#1 roldown  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-April 10

Nested For Loop

Posted 06 September 2010 - 06:23 AM

Hello

I am currently trying to make a pyramid that starts from the lowest number up to the highest number (number entered).
This also includes a basic input number, so the for loop can use it and create this pyramid.
I want the Pyramid to look like this

.... *
... * *
.. * * *
. * * * *

NOTE: the dots and large spaces are not included.

So the number entered would be 4

I get the opposite of this with my program

#include<iostream>

using namespace std;

int main()
{
    int number;

    
    cout<<"Enter the number of disks in the pyramid: ";
    cin>>number;
    
    for(int i=0;i<number;i++)   //rows
    { 
            for(int g=i; g>number;g--)    //spaces
                    cout<<" ";
                    
                    for(int h=number-i;h>0;h--)    //number of stars
                            cout<<"*";
                            cout<<endl;
     }
    
    cout<<"\n";
    
    cout<<endl;
    
    system("pause");
    return 0;
}



Any Help at all will be Great full
:)

Is This A Good Question/Topic? 0
  • +

Replies To: Nested For Loop

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,087
  • Joined: 30-May 10

Re: Nested For Loop

Posted 06 September 2010 - 08:19 AM

Since i is always < number (by the outer loop), then this can never be true

> for(int g=i; g>number;g--)

No spaces will be printed.

Consider something like
for(int i=0;i<number;i++) {
  int numSpaces = ....
  int numStars = ....
}


Assign suitable values for those two variables, then write simple loops to output that many spaces and stars.
Was This Post Helpful? 1
  • +
  • -

#3 Guest_deadlydreamer*


Reputation:

Re: Nested For Loop

Posted 06 September 2010 - 08:55 AM

#include <iostream>

int main()
{
    // Set this 
    const int max = 5;


    for(int i = max; i > 0; i--)

    {
        for(int x = 0; x < i - 1; x++)

            std::cout.put(' ');

        for(int x = i; x < max; x++)

            std::cout << x;
        for(int x = max; x >= i; x--)

            std::cout << x;

        std::cout << std::endl;

    }
    return 0;
}


is that what you're looking for?
Was This Post Helpful? 1

#4 roldown  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 24
  • Joined: 14-April 10

Re: Nested For Loop

Posted 07 September 2010 - 05:06 AM

Ohh yer i forgot

Thankyou very much for your replys..

its greatly appreciated :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1