In: Computer Science
(b) Postcard from Giza (actually: a Pyramid)
It is quite possible that programming to generate rectangles using loops is a piece of cake for you. Here is another programming challenge to see how well you can use the for loop, so now we want you to draw a "Postcard from Giza (Links to an external site.)" i.e. create a view of a famous location: a single side of a hollow pyramid.
You are to write an interactive program that asks the user about a single pyramid's width, a positive integer representing the size of its base and then creates a picture like the one below. Use repetition control structures better known as "loops" to output a hollow pyramid shape based on the width specified by the integer. If the input is an even number, it should be increased to the next odd number.
Don't cheat! Don't use the setw manipulator or output hard-coded
strings containing a rigid number of asterisks and spaces – all
elements must be created dynamically using loops!
Enter a value to represent the base of a hollow pyramid (not to exceed 80): 11 * * * * * * * * * *********** |
Notice the specific character * (an asterisk) used to generate the hollow pyramid.
When thinking of how to generate a somewhat complex pattern such as the one above using loops we can begin by looking at the pyramid displayed and asking a few questions. Here are a few you might think about.
How is the integer value input of 11 used in this pyramid
display? Answer: Used in a loop to generate a line of asterisks for
the base of the pyramid.
Can the integer value of 11 be used to determine the number of rows
needed to be generated? Answer: A possible formula here might be
11/2+1= 6 rows
How can the integer value of 11 be used to determine the number of
outer whitespaces to be generated for each row? Answer: 11/2=5
outer whitespaces for the first row. Determine how to come up with
outer whitespaces for the remaining rows as this will be helpful in
generating the left slope of the pyramid.
How can the integer value of 11 be used to determine the inner
whitespaces to be generated for each row? Answer: Inner whitespace
for second row - 11-8 (outer whitespace*2) - 2 (left and right *
characters) = 1 whitespace. This sort of study of the pattern
progressing through each row will help in generating the right
slope of the pyramid.
Remember, there may be multiple ways to complete a programming challenge, and the algorithm here may not necessarily be the only, or even the best, way. Implement the algorithm that you understand and works best for.
Your task is to achieve the following goals:
ask the user to enter a positive integer value greater than 4 (n) to represent the width or base of the pyramid; This integer value should not be used to represent the rows needed to generate the pyramid, make sure you get this correct. If the input is an even number, it should be increased to the next odd number. The odd number width is needed to ensure that the pyramid has a single tip.
check the entered value and if it isn't legal, output a message stating the reason;
draw a hollow pyramid using the character * (an asterisk) with a base of n (see sample output above)
check to make sure the number of inner whitespaces in each row starting with the second row is an odd number. So for the above sample pyramid with a base of 11, the number of inner whitespaces should be Row2=1, Row3=3, Row4=5 and Row5=7.
find a reasonable upper limit for the width or base (depending on your screen's dimensions or an acceptable range) and embed it into your input validation code.
Input Validation: Ensure that the user inputs a positive integer value greater than 4 and does not exceed an acceptable range determined by you. Do not accept negative or non-numeric values.
Use meaningful variable names, nested loop statements with proper indentation, appropriate comments, and good prompting messages.
Here us a sample test run of the Postcard from Giza program with some user input validation routines.
Enter a value to represent the base of a hollow pyramid (not to exceed 20): four OOPS! Looks like you typed some bad data here! The acceptable base for the pyramid should range from 5 to 20... Enter a value to represent the base of a hollow pyramid (not to exceed 20): 4 OOPS! Looks like you typed some bad data here! The acceptable base for the pyramid should range from 5 to 20... Enter a value to represent the base of a hollow pyramid (not to exceed 20): 11 ************** A POSTCARD ************** ********* ********* ********* * * * * * * * * * *********** ********** A PYRAMID FROM GIZA ******** Process returned 0 (0x0) execution time : 12.188 s Press any key to continue. |
#include <stdio.h>
int main() {
int i, j, rows,num;
printf("Enter a value to represent the base of a hollow pyramid
(not to exceed 20) ");
scanf("%d", &num);
if(num>0 & num>4 &num<=20)
{
printf("valid input continue\n");
if(num%2==0)
num=num+1;
rows=(num/2)+1;
printf("************** A POSTCARD
**************\n*********\n*********\n*********\n");
for(i=1; i<=rows; i++)
{
/* Print trailing spaces */
for(j=i; j<rows; j++)
{
printf(" ");
}
/* Print hollow pyramid */
for(j=1; j<=(2*i-1); j++)
{
/*
* Print star for last row (i==rows),
* first column(j==1) and for
* last column (j==(2*i-1)).
*/
if(i==rows || j==1 || j==(2*i-1))
{
printf("*");
}
else
{
printf(" ");
}
}
/* Move to next line */
printf("\n");
}
printf("********** A PYRAMID FROM GIZA ********");
}
else{
printf("OOPS! Looks like you typed some bad data here! \nThe
acceptable base for the pyramid should range from 5 to 20..\n
");
}
return 0;
}