Hello, I am trying to output a specific pattern of hashes in this order (based off of user input)
#
##
###
####
#####
and
#
##
###
####
#####
and
#
##
###
####
#####
The program below is what I have tried so far, I know I have to nest a "for" loop to display the correct pattern, but no matter what I try, it won't seem to display the pattern I want. Any help is appreciated.
#include <iostream>
usingnamespace std;
int main ()
{
int height, n,m,y,x;
cout << "What is the height of the triangle? " << "\n";
cin >> height;
for (n = 1; n <= height; n++)
{
for (m = 1; m <= n; m++)
cout << '#';
cout << "\n";
}
for (y = 1; y <= height; y++)
{
for (x = 1; x <= y ; x++)
cout <<'#';
cout << "\n";
}
}
Here you go use mine as a reference and fool around with it
1 2 3 4 5 6 7 8 9 10 11
int main ()
{
for (int i = 0; i < 6; i++) { // change the number 6 to a variable the user will input
for (int j = 0; j < i; j++) {
cout << "#";
}
if(i > 0) //do not end line for the first iteration
cout << endl;
}
return 0;
}
#include <iostream>
usingnamespace std;
int main ()
{
int height;
cout << "What is the height of the triangle? " << "\n";
cin >> height;
//This loop displays hash pattern starting from the left side
for (int a = 1; a <= height; a++) //outer loop displays columns of first pattern
{
//nested loop displays rows of pattern
for (int b = 1; b <= a; b++)
cout << '#';
cout << "\n";
}
//This loop displays hash pattern starting from the right side
for (int c = 1; c <= height; c++) //outer loop displays columns of second pattern
{
//nested loop displays spaces needed to indent hashes in order to make second pattern
for(int d = height-c; d>0; d--)
cout << " ";
//nested loop displays rows of second pattern
for(int e = 1; e <= c; e++)
cout << '#';
cout << "\n";
}
}
Excellent. You've got the first two patterns. What you need to do for the third is look carefully for the pattern in the blank spaces vs the pattern in the #'s.
for (int c = 1; c <= height; c++)//outer loop displays columns of second pattern
{
//nested loop displays spaces needed to indent hashes in order to make second pattern
for(int d = height - c; d>0; d=d-2)
cout << " ";
//nested loop displays rows of second pattern
for(int e = 1; e <= c; e++)
cout<< '#';
cout << "\n";
}
Hmm something is definitely wrong with my code. When I ran the program in CodeBlocks, it compiled successfully and showed the correct patterns. However when I tried to compile the code in my class's website(hypergrade), it told me there was a run time error because the program ended abnormally. Do you think that there's anything in this program that could cause a run time error or maybe an infinite loop that I haven't seen?
#include <iostream>
usingnamespace std;
int main ()
{
int height;
cout << "What is the height of the triangle? " << endl;
cin >> height;
//The first loop displays hash pattern starting from the left side
for (int a = 1; a <= height; a++) //outer loop displays columns of first pattern
{
//nested loop displays rows of pattern
for (int b = 1; b <= a; b++)
cout << '#';
cout << "\n";
}
//The second loop displays hash pattern starting from the right side
for (int c = 1; c <= height; c++)//outer loop displays columns of second pattern
{
//nested loop displays spaces needed to indent hashes in order to make second pattern
for(int d = height - c; d>0; d--)
cout << " ";
//nested loop displays rows of second pattern
for(int e = 1; e <= c; e++)
cout << '#';
cout << "\n";
}
//The third loop displays pattern in semi-Pyramid form
for (int c = 1; c <= height; c++)//outer loop displays columns of second pattern
{
//nested loop displays spaces needed to indent hashes in order to make second pattern
for(int d = height - c; d>1; d= d-2)
cout << " ";
//nested loop displays rows of second pattern
for(int e = 1; e <= c; ++e)
cout << '#';
cout << "\n";
}
}