MHB Tackling a Difficult Triangle Exercise

  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Exercise Triangle
AI Thread Summary
The discussion centers around modifying a program to output a right triangle using a user-specified character instead of a fixed '*'. The original program prompts the user for a character and a triangle height but only outputs a static triangle. Participants emphasize the importance of understanding loops, particularly nested loops, to achieve the desired output. The key steps involve iterating through lines from 1 to the specified height and printing the user-defined character, ensuring to include a space after each character. For those struggling, it's recommended to review textbook examples of loops and to conceptualize the problem in terms of managing variables for line and column numbers. The discussion encourages developing an algorithm to systematically approach the triangle printing task, highlighting the need for logical thinking in programming.
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;

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

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

cout << "*" << " " << endl;
cout << "*" << " " << "*" << " " << endl;
cout << "*" << " " << "*" << " " << "*" << " " << 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top