Tackling a Difficult Triangle Exercise

  • Context:
  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Exercise Triangle
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 5K views
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;
}
 
Physics 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.