|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e6/solution/p4.c
#include <stdio.h>
/**
* Complete this function. It should use a loop to output length number of
* hash marks. Then output a newline after the loop.
*/
void line(int length)
{
// Just counts out length number of hash marks and prints them:
for(int i=0; i < length; i++) {
printf("#");
}
// Newline is printed when the loop is over:
printf("\n");
}
/**
* Complete the main function such that it prompts for a height and then prints
* numbers 1 through the input height (right aligned to 2 columns) followed by
* a colon (:) and a space, then call the above function to print that same
* number of hash marks.
* Example input/output:
* ./p4
* Height: 5
* 1: #
* 2: ##
* 3: ###
* 4: ####
* 5: #####
*/
int main(void)
{
int h;
printf("Height: ");
scanf("%d", &h);
for(int i=1; i <= h; i++) {
printf("%2d: ",i);
line(i);
}
return 0;
}
|