Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Overview In this lab, you will write a program that prints out a pattern, based upon the user's input. You will restrict the user's input

Overview

In this lab, you will write a program that prints out a pattern, based upon the user's input. You will restrict the user's 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:

 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 1 2 3 4 5 1 2 3 1 

This lab requires you to design and implement three functions: get_input, is_valid, and print_pattern, in addition to your main function. The function get_input will repeatedly get input and check it with is_valid, until the user enters a number that passes the validity check (described more below).

Objectives

Practice using selection (if ... else if ... else) statements in C

Practice using while and/or do ... while loops in C

Practice using for loops in C

Practice nesting a loop within another loop

Learn about additional formatting codes for printf

Design a robust text user interface that checks input validity

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 placeholder for an integer.

You should then call is_valid and pass the user-entered value 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.

TIP: It is common to implement an input request from a user with 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 an input parameter.

The function will determine if the parameter is an odd number between 1 and 9.

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

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 the user 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. 

Print Function

Next, complete your function named print_pattern.

This function will take 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 rows of numbers such that they form a diamond, as shown in the two Example Executions.

You will accomplish this using a loop nested inside another loop. (One loops over the rows, the other over each values to be printed in each row.)

NOTE: 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 a dynamic calculation approach, like the one described in the hints section below.

TIP: To acheive the proper spacing in this function, you need to explore the "%*s" placeholder format code for printf, which gives variable length padding.

 printf("%*s", x, " "); /* Prints x number of spaces */ 

Main Function

Your main function will first call get_input and save the number returned.

Next, call print_pattern with the input value, to display the pattern.

TIP: Never use a float or double as a loop counter, as you may end up in an infinite loop. Instead use an int type for your loop counter variables.

Test

Now, run and test your program. There are a number of different choices you may make during the design and implementation of this program. However, you will lose significant points if you do not properly format your diamond as shown.

Extra Steps

The following are optional enrichment exercises that you may choose to implement in order to practice additional skills. You may choose to implement some, all or none of them.

Restructure your code in such a way that you do not use either >= nor <= relational operators. You will use < or > and/or other operators.

Generate the same output, but use only decrement counters (e.g. i = i - 1)

Modify your code to use only prefix decrements (--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.

NOTE: If you complete any of the Extra Steps above, make sure your output still matches the expected output given in the Example Executions.

Example Execution 1

 $ ./program1 Enter an odd number less than or equal to 9 and greater than 0 > 6 You have entered an even number. Please try again. Enter an odd number less than or equal to 9 and greater than 0 > 11 You have entered a number greater than 9. Please try again. Enter an odd number less than or equal to 9 and greater than 0 > 5 1 1 2 3 1 2 3 4 5 1 2 3 1 

Example Execution 2

 $ ./program1 Enter an odd number less than or equal to 9 and greater than 0 > 0 You have entered a number less than 1. Please try again. Enter an odd number less than or equal to 9 and greater than 0 > 4 You have entered an even number. Please try again. Enter an odd number less than or equal to 9 and greater than 0 > 7 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 1 2 3 1 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students explore these related Databases questions