What is wrong with my program?

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace 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";

    }

}
Last edited on
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;
}
Last edited on
So far I have been able to do the first two pattern, but I am still having trouble doing the third one



  #
  ##
 ###
 ####
#####



Here is my updated code. If you can give me any hints on making the pattern above I would really appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


#include <iostream>

using namespace 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";
    }






    }







Last edited on
closed account (48T7M4Gy)
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.
Thank you, I have figured it out now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21




   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?

Here is my complete code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

using namespace 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";
        }


}
closed account (48T7M4Gy)
It runs OK using the gear wheel here. Maybe you need to clean and rebuild on your other system.

Nothing stands out to my eye except you really should have a "return 0;" at the end.

Congratulations on getting the third 'diagram'.
Topic archived. No new replies allowed.