Hi there, I need help with making a code for an upside triangle. I am suppose to be using functions for example void draw_triangle().
I have a working code for a triangle, but I have no idea how to make an upside down triangle, or a diamond using the same method.
#include <iostream>
usingnamespace std;
void draw_triangle( int size, char ch); //function declaration
int main()
{
int size, i, j;
char ch;
cout << "Please enter your desired size \n";
cin >> size;
cout << "Please enter the character \n";
cin >> ch;
for ( i =1 ; i <= (size) ; i++) //this displays the size.
{
for (j=0; j <(2*size -i); j++) //this displays the spaces.
{
cout << " ";
}
for (j=0; j < 2*i-1; j++) //this displays the character chosen by the user.
{
cout << ch;
}
cout <<endl;
}
\
return 0;
}
Well I am not going to give the exact code as I think that is irrelevant to your problem. Lets say the top of triangle ie the max number of characters is 4... So what is the relationship between the top of the triangle and the bottom of the triangle. Keep in mind this is output (i am guessing) is suppose to look like for the function
* * * *
* * *
* *
*
Now this comes down observing the pattern of the triangle, in the triangle demonstrated in the output all I did was with each row I increased the amount of initially spaces by 1. For example, in the first row there is 0 spaces before the first character, then in the second row there is one space before a character, then in the third row there is 2 spaces before the character and so forth. Conversely, I am also decreasing the amount of characters printed in each line. But, the printing pattern stays the same character then space character then space until the max amount of char are hit... Hopefully, that helps... it should since I pretty much told you how to write the function is merely a matter of translating some bad English into code.