Reputation: 125
I want to make a program for an exercise in the CS50 course that asks the user to input the height of the figure and then makes a figure that is similar to:
#
##
###
####
#####
######
#######
########
considering the user inputs height 8.
I have the code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int height;
do
{
height = get_int("Enter The Height: ");
}
while (!(2 <= height && height<=8));
for (int row = 1; row <= height; row++)
{
for (int column = 1; column <= height; column++)
{
printf("#");
}
printf("\n");
}
}
but it prints this:
###
###
###
when I give height 3.
I am a beginner in the C language and so won't understand the advanced concepts, so I request a simple explanation, please.
Upvotes: 1
Views: 55
Reputation: 64700
Shorter is better.
#include <stdio.h>
int main(void) {
int height = 5;
char blocks[height];
memset(blocks, '#', height);
for(int i=0; i<height; ++i)
{
printf("%*.*s\n", height, i+1, blocks);
}
return 0;
}
Upvotes: 3
Reputation: 311048
Here you are.
#include <stdio.h>
int main(void)
{
const char c = '#';
while ( 1 )
{
printf( "Enter The Height (0 - exit): " );
int n;
if ( scanf( "%d", &n ) != 1 || n <= 0 ) break;
putchar( '\n' );
for ( int i = 0; i < n; i++ )
{
printf( "%*c", n - i, c );
for ( int j = 0; j < i; j++ ) putchar( c );
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter The Height (0 - exit): 10
#
##
###
####
#####
######
#######
########
#########
##########
Enter The Height (0 - exit): 9
#
##
###
####
#####
######
#######
########
#########
Enter The Height (0 - exit): 8
#
##
###
####
#####
######
#######
########
Enter The Height (0 - exit): 7
#
##
###
####
#####
######
#######
Enter The Height (0 - exit): 6
#
##
###
####
#####
######
Enter The Height (0 - exit): 5
#
##
###
####
#####
Enter The Height (0 - exit): 4
#
##
###
####
Enter The Height (0 - exit): 3
#
##
###
Enter The Height (0 - exit): 2
#
##
Enter The Height (0 - exit): 1
#
Enter The Height (0 - exit): 0
As for your code then you are always outputting the same number of the symbol '#'
with no indentations.
for (int column = 1; column <= height; column++)
{
printf("#");
}
You can change your loops the following way
for (int row = 1; row <= height; row++)
{
for (int column = 1; column <= height; column++)
{
putchar( column < height - row + 1 ? c2 : c1 );
}
printf("\n");
}
where c1
is equal to '#'
and c2
is equal to ' '
.
Here is another demonstrative program
#include <stdio.h>
int main(void)
{
const char c1 = '#', c2 = ' ';
while ( 1 )
{
printf( "Enter The Height (0 - exit): " );
int height;
if ( scanf( "%d", &height ) != 1 || height <= 0 ) break;
putchar( '\n' );
for (int row = 1; row <= height; row++)
{
for (int column = 1; column <= height; column++)
{
putchar( column < height - row + 1 ? c2 : c1 );
}
printf("\n");
}
putchar( '\n' );
}
return 0;
}
Its output might look like
Enter The Height (0 - exit): 9
#
##
###
####
#####
######
#######
########
Enter The Height (0 - exit): 0
Upvotes: 2