



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An exercise for learning how to write user-defined functions in c++ with a focus on function headers, function bodies, function prototypes, and information passing through return values and pass by value or reference. The exercise also covers validating input data for dates and times using functions. Students will write a program that validates integer input data and creates functions to determine the maximum number of days in a month and check for leap years.
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




This exercise is designed to give you the ability to write code with user-defined functions. You should learn how to write function headers, function bodies and function prototypes. You should also learn how to transfer information into and out of functions. This information passing includes returning values by the function name and the use of pass by value and pass by reference.
During class, we discussed the general idea that a C++ program was a collection of functions. Previously we have had only one function, the main function, in our programs. In class, we noted that all programs have a main function because execution always starts with this function. The general form of all functions is shown below.
preceded by their data types. The argument list is the method by which we pass information from the calling program into the function. (If no information is passed into the function, we simply use parentheses with nothing in them, (), in the function header; we have already seen this with the main functions we have written.) In the normal default function operation, only the values of the variables in the calling program are passed into the function. In this case, called pass by value, any operations within the function do not affect the variables in the calling program. In an alternative approach, called pass by reference, it is possible to change the values of variables in the calling program that are passed to the function.
Information is normally returned from the function to the calling program through the function name. When a calling program uses a function name in an expression, control is transferred to the function. The body of the function is executed and the function returns a value to the calling program. That value is then used in evaluating the expression that contains the function name. This value returned by the function is assigned (in the function) by a return statement. We can have functions that do not return a value; such functions are assigned the void type.
When we use functions, we get the results from the functions by selecting the correct data to pass to the function. Once the function is coded and tested, we should not have to recode the function for a new application of the same function.
This exercise has two tasks. Each task is related to writing a program that validates input data relating to dates and times using functions. The first task requires you to copy one function from this exercise handout, and write a main program that calls this function to get valid integer input data. The second task asks you to create two new functions from pseudocode to determine the maximum number of days in a month, and to place all the input in a separate input function. The
second task is really an introduction to the first programming project. The functions that you develop here will be used directly in that project.
The instructions below discuss the items that you should place on the submission file for this assignment. Make sure that your name and your lab day are on the top of this file.
Task one: A function to check input ranges for integer variables
Background for the task. During class we have discussed the need to check the validity of input data. Because this task occurs so often in programming it is one that is well suited for coding as a function. A function to do this, called getValidInt, is provided below. This function has the following prototype.
int getValidInt( int xMin, int xMax, string name )
The getValidInt function delivers an input prompt to the user, gets the input value from the user, and checks that input value against the minimum and maximum allowed values of the variable, provided in the function call. If the variable is out of range, the user will be informed and given the opportunity to reenter the data. Once the user enters a valid value, that value is returned in the function name, getValidInt.
All the tasks described in the last paragraph are done by the code in the function. A programmer who uses this function can have a program perform all these tasks simply by calling the function with the correct arguments. The function may be called several times, with different arguments, to handle the input of several variables.
This function takes three arguments; the first two are the minimum and maximum allowed values of the input variable. The third argument is a string variable that contains the name of the variable for which the user will enter the input. 1 This string is used in the input prompt. For example, a date program that wants the user to enter the number of a month, which can be between 1 and 12, could use the following statement to call the getValidInt function.
int month = getValidInt( 1, 12, “month” );
When the main program encounters the example statement above, control is transferred to the getValidInt function. The function arguments xMin, xMax, and name are assigned the values 1, 12, and ”month”, respectively. The function has a do-while loop in which the user is prompted to enter a value for the input variable. In the example used here (getValidInt( 1, 12, “month” )), the initial cout statement in the function (shown on the next page) would print the following line to the screen.
Enter a value for month between 1 and 12:
Compare this line with the code for the cout statement. Note that the values for xMin, xMax, and name which were passed in the function call are used to provide the desired input prompt. The subsequent cin statement would then read a value into the variable x, which is a local integer
(^1) The string data type is used to represent character information. Such information is commonly used in
data processing programs to represent individuals’ names, addresses, etc. A string literal is a series of characters enclosed in quotation marks. So that statements like the following are valid: string name = “Larry”; string name2 = name;
To use the string data type it is necessary to have the following directive, #include
integer function getMaxDays( input month as integer, input year as integer) if month = 4 or month = 6 or month = 9 or month = 11 return 30 as function result otherwise, if month = 2 if the year is a leap year return 29 as function result otherwise return 28 as function result otherwise return 31 as function result End of function
To determine if the year is a leap year, you can use a function with the following prototype:
bool leap( int year );
This function has the year as an integer input parameter and returns a value of true if the year is a leap year and a value of false if the year is not a leap year. The decision as to whether a year is or is not a leap year is based on the following rules.
Years evenly divisible by four are leap years, except Years evenly divisibly by 100 are not leap years, except, Years evenly divisible by 400 are leap years.
This can be implemented in C++ code in several ways. Recall that we can use the mod operator (%) to determine if one number is evenly divisible by another. The expression year % 4 == 0 will be true if year is evenly divisible by four. Similarly, year % 4 != 0 will be true if year is not evenly divisible by four. One possible pseudocode implementation of the leap( year ) function is shown below.
Boolean function leap( input year as integer) If year is not evenly divisible by four return false as function result otherwise, if year is evenly divisible by 400 return true as function result otherwise, if year is evenly divisible by 100 return false as function result otherwise return true as function result End of function
In a more compact approach the leap(year) function would have a single statement: return ( year % 4 == 0 ) && ( year % 100 != 0 || year % 400 == 0).
A final element of this task is to provide a separate input function. Such a function is usually used in larger programs where the isolation of all inputs into a separate function makes a significant enhancement to the understanding of the code. Here, the creation of a separate input function provides you practice in organizing code into separate functions and the use of pass by reference. One possible prototype for your input function is shown below.
void getInput( int& year, int& month, int& day );
This function would get the input values for the three variables shown in its argument list. The use of pass-by-reference, which is set by the ampersands (&) in the argument list, is required so that the function can change the variables that are passed to the function.
Specific assignment for task two. Modify the program used in task one so that all the input (using the getValidInt function) is placed in a separate input function. Change the maximum value for the input day of the month from 31 to the result of the getMaxDays( month, year ) function. This will require you to prepare two functions (getMaxDays and leap) from the pseudocode given here and to include these in your code for this task.
Although it is not required for this exercise, you can make your input for this task easier by using the following pseudocode for your main program. (This is based on the code used in exercise one, task three, which you can modify for this task.)
Start loop to allow repeated user inputs. o Call a function to get inputs on month, day and year from the user. o Print the date entered by the user o Prompt the user and get the appropriate input to see if the user wants to run another case. At the end of loop for running a single set of user input data, exit the loop if the user does not want to run another case. Otherwise, continue the loop.
Make sure that you have the necessary function prototypes for all the functions in your program. Remember that you can get the function prototype by simply copying the function header and adding a semicolon.
For this task, as in the previous task, a valid date is one between January 1, 1900 and December 31, 2000. The submission file for this task should contain a copy of your code followed by the results for the input data sets shown below. In some of these data items, you are asked to use incorrect data to test your data validation routines. When this is done, you are given one or more incorrect values followed by a correct value. For example, in the first data set you should enter two incorrect values (1899 and 2001) for the year followed by a correct value of 1990. After entering a correct value of 2 for the month, you should then enter two incorrect values (0 and 29) for the day of the month.
Your submission file should contain a listing of all the code for this task and the screen output for all the data entries in the table below. This should show that your program is giving the proper error message and allowing new input to the submission file. Don’t forget to place a statement in the main program that prints the user output to the screen once all the input values are correct.
Data Set Year Month Day 1 1899 then 2001 then 1990 2 0 then 29 then 28 2 2000 2 0 then 29 3 1900 2 29 then 28 4 1999 13 then 12 32 then 31 5 1999 0 then 6 31 then 30
Submit a copy of the submission file with all the elements asked for in the each task above.