Using only do-while loops to make an inverted equilateral triangle

closed account (9G3v5Di1)
Write a program using DO WHILE statement that displays an inverted equilateral triangle using asterisks. No asterisk should be adjacent to one another. The height will depend on the user input.

Hint: You can use an IF statement to eliminate the initial space for the first row.

I was wondering the whole time if one of my increment/decrement placing is wrong. Everytime I debug, 1st iteration occurs and then next is a non-stop spacing.

My codes are here:

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
#include <limits>
#include <cstring>
#include <float.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "_pause.h"

using namespace std;

int main(){
    int n;

    cout << "Enter a number: ";
    cin >> n;

    int y = 0;
    do{
        int x = n - y;
        do{
            cout << "*" << " ";
            x--;
        }while(x > 0);
        int s = 0;
        do{
            cout << " ";
            ++s;
        }while(s>0);
        cout << endl;
        y++;
    }while(y <= n);

    std::cin.get();
    _pause();
	return 0;
}


Been solving this for 3hours already.. :(

My output:

* * * * *
-infinite space-
Last edited on
Hello goldwinbryanr,

Welcome to the forum.

Given this bit of code:
1
2
3
4
5
6
do
{
	cout << " ";
	++s;
} while (s>0);

You will notice that if "s" started at zero line 4 adds one to "s" before the while condition is checked, so "s" will always be greater then zero and you will have an endless loop.

You need to do something with the while condition so "s" has a place to stop.

Hope that helps,

Andy
closed account (9G3v5Di1)
I switched the two do-while's from

1
2
3
4
5
6
7
8
9
10
int x = n - y;
        do{
            cout << "*" << " ";
            x--;
        }while(x > 0);
        int s = 0;
        do{
            cout << " ";
            ++s;
        }while(s>0);


to

1
2
3
4
5
6
7
8
9
10
int s = 0;
        do{
            cout << " ";
            ++s;
        }while(s<=y);
        int x = n - y;
        do{
            cout << "*" << " ";
            x--;
        }while(x > 0);


and the output became

* * * * *
 * * * *
  * * *
   * *
    *
     *


where did that last line came from?
Last edited on
You have the right idea but you loops needs a few modifications and the locations needs to be changed:
* You are also able to use if statements. *

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
#include <limits>
#include <cstring>
#include <float.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>


using namespace std;

int main() {
	int n;

	cout << "Enter a number: ";
	cin >> n;

	int x, s; 
	int y = 0;

	do {

		x = n - y;
	    s = 0;
		if (y != 0) {
			do {
				cout << " ";
				++s;
			} while (s < y);
		}

		if (x != 0) {
			do {
				cout << "*" << " ";
				x--;
			} while (x > 0);
		}


		cout << endl;
		y++;
	} while (y <= n);

	std::cin.get();
	system("pause"); 
	return 0;
}


Enter a number: 10
* * * * * * * * * *
 * * * * * * * * *
  * * * * * * * *
   * * * * * * *
    * * * * * *
     * * * * *
      * * * *
       * * *
        * *
         *

Press any key to continue . . .
Last edited on
closed account (9G3v5Di1)
btw thank you for welcoming me Mr. Andy.

thank you so much for the codes Mr. tibrado, will try to understand this one! But is there any ways to fix that last line without using if statements?
Last edited on
Yep instead of less than zero. It should be less than or equal to zero.
1
2
3
4
		do {
				cout << "*" << " ";
				x--;
			} while (x >= 0 );
Hello goldwinbryanr,

You are welcome. Glad you got something out of it.

Andy
closed account (9G3v5Di1)
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
#include <limits>
#include <cstring>
#include <float.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "_pause.h"

using namespace std;

int main(){
    int n;

    cout << "Enter a number: ";
    cin >> n;

    int y = 0;
        do{
        int s = 0;
        do{
            cout << " ";
            ++s;
        }while(s<=y);
        int x = n - y;
        do{
            cout << "*" << " ";
            x--;
        }while(x >= 0);
        cout << endl;
        y++;
    }while(y <= n);

    std::cin.get();
    _pause();
	return 0;
}


output went


Enter a number: 5
* * * * * *
 * * * * *
  * * * *
   * * *
    * *
     *
Press any key to continue . . . . .


how do I eliminate the first row using if statement?
Last edited on
closed account (9G3v5Di1)
Also @tibrado I want to know what are the use/meaning of each variable used in your codes to make it clear for me.. Sorry I am just a beginner..

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
int main() {
	int n;

	cout << "Enter a number: ";
	cin >> n;

	int x, s; 
	int y = 0;

	do {
		x = n - y;
	    s = 0;
		if (y != 0) {
			do{
				cout << " ";
				++s;
			} while(s < y);
		}

		if (x != 0) {
			do {
				cout << "*" << " ";
				x--;
			}while(x > 0);
		}
		cout << endl;
		y++;
	}while(y <= n);
Last edited on
Be a little more specific. The code above is the same code you displayed. Only difference was the order of your loops.
closed account (9G3v5Di1)
thank you so much.. I just need to understand well the codes you've gave me. this helped a lot!
Topic archived. No new replies allowed.