How to Create Patterns in C++ with For-Loops?

  • Context: C/C++ 
  • Thread starter Thread starter RoughRoad
  • Start date Start date
  • Tags Tags
    C++ Patterns
Click For Summary

Discussion Overview

The discussion revolves around creating patterns in C++ using for-loops, specifically focusing on generating a triangular pattern of asterisks. Participants explore the algorithmic approach to achieve this, share their experiences, and clarify programming concepts related to output formatting.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant asks how to create a specific pattern using two for-loops and requests an explanation of the algorithm.
  • Another participant explains that the external loop controls the number of lines, while the internal loop handles the number of asterisks printed on each line.
  • A participant expresses difficulty in achieving the desired output, stating they only get a horizontal line of asterisks.
  • Another participant suggests using 'cout << endl;' to manage line breaks in the output.
  • A participant discusses how including 'endl' in the output statement affects the formatting of the printed asterisks, providing examples of different outputs based on the placement of 'endl'.
  • One participant advises on the importance of outlining logical steps before coding, introducing the concept of "pseudo code" to clarify the thought process behind programming tasks.

Areas of Agreement / Disagreement

Participants generally agree on the need for a structured approach to programming and the role of loops in generating patterns. However, there is no consensus on the specific implementation details, as some participants face challenges in achieving the correct output.

Contextual Notes

Some participants mention issues related to output formatting and the use of 'endl', indicating that understanding these concepts is crucial for successfully creating the desired pattern. The discussion does not resolve the specific programming challenges faced by participants.

Who May Find This Useful

Individuals interested in learning C++ programming, particularly beginners seeking to understand loops and output formatting, may find this discussion beneficial.

RoughRoad
Messages
63
Reaction score
0
How to create patterns using C++? I am new to programming and would really appreciate some help in creating patterns. For example, how will you create this pattern using two for-loops?

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


And btw, this is not a homework question. Was just curious. Thanks. And please also explain the algorithm for creating such patterns. Thanks!
 
Technology news on Phys.org
External loop is responsible for the number of lines printed, internal loop is responsible for printing stars in each line.
 
I tried, but all I get is a horizontal line of asterisks. what can be wrong?
 
cout<<endl;
 
I included endl in the same count statement, like this:

count <<"*"<<endl;

does that make any difference?
 
endl ends the line - if there is no endl added, all asterisks will land in one line:

Code:
cout << "*";
cout << "*";
cout << "*";
cout << "*";

yields

****

while

Code:
cout << "*";
cout << "*";
cout << endl;
cout << "*";
cout << "*";

yields

**
**

Code:
cout << "*" << endl;

and

Code:
cout << "*";
cout << endl;

are equivalent.
 
RoughRoad, when you are trying to do something with a program is is best to follow the old adage "if you don't know how to do it without a computer, then you don't know how to do it WITH a computer". The implication of this is that you should lay out a set of clearly defined, logical steps that do what you want. Then implementing those steps in a computer language should be trivial.

For example:

step 1: set n=1
step 2: start a new line
step 3: print n asterisks in a row
step 4: increment n
step 5: if n > 10 then stop
step 5: go back to step 1

EDIT: these steps, by the way, are called "pseudo code", which basically just means "I'm not really computer code but I'm a consistent, logical, set of steps written in English so I'm pretending to be computer code until somebody decides on a language and turns me into REAL computer code"
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
81
Views
8K
  • · Replies 60 ·
3
Replies
60
Views
8K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K