Reputation: 15
Goal:
Implement a program that prints out a double half-pyramid of a specified height, per the below.
$ ./mario
Height: 4
# #
## ##
### ###
#### ####
Problem:
Have been messing with the code for the past 3 hours or so. I got the first line to work, but everything beyond that was just 2 parallel pillars consisting of a singular hash mark. At present, the program is indefinitely printing a hash mark.
Code:
int main (void) {
// user gives height value for pyramid
// h = height
int h;
do {
printf (
"How tall do you want your pyramid to be? Please type a number "
"between 0 and 23.\n");
h = get_int ();
} while (h < 0 || h > 23);
// Left side of pyramid says spaces first, followed by hashes. Two spaces
// inbetween sides of the pyramid. Right side renders hashes first, then
// inputs newline character.
// r = row
// s = space
// p = hash
// q = hash right
int q;
int p;
int s;
int r;
for (r = 0; r < h; r++) {
do {
do {
printf (" ");
s++;
} while (s < h - r);
// says spaces
do {
printf ("#");
p++;
} while (p < r++);
// says left hashes
// always print 2 spaces inbetween
printf (" "); // two spaces
do {
printf ("#");
q++;
} while (q < r++);
// says right hashes
printf ("\n");
q = 0;
p = 0;
s = 0;
// reset q,p,s values
} // end bracket of encompassing 'do' function
while (r < h);
//'for' function should execute once, increment 'r' by 1, and repeat
//until row height becomes height provided by user.
} // end bracket of for loop
} // end bracket of int main
Expected Result:
Program creates pyramid formed by hash symbols and spaces.
Actual Result:
Infinitely looping print function.
Upvotes: 0
Views: 89
Reputation: 28850
A shorter implementation, for your study
int i,j;
for (i=0 ; i<h ; i++) { // h lines
for(j=0 ; j < h+2+i+1 ; j++) { // cover all characters on 1 line
printf("%s", j<h-i-1 || j<h+2 && j>=h ? " " : "#");
}
printf("\n"); // end of line
}
Regarding j<h-i-1 || j<h+2 && j>=h ? " " : "#"
j<h-i-1
spaces before the first #
j<h+2 && j>=h
spaces between the #
s?:
operator: C ? A : B
translates to if C is true give A otherwise give B Upvotes: 1
Reputation: 140276
the end of your loop is like:
p++;
}
while (p < r++);
so r
is always ahead of p
because you're incrementing both. That's your infinite loop.
I get a pretty good result moving r++
after the second while
inner loop, like this:
# #
# #
## ##
### ###
so still a bit of tuning and you're here. The other issue is with do/while
which execute once even if the condition is false. Replace by while
loops or you're one off like above:
do
{
while (s < h - r)
{
printf(" ");
s++;
}
//says spaces
while (p < r)
{
printf("#");
p++;
}
//says left hashes
//always print 2 spaces inbetween
printf(" ");
//two spaces
while(q < r)
{
printf("#");
q++;
}
//says right hashes
r++;
printf("\n");
q = 0;
p = 0;
s = 0;
// reset q,p,s values
}//end bracket of encompassing 'do' function
while(r < h);
Note that it's also cheap to autoindent your code using "Notepad++ / TextFX / TextFX edit / Reindent C++ code"
# #
## ##
### ###
#### ####
##### #####
###### ######
####### #######
######## ########
######### #########
Upvotes: 1