# Pyramid of *s!

Page 1 of 1

## 7 Replies - 8975 Views - Last Post: 14 September 2011 - 04:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=246034&amp;s=f25aaa542e19f14789776499f5a4a4ec&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 C++ newb

• New D.I.C Head

Reputation: 0
• Posts: 27
• Joined: 04-September 11

# Pyramid of *s!

Posted 04 September 2011 - 07:44 PM

Hullo everyone! As you can tell from my username I'm a beginner with the C++ programming language and it would be awesome if I got a little sliver of a hint just to point me in the right direction! I'm trying to answer this question:

Write a C++ program with a nested loop structure that displays a pyramid/ isosceles triangle in "*".

And so far I've only been able to make an equilateral triangle like this:

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

Right now I'm trying to make the spaces to make it a pyramid. Here's my current code:

```#include <iostream>
using namespace std;
int main ()
{
int i, j;
for (int i=0; i<16; i+=2)
{
for (int j=0; j<=i; j++)
{
cout << "*";
}
cout << endl;
}
system ("pause");
return 0;
}

```

I've been thinking that I need to add a third variable 'k' to determine the spaces, but I'm not sure where to put its statement in my for loop or how I get it to interact with the other variables (or if I need it to interact with the other ones at all)! So far when I try to add variable k with a statement like:
``` for (int k=8; k>=0; k--)
{
cout << " ";
}
```

I usually get never-ending *s! I once got a rectangle, but I can't remember how I got that now...
Any hints would be awesome, no spoilers on the code please! I really want to learn how to do this! Even a hint on how to make it right justified would be uber-awesome! Please and thanks!

- C++ newb.

Is This A Good Question/Topic? 0

## Replies To: Pyramid of *s!

### #2 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: Pyramid of *s!

Posted 04 September 2011 - 07:52 PM

Hint: Using smaller triangles for reference, let's look at the pattern:

```  *    <- 2 spaces
***   <- 1 space
*****  <- no spaces

*    <- 3 spaces
***   <- 2 space
***** <- 1 space
******* <- no spaces

```

so on and so forth
Was This Post Helpful? 1

### #3 Gevito236

• New D.I.C Head

Reputation: -4
• Posts: 9
• Joined: 24-December 09

## Re: Pyramid of *s!

Posted 04 September 2011 - 07:54 PM

Well... I wish I had my C++ compiler on this new computer right now to verify but it looks like your new loop should fit in your first loop but not your second.
Was This Post Helpful? 0

### #4 Gevito236

• New D.I.C Head

Reputation: -4
• Posts: 9
• Joined: 24-December 09

## Re: Pyramid of *s!

Posted 04 September 2011 - 08:04 PM

Gevito236, on 04 September 2011 - 08:54 PM, said:

Well... I wish I had my C++ compiler on this new computer right now to verify but it looks like your new loop should fit in your first loop but not your second.

Scratch that. That's partially true... find a way to make that k go down from 8 to 7 once you are done with your first line so it can make 1 less space the next time. Maybe get the "k" to relate to another variable that goes down an interval in the outside loop at the end. outsideVariable-- then int k = outsideVariable (which went from 8 to 7) and so on. Hope that makes sense
Was This Post Helpful? 1

### #5 Adak

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Pyramid of *s!

Posted 05 September 2011 - 01:32 AM

Consider the job here - you're to make 8 rows (or some number of rows), with up to a maximum of 16 *'s (or some maximum number of *'s). The exact number don't change the logic you need.

Your outer loop (usually a for loop is easiest), controls the rows. The inner for loop (again, usually best), controls what is printed on any ONE particular row - or in other words, it controls what's printed on any column, of the row.

```01	#include <iostream>
02	using namespace std;
03	int main ()
04	{
05	     int i, j;
//the variable i is already declared, so remove the int below
//You want 16 rows?
06	     for (int i=0; i<16; i+=2)  //your row control loop.
07	     {
//same here, j is already declared, remove the int
08	            for (int j=0; j<=i; j++) //your column control loop.
09	            {
10	                cout << "*";
11	            }
12	            cout << endl;
13	    }
14	system ("pause");
15	return 0;
16	}

```

Think about the above, and try to work it out by hand several times. You'll see the pattern of logic that you need.

This post has been edited by Adak: 05 September 2011 - 01:35 AM

Was This Post Helpful? 1

### #6 C++ newb

• New D.I.C Head

Reputation: 0
• Posts: 27
• Joined: 04-September 11

## Re: Pyramid of *s!

Posted 11 September 2011 - 01:34 AM

Gevito236, on 04 September 2011 - 08:04 PM, said:

Gevito236, on 04 September 2011 - 08:54 PM, said:

Well... I wish I had my C++ compiler on this new computer right now to verify but it looks like your new loop should fit in your first loop but not your second.

Scratch that. That's partially true... find a way to make that k go down from 8 to 7 once you are done with your first line so it can make 1 less space the next time. Maybe get the "k" to relate to another variable that goes down an interval in the outside loop at the end. outsideVariable-- then int k = outsideVariable (which went from 8 to 7) and so on. Hope that makes sense

AH! That is genius! I never thought to CREATE a new variable that K could relate to! Your advise is awesome!!! Thank-you sooooo much! I shall try that right away!
Was This Post Helpful? 0

### #7 C++ newb

• New D.I.C Head

Reputation: 0
• Posts: 27
• Joined: 04-September 11

## Re: Pyramid of *s!

Posted 11 September 2011 - 01:43 AM

Adak, on 05 September 2011 - 01:32 AM, said:

Consider the job here - you're to make 8 rows (or some number of rows), with up to a maximum of 16 *'s (or some maximum number of *'s). The exact number don't change the logic you need.

Your outer loop (usually a for loop is easiest), controls the rows. The inner for loop (again, usually best), controls what is printed on any ONE particular row - or in other words, it controls what's printed on any column, of the row.

```01	#include <iostream>
02	using namespace std;
03	int main ()
04	{
05	     int i, j;
//the variable i is already declared, so remove the int below
//You want 16 rows?
06	     for (int i=0; i<16; i+=2)  //your row control loop.
07	     {
//same here, j is already declared, remove the int
08	            for (int j=0; j<=i; j++) //your column control loop.
09	            {
10	                cout << "*";
11	            }
12	            cout << endl;
13	    }
14	system ("pause");
15	return 0;
16	}

```

Think about the above, and try to work it out by hand several times. You'll see the pattern of logic that you need.

Thanks so much for clearing up some confusion (like why I was adding int before the variables after I'd already declared them- saw a classmate do it because he saw someone else do it so I thought we had to do it too, hur hur hur). And yeah I played around with my column and row loops to get what I've got, but thanks for concreting my understanding!
Was This Post Helpful? 0

### #8 C++ newb

• New D.I.C Head

Reputation: 0
• Posts: 27
• Joined: 04-September 11

## Re: Pyramid of *s!

Posted 14 September 2011 - 04:32 AM

Okay, I am almost close to simply giving up on this question! I have tried to look at the code in different angles and have tried with different types of logic, but I simply cannot fit the spaces into my for loop! WHY?! I don't know what to do anymore!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }