5 Replies - 13184 Views - Last Post: 14 April 2010 - 06:02 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 Loop Structure Pyramid

Posted 14 April 2010 - 04:49 AM

Hello C++ users i need help for a question of my Assignment, as this website is my last resort. The Questions asks me to create a pyramid out of asterisks.
That looks likes this

******************
.****************
..**************
...************
....**********
.....********
......******
the Dots are spaces.

This Pyramid goes starts at 18 at the tops, and goes down by 2 each row.
I Can not figure out how to subtract 2 from each row going down. and how to leave spaces before the character symbols.
I have spent the last days in the library, reading past books trying to figure out and trying to find past examples, but no book seems to direct Nested Loop Structures to what the assignment wants.
This is my current Code that is getting me no where.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int row, col,space;
    row=0;
    col=0;
    space=2;
    
    space=2;

    for (row=1;row<=7;row++)
    {
        for(col=18;col>=1;col--)
        cout<<"*";
        cout<<endl;
    }


   system("pause");
   return 0;
}


Any Help will be Appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Nested Loop Structure Pyramid

#2 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Nested Loop Structure Pyramid

Posted 14 April 2010 - 05:00 AM

you simply at the first line print n stars,

each time stars are less, but gaps are more !

drop-down untill it is 1 star, then stop


for (int i=0; i<8; i++) 
{
    for(int j=i; j>0; j--) 
        cout << " ";
    for(int w=(8-i); w>0; w--) 
        cout << "*";
    cout << endl;
}


Was This Post Helpful? 1
  • +
  • -

#3 roldown  Icon User is offline

  • New D.I.C Head

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

Re: Nested Loop Structure Pyramid

Posted 14 April 2010 - 05:18 AM

Im only Getting Half of the pyramid structure
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int i;
    i=18;


    for (int i=0; i<7; i++) 
    {
    for(int j=i; j>0; j--) 
        cout << " ";
    for(int w=(8-i); w>0; w--) 
        cout << "*";
    cout << endl;
     }

   system("pause");
   return 0;
}



and if i do make the second for value 18
it wont let me create spaces at the end.
if you know what i mean .. thanks for the reply by the way .
Was This Post Helpful? 0
  • +
  • -

#4 roldown  Icon User is offline

  • New D.I.C Head

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

Re: Nested Loop Structure Pyramid

Posted 14 April 2010 - 05:35 AM

I got it .. Thankyou sooooo much !
Was This Post Helpful? 0
  • +
  • -

#5 codeaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-November 09

Re: Nested Loop Structure Pyramid

Posted 14 April 2010 - 05:55 AM

plus is giving you a really good hint.
However you need to print 2 less stars each time therefore you need to change the second loop to :
  for(int w=(8-(i*2)); w>=0; w--) 
        cout << "*";



I'm pretty sure this fixes the problem.
Note 8 should be replaced by the number of the rows of your pyramid


BTW don't forget to press the thank you Button if you think I helped.
Was This Post Helpful? 1
  • +
  • -

#6 codeaholic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 06-November 09

Re: Nested Loop Structure Pyramid

Posted 14 April 2010 - 06:02 AM

Oops I didn't see your last post.
Otherwise I wouldn't post my comment.
But I leave it in case it helps someone else in future.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1