# Creating a ASCII Shapes with nested for loops

Page 1 of 1

## 5 Replies - 17851 Views - Last Post: 26 September 2012 - 08:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293228&amp;s=bac91785a91aefaeed44372034976e3c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Japandabear

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 25-September 12

# Creating a ASCII Shapes with nested for loops

Posted 25 September 2012 - 06:29 PM

I am new to C++ and for my class I have to create different patterns using nested for loops and if else statements. These patterns are:

Solid Square Pattern

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Square Outline Pattern

* * * * *
* *
* *
* *
* * * * *
Solid Triangle Pattern

*
* *
* * *
* * * *
* * * * *
Letter X Pattern

* *
* *
*
* *
* *
Letter Z Pattern

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

The portion I am running into trouble on is creating a solid right triangle out of stars like this:
*
* *
* * *
* * * *
* * * * *

The height of this triangle is determined by user input (5-20).

My code so far is as follows:
```#include <iostream>
using namespace std;

int main()
{
// Declare Variables
int num = 0;

// Ask for user to input a number between 5-20
cout << "Enter a number between 5 and 20\n";
cin >> num;

//Use for and while loops to create  the shapes

// loop for Square Pattern
for (int height=0; height<num; height++)
{
for( int width=0; width<num; width++)
{
cout << " * ";
}
cout << endl;
}
// Adding space between shapes
cout << endl;

// Loop for Square Outline
for (int height=0; height<num; height++)
{

for( int width=0; width<num; width++)
{
if ((width == 0) || (width == num-1))
cout << " * ";
else if ((height == 0) || (height ==num-1))
cout << " * ";
else
cout << "   ";
}
cout << endl;
}

// Loop for the Solid Triangle Pattern

```

The test code I am working with for the triangle problem is:
```for (int height = 0; height<num; height++)
{
for (width = 0; width<num; width++)
{
if ((num-width == num)
cout << endl;
else if (num-height == num-1)
cout << " * ";
}
cout << endl;
}

```

However, I realize that I need to know the user's input in order to make all the if else statements to properly run this code this way. I can't think of any logical expressions that I could right to make a triangle without knowing the users input. Am I missing something simple? Thanks for the help in advance.

Is This A Good Question/Topic? 0

## Replies To: Creating a ASCII Shapes with nested for loops

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Creating a ASCII Shapes with nested for loops

Posted 25 September 2012 - 09:01 PM

Well, I would say that the user input is very much required for the program to run. I mean to say that it wouldn't be logical to print several lines of stars without asking the user as to how many stars he wants.You could probably edit your cout statement to something like this.

```cout<<"Please enter a number [5-20] to print a solid triangle pattern of stars in increasing order
```

Whatever statement I have given above could be modified probably and fed to the user for his input.

Hope this helps

regards,
Raghav

This post has been edited by JackOfAllTrades: 26 September 2012 - 02:41 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 1

### #3 #define

• Duke of Err

Reputation: 1855
• Posts: 6,678
• Joined: 19-February 09

## Re: Creating a ASCII Shapes with nested for loops

Posted 25 September 2012 - 09:02 PM

```Solid Square Pattern

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

Square Outline Pattern

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

Solid Triangle Pattern

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

Letter X Pattern

*       *
*   *
*
*   *
*       *

Letter Z Pattern

* * * * *
*
*
*
* * * * *
```

Japandabear, on 26 September 2012 - 04:29 AM, said:

The portion I am running into trouble on is creating a solid right triangle out of stars like this:

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

The height of this triangle is determined by user input (5-20).

```// Ask for user to input a number between 5-20
cout << "Enter a number between 5 and 20\n";
cin >> num;

```

However, I realize that I need to know the user's input in order to make all the if else statements to properly run this code this way. I can't think of any logical expressions that I could right to make a triangle without knowing the users input. Am I missing something simple? Thanks for the help in advance.

User appears to input to num.
Was This Post Helpful? 0

### #4 Mrk

• D.I.C Head

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: Creating a ASCII Shapes with nested for loops

Posted 26 September 2012 - 06:50 AM

Japandabear, on 25 September 2012 - 07:29 PM, said:

However, I realize that I need to know the user's input in order to make all the if else statements to properly run this code this way. I can't think of any logical expressions that I could right to make a triangle without knowing the users input. Am I missing something simple? Thanks for the help in advance.

Actually, you don't need to know the users input in advance to write your code. You let the users input drive your code. For example, the number of lines in the triangle may be generated with a loop. So the height of the triangle is determined by how many times the loop loops. And the user input sets the loop value:

```user input = cin >> x;

loop x times
{
print a line of stars;
}
```

See how the user input drives the program and controls how many lines are generated and thus controls the height of the triangle? All you need is an algorithm to print each line with the proper amount of stars. And here is one clue: The number of stars in each line is equal to the line number - line 4 has 4 stars, line 92 has 92 stars, etc.

So make an algorithm to print out one line with one star, then line 2 with 2 stars, then line 3 with 3 stars and each successive line with one more star. So on it's own, this algorithm would run forever and generate a triangle of infinite size. However, you then use the user input to control the number of lines that actually get generated.

This post has been edited by Mrk: 26 September 2012 - 06:59 AM

Was This Post Helpful? 3

### #5 Mrk

• D.I.C Head

Reputation: 29
• Posts: 66
• Joined: 03-December 09

## Re: Creating a ASCII Shapes with nested for loops

Posted 26 September 2012 - 07:11 AM

Programming is problem solving. You need to learn how to come up with solutions to solve problems. Once you have a solution in mind, you just use a programming language to implement the solution. If you can't solve the problem, then knowing C++ is of no help, because you won't get a chance to use it until you come up with a solution first.

You learn to recognize patterns and relationships within the problem, i.e., you get to know the problem, like the number of stars on each line is the same as the line number, and that the size of the triangle is determined by the number of lines, etc. Seeking out these patterns and relationships within the problem allows you to figure out how to solve it. There are always patterns in the problem - even if there is no pattern then THAT is the pattern!

This post has been edited by Mrk: 26 September 2012 - 07:17 AM

Was This Post Helpful? 0

### #6 Japandabear

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 25-September 12

## Re: Creating a ASCII Shapes with nested for loops

Posted 26 September 2012 - 08:27 AM

Quote

So make an algorithm to print out one line with one star, then line 2 with 2 stars, then line 3 with 3 stars and each successive line with one more star. So on it's own, this algorithm would run forever and generate a triangle of infinite size. However, you then use the user input to control the number of lines that actually get generated.

Thanks this is what I was not getting. I knew there was a relationship between the height and width, but I could not for the life of me figure it out that late at night.
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; }