This is prefaced by my top comment.
printf
does not "know" how many spaces to print. Your printf(" ");
will print one space. But, it is called in a loop. So, the number of spaces is the number of loop iterations.
Here's a refactored version of your program that shows various height
values and the number of spaces they print. I've replaced " "
with "#"
so they can be seen.
Also, I believe you want printf("\n");
after the end of the inner loop. That allows it to generate a pyramid like output.
Anyway, here's the code:
#include <stdio.h>
//#include <cs50.h>
int
main(void)
{
int height;
#if 0
do {
printf("Size:\n");
scanf("%d",&height);
//height = get_int("Size: \n");
} while (height < 1 || height > 50);
#endif
for (height = 0; height <= 20; ++height) {
if (height > 0)
printf("\n");
printf("height: %d\n",height);
int count = 0;
for (int row = 1; row < height + 1; row++) {
for (int space = height - row; space > 0; space--) {
#if 1
printf("#");
#endif
++count;
}
printf("\n");
}
printf("count: %d\n",count);
}
return 0;
}
Here is the output of the program:
Note that the program always prints a blank line at the end. This is because you're going one iteration too far.
height: 0
count: 0
height: 1
count: 0
height: 2
#
count: 1
height: 3
##
#
count: 3
height: 4
###
##
#
count: 6
height: 5
####
###
##
#
count: 10
height: 6
#####
####
###
##
#
count: 15
height: 7
######
#####
####
###
##
#
count: 21
height: 8
#######
######
#####
####
###
##
#
count: 28
height: 9
########
#######
######
#####
####
###
##
#
count: 36
height: 10
#########
########
#######
######
#####
####
###
##
#
count: 45
height: 11
##########
#########
########
#######
######
#####
####
###
##
#
count: 55
height: 12
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 66
height: 13
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 78
height: 14
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 91
height: 15
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 105
height: 16
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 120
height: 17
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 136
height: 18
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 153
height: 19
##################
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 171
height: 20
###################
##################
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 190