In this lab, you will write a program that prints out a pattern based on user given input. You will restrict the user input to any odd numbered integer greater than or equal to 1 and less than or equal to 9. For instance, when the user enters a 9, your program will output: See image.
This program requires you design and implement three functions: get input, is valid, and print pattern, in addition to your main function. Your main function will call get input and print pattern. Your function get input will repeatedly call is valid until the user enters a proper number that passes the validity check described below.
Begin by writing a function named get input to retrieve integer input from the user.
Protip: It is common to implement input request with a user using a do...while loop. This allows you to ask at least once and repeat until the user enters a valid input.
Next, write your function named is valid. This function will be called by your get input function. You will not use a loop in this function.
Protip: To achieve the proper spacing in your print pattern function, you need to explore the "%*s" format code for printf, which gives variable length padding.
Example: printf ("%*s", x, ""); // Prints x number of blank spaces
Next, complete your function named print pattern.
Protip: Do not use a long series of if statements and pre-formatted strings to achieve the variable white space printing. This will be verbose, difficult to follow, inefficient, and not representative of good computer programming. Instead, use the dynamic calculation approach described in the Hint below.
SPOILER ALERT
Hint: You will use two nested loops to accomplish your formatting of the diamond.
The outer for loop will be responsible for the number of rows (lines) necessary and will increment the counter by two each time.
For each row, first print out the correct number of whitespaces. To achieve the variable number of white spaces on the left, use the "%*s" format code. The number of white spaces will be the difference between your user input number and the outer variable counter. This allows you to determine the number of white spaces being printed as a calculation based on the user number.
For example, consider a user input value of n=7 and a row counter i starting at i=1
______1 // i=1, 6 blank characters = n - i
____1 2 3 // i=3, 4 blank characters = n - i
__1 2 3 4 5 // i=5, 2 blank characters = n - i 1 2 3 4 5 6 7 // i=7, 0 blank characters = n - i
Next, your inner while loop will print out the individual numbers on each row. Do not forget to print extra space after each digit.
Do not forgot to print a newline character n at exactly the appropriate place.
You will need a pair of nested loops to print the top half of the diamond, and a second set of nested loops to print to lower half of the diamond. Take care not to reprint the middle line.
Protip: Never use a float or a double as a loop counter variable as you may get an infinite loop. Instead use an int for your loop counter variables.
Test your program! There are a number of choices you may make during your design and implementation. However, you will lose significant points if you do not properly format your diamond as shown.
Protip: Make sure you output what is expected, nothing more and nothing less. Otherwise the automated grading script will subtract points!
The following are optional enrichment exercises that you may chose to implement in order to practice additional skills. You may chose to implement some, all, or none of them.
Protip: If you complete any of the Extra Steps above, make sure your output still matches the expected output given in the Example Executions.