ASCII ARt triangle

Hey guys I have a problem with the following code, please help.

Write a program that outputs an upside down ASCII-art triangle given the number of lines
that the triangle should span. The triangle should be lled with the * character, tipped with
a V character, the sides should consist of slashes, and the top should consist of underscore (_)
characters. The `background' should consist of dot . characters.
Example program run:
Enter the number of starred lines the triangle should have: 4
_________
\*******/
.\*****/.
..\***/..
...\*/...
....V....
Note that the total number of lines the triangle spans should be two more than the input
number.
So, what's your problem? What have you done? What do you struggle with?
@CheyezaM

The first row is easy: figure out how many underscores you need to print based on the user input.
The last row is similarly easy: figure out how many dots to print, then print a 'V', then print the same number of dots again.

Consider the first and last row as isolated from the rest of the problem. In other words, treat the first and last row as special cases to handle on their own, and consider the middle part separately.

Now your problem becomes: "Based on user input, how do I create this?"
\*******/
.\*****/.
..\***/..
...\*/...


Can you think of an algorithm and code something to produce this?

It may be helpful to define a function like this:
1
2
3
4
5
6
7
8
//printChar('Z', 5); would print ZZZZZ to the screen
void printChar(char charToPrint, int timesToPrint)
{
    for(int i = 0; i < timesToPrint; i++)
    {
        std::cout << charToPrint;
    }
}

so that you may concentrate on what to do in each row and not get lost in a sea of for loops.
Assuming usrInput contains user input, we could handle the last line on its own like this:
1
2
3
4
5
    //print last row
    printChar('.', usrInput);
    std::cout << 'V';
    printChar('.', usrInput);
    std::cout << std::endl;
Last edited on
Topic archived. No new replies allowed.