Sep 18, 2018 at 9:49am UTC
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 Sep 18, 2018 at 10:15am UTC
Sep 18, 2018 at 10:50am UTC
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 Sep 18, 2018 at 10:51am UTC
Sep 18, 2018 at 10:59am UTC
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 Sep 18, 2018 at 11:00am UTC
Sep 18, 2018 at 11:03am UTC
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 Sep 18, 2018 at 11:04am UTC
Sep 18, 2018 at 11:23am UTC
Hello goldwinbryanr,
You are welcome. Glad you got something out of it.
Andy
Sep 18, 2018 at 11:31am UTC
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 Sep 18, 2018 at 11:32am UTC
Sep 18, 2018 at 12:18pm UTC
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 Sep 18, 2018 at 12:18pm UTC
Sep 18, 2018 at 2:47pm UTC
Be a little more specific. The code above is the same code you displayed. Only difference was the order of your loops.
Sep 19, 2018 at 4:17am UTC
thank you so much.. I just need to understand well the codes you've gave me. this helped a lot!