DOC PREVIEW
CSUN COMP 106 - Seventh Programming Exercise

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

College of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Class number: 11672 Instructor: Larry CarettoSeventh Programming ExerciseObjectiveThis 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 includesreturning values by the function name and the use of pass by value and pass by reference.Background for AssignmentDuring 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 notedthat all programs have a main function because execution always starts with this function. The general form of all functions is shown below.<function type> <function name> ( <argument list> ){<function body>}The first line of the function is called the function header. In this line, the <function type> is the data type of the value returned by the function. The <argument list> is a list of variables preceded by their data types. The argument list is the method by which we pass information fromthe calling program into the function. (If no information is passed into the function, we simply useparentheses 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.Overview of the exerciseThis 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 Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448Email: [email protected] 8348 Fax: 818.677.7062this 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.Specific tasksTask one: A function to check input ranges for integer variablesBackground 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: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 <string>, at the start of the program to invoke the string library.Jacaranda (Engineering)


View Full Document

CSUN COMP 106 - Seventh Programming Exercise

Download Seventh Programming Exercise
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Seventh Programming Exercise and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Seventh Programming Exercise 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?