Tackling a Difficult Triangle Exercise

  • Context: MHB 
  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Exercise Triangle
Click For Summary
SUMMARY

This discussion focuses on modifying a C++ program to output a right triangle based on user-defined parameters. The program requires the user to specify both the character (triangleChar) and the height (triangleHeight) of the triangle. Participants emphasized the importance of using nested loops to achieve the desired output, iterating through lines and columns to print the specified character with appropriate spacing. Understanding loops, particularly for loops, is crucial for successfully implementing these modifications.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of nested loops
  • Basic input/output operations in C++
  • Familiarity with character data types
NEXT STEPS
  • Learn about C++ for loops and their syntax
  • Explore nested loop examples in C++
  • Practice modifying existing C++ programs for user input
  • Study algorithms for generating patterns in programming
USEFUL FOR

Beginner C++ programmers, educators teaching programming concepts, and anyone interested in learning how to manipulate loops for pattern generation.

MMOne
Messages
3
Reaction score
0
I am having a very difficult time comprehending this exercise and any direction would be appreciated. Feel completely lost!
Not necessarily asking for the answer just want to know how to tackle this.

This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar.

(1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character.

(2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches triangleHeight. Output a space after each user-specified character, including a line's last user-specified character.

#include <iostream>
using namespace std;

int main() {
char triangleChar = '-';
int triangleHeight = 0;

count << "Enter a character: " << endl;
cin >> triangleChar;

count << "Enter triangle height: " << endl;
cin >> triangleHeight;

count << "*" << " " << endl;
count << "*" << " " << "*" << " " << endl;
count << "*" << " " << "*" << " " << "*" << " " << endl;

return 0;
}
 
Technology news on Phys.org
If you have never seen programs with loops (in particular, [m]for[/m] loops), then it's understandable that you feel lost. But then the right way to go about it is to read your textbook or lecture notes to see examples of programs that do something repeatedly. If, on the other hand, you have seen and understood loop examples, then your confusion is less justifiable. What could be easier than to iterate over lines from 1 to triangle height and within each line iterate over columns from 1 to ... and print a symbol at every step? For starters, can you figure out what should be substituted for ... in the previous sentence?

The way I think about programming is you have a number of boxes, in this case three. The first contains the current line number, the second, the current column number, and the third, the triangle height. Imagine that you are almost blind and cannot see what is being printed. The idea is that the computer cannot rely on a powerful visual system humans have, which is able to find intricate patterns in what we see. Besides, you don't have a large memory, so you can only remember one or two numbers at any moment. You can open a box, see which number is stored there and do something (such as print a star or a newline) depending on whether this number is 0, or greater than some constant, or some other condition. You may also change the number, put it back in the box and close it. Now devise an algorithm for yourself how to act in order to print the required triangle.
 

Similar threads

Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 14 ·
Replies
14
Views
34K