Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 21st, 2012, 08:13 AM
 InfinityCounter
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 19 m 53 sec
Reputation Power: 0
Mario's Pyramid

I have been trying to use c to create a half pyramid that is aligned to the right, like the one in mario, except like this.

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

Except my code does not produce that can you guys help me with my code see, if theres anything wrong. I have 2 different versions.

1.
#include <stdio.h>
int main (void)
{
int height;
printf("please enter a positive integer no more than 23:")
scanf("%i", &height);

while(height > 23|height <=0)
{
printf("please enter a positive integer no more than 23:")
scanf("%i", &height);
}
for ( int y= 9- height; y<height;y++)
{
printf(" ");
}
for ( int x= 9- height; x<height;x++)
{
printf("#");
}
printf("\n");
return 0;
}

2. The other is

#include <stdio.h>
int main (void)
{
int height;
printf("please enter a positive integer no more than 23:")
scanf("%i", &height);

while(height > 23|height <=0)
{
printf("please enter a positive integer no more than 23:")
scanf("%i", &height);
}
for ( int y= 9- height; y<height;y++)
{
printf(" ");
printf("#");
printf("\n");
height= (height-1);
}
return 0;
}

so which code do you think is better? And how could i fix the problems and flaws.

#2
December 22nd, 2012, 03:34 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,117
Time spent in forums: 1 Month 3 Weeks 2 Days 3 h 10 m 4 sec
Reputation Power: 455
Compilers are quite accurate. If your compiler tells you you need a semi-colon between printf and scanf statement, insert one.

Or is a good concept. You need logical or || instead of the bitwise or which has different precedence.
Code:
```(height > 23|height <=0)  /* wrong */

((height < 1) || (23 < height)) /* correct */```

Happy holiday!
__________________
[code]Code tags[/code] are essential for python code!

#3
December 29th, 2012, 11:02 PM
 InfinityCounter
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 19 m 53 sec
Reputation Power: 0
Thanks, i managed to complete it!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Mario's Pyramid