Question

In: Computer Science

(b) Postcard from Giza (actually: a Pyramid) It is quite possible that programming to generate rectangles...

(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.

  • The algorithm should provide the user an opportunity to try the shape generator program again or quit.

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.

Solutions

Expert Solution

#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;
}


Related Solutions

(R programming) Generate 50 samples from a Poisson distribution with lambda to be 2 and define...
(R programming) Generate 50 samples from a Poisson distribution with lambda to be 2 and define the log likelihood function Use optimization to find the maximum likelihood estimator of lambda. Repeat for 100 times using forloop. You will need to save the results of the estimated values of lambda.
a) Using the programming tool of your choice generate 10 random numbers from a flat distribution...
a) Using the programming tool of your choice generate 10 random numbers from a flat distribution between -0.5 and 0.5, and find the mean of these 10 numbers. Consider this mean to be the ‘result’ of this procedure. b) Repeat this 10 times and calculate the mean and variance of your 10 results. Is the distance of the mean from 0 about what you would expect? Why? c) Now repeat it 100 times and calculate the mean and variance. Is...
Java Programming Question The problem is to count all the possible paths from top left to...
Java Programming Question The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.Input: The first line of input contains an integer T, denoting the number of test cases. The first line of each test case is M and N, M is number of rows and N is number of columns.Output: For each test case, print the...
r programming generate 100 samples of size n= 5 from a normal random variable with u=2,...
r programming generate 100 samples of size n= 5 from a normal random variable with u=2, o= 3 in r
Please dont copy from other answers. Do as simole as possible (C programming). code 1: create...
Please dont copy from other answers. Do as simole as possible (C programming). code 1: create a program that will copy the contents of a text file called (input.txt) to a file called (copied.txt). After the program runs the contents of both files should be the same. (im creating the input manually) code 2: Change the code from fist part so that instead of always copying from input.txt to copied.txt it instead asks the user to provide both file names....
(a) Generate n=10 random sample from a population with normal(40,1) distribution. (b) For data in part...
(a) Generate n=10 random sample from a population with normal(40,1) distribution. (b) For data in part (a) find a 95% confidence interval for mu (the mean of the population). Can I claim that the mean is 41? If your answer is "yes" to this question, which one is the mean? 40 or 41? (c) For data in part (a) find a 99% confidence interval for sigma (the standard deviation of the population).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT