Tackling a Difficult Triangle Exercise

In summary, the given program outputs a right triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character.
  • #1
MMOne
3
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
  • #2
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.
 

Related to Tackling a Difficult Triangle Exercise

1. What is a difficult triangle exercise?

A difficult triangle exercise is a math problem that involves finding the missing angles or sides of a triangle, using various tools and techniques such as trigonometry, Pythagorean theorem, or special triangle properties. These exercises can be challenging because they require a deep understanding of geometric concepts and problem-solving skills.

2. How do I approach a difficult triangle exercise?

The first step in tackling a difficult triangle exercise is to carefully read and understand the given information. Draw a diagram to visualize the problem and identify any known angles or sides. Then, use the appropriate formulas and theorems to find the missing values. It may also be helpful to break the problem down into smaller, easier-to-solve steps.

3. What are some common mistakes to avoid when solving a difficult triangle exercise?

One common mistake is using the wrong formula or theorem. Make sure to double-check which tools are necessary for the specific problem. Another mistake is not labeling the diagram properly, which can lead to confusion and incorrect solutions. It is also important to pay attention to units and use the correct unit of measurement for angles and sides.

4. What strategies can I use to solve a difficult triangle exercise?

One effective strategy is to work backwards from the given information and use known values to find the missing ones. Another strategy is to use similar triangles to solve the problem. You can also try to use multiple methods to solve the same problem and compare your answers to ensure accuracy.

5. How can I improve my skills in tackling difficult triangle exercises?

Practice is key to improving your skills in solving difficult triangle exercises. Work on a variety of problems and try to understand the underlying concepts rather than just memorizing formulas. You can also seek help from a teacher or tutor if you are struggling with a specific concept. Additionally, reviewing and understanding your mistakes can also help you improve in the future.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
Replies
10
Views
994
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
917
Back
Top