Overview

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.

Input Function

Begin by writing a function named get input to retrieve integer input from the user.

  • Your function should prompt the user for an odd number between 1 and 9.
  • Recall that "%d" is the code for a decimal integer.
  • You should then call is valid and pass it the user-entered value in as an argument.
  • If the value is invalid, force the user to try again until the number is valid.
  • Your function will return a valid integer: an odd digit, between 1 and 9 inclusive.
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.

Validation Function

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.

  • This function should take an integer as input parameter.
  • Your function will determine if the parameter is an odd number between 1 and 9.
  • You should display a clear message whenever the user enters an invalid number:
    • You should tell the user when the number was even (not odd): You have entered an even number. Please try again.
    • You should tell them when the number is too large ( > 9 ): You have entered a number greater than 9. Please try again.
    • You should tell them when the number is too small ( < 1 ): You have entered a number less than 1. Please try again.
  • If the input is an odd number between 1 and 9, you should return 1 (true).
  • If the input is not an odd number between 1 and 9, you should return 0 (false).
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

Print Function

Next, complete your function named print pattern.

  • This function will take in an integer as an input parameter and print the correct pattern based on this number.
  • You can assume the number is an odd digit, between 1 and 9, since you verified its validity by using the function is valid from within your get input function.
  • The function needs to display your rows of numbers such that they form a diamond as shown in the two Example Executions.
  • You will accomplish this using while loop nested inside a for loop. Try to figure this out on your own before reading the substantial Hint below.
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.

Main Function

  • Your main function will first call get input and save the number returned.
  • Next, call print pattern to display your number pyramid.
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

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!

Extra Steps

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.

  • Restructure your code in such way that you do not use neither <= nor >= relational operators. You will use < or > and/or other operators
  • Generate the same output, but use only decremented counters (e.g. i = i - 1).
  • Modify your code to use only prefix decrements (e.g., --i).
  • Change your is valid function to use a switch construct instead of if statements.
  • Rewrite your print pattern function to use a for loop nested within a for loop.
Protip: If you complete any of the Extra Steps above, make sure your output still matches the expected output given in the Example Executions.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.