Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What this Lab Is About: Learn to declare & create void and non-void functions with or without input parameters. Learn to call different type of

What this Lab Is About:

Learn to declare & create void and non-void functions with or without input parameters.

Learn to call different type of fucntions from main() program

Coding Guidelines for All Labs/Assignments (You will be graded on this)

Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).

Keep identifiers to a reasonably short length.

Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).

Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.

Use white space to make your program more readable.

Use comments properly before or after the ending brace of classes, methods, and blocks to identify to which block it belongs.

1. Lab Description

In order to write a "good" modular program, it is important that the program be composed of useful, well-defined functions. There are two types of C++ functions:

System defined functions, such as the sqrt(), pow() and rand() funtions we learned and used in Lab #3 and #4

User-defined functions.

User defined functions can be further divided into two groups depending on whether a function does or does not return a value.

void function : no value is returned

non-void function: one value is returned

Another topic of this lab is function parameters, some functions has none (0) parameters, where others may have one or more parameters. In C++, parameters are used to pass information into a function. For this lab, you are given the skeleton of Lab8.cpp file, inside you will need to declare and create 4 different functions and call them from the main() by plugging in the actual parameters.

1.1 Step 1: Getting Started

Right click to download Lab8.cpp, be sure to name the file Lab8.cpp (again, no empty spaces and special characters are allowed, follow the naming conventions).

For documentation purpose, at the beginning of each programming lab/assignment, you must have a comment block with the following information inside:

//----------------------------------------------------------- // FILENAME: Lab8.cpp // AUTHOR: your name // ASU ID: your 10 digits ASU ID // SPECIFICATION: short description of the program //-----------------------------------------------------------

Besides the two libraries we included already, you need to include the sstream library as follows:

#include

sstream stands for String Stream, this library is used to get everything user entered from keyboard as a string first, then convert them into the corresponding correct data type (int, double, char, etc) accordingly. We use it here to avoid reading in last line stroke which may cause the submission problem on Linux server.

The general way to use it, for example, as the following segment of codes show, we want to get a double value from keyboard and save it inside a variable num.

double num; //declare a string variable string userInput = " "; //user prompt cout << "Enter a number: "; //get user input as a stream of strings stringstream inputStream(userInput); //convert the inputstream to a double value inputStream >> num;

1.2 Step 2: Function declarations

The C++ compiler requires specific information related to all functions activated in the program. To provide this information to the compiler, we must use a function declaration statement (or function prototype declaration). The syntax for declaring the function prototype is:

return_data_type functionName(formal_parameter_list);

reture_data_type : the type of the value returned by the function. When no value is returned, the type is "void".

function-name: the identifier of the function. By naming convention, normally we use verbs.

formal_parameter_list: a list of parameters with their types used in the function. When no parameters are used, i.e., the list is empty, just include the open and closed parentheses.

don't forget the semicolon ";" at the end

Example : A function named displayMenu that does not return a value, and does not require formal parameters can be declared as the following:

void displayMenu(); //function #1

Besides above function #1, in this lab, you need to declare three more other functions as below described:

Function #2: declare a function called showTriangle which takes a postive integer as input parameter and show, for example, if the integer entered is 8, the function should print the following triangle shape on screen. i.e. this function has no value to return and its return data type should be void.

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

Function #3: declare a function called printLetterGrade which takes two input parameters, the first one is a string represents a student's name, the second one is his/her test score, then the function should calculate and print the student's corresponding letter grade, for example, print the following message on screen (user input is in bold). According to the description here, this function has no value to return and its return data type should be void.

Please enter the student's name: John Smith Enter the student's test score: 92.7 John Smith's letter grade is: A

Function #4: declare a function called areaTriangle which takes a triangle's base and height as input parameter and returns the triangle's area. For this function, it has one return value and the value's data type should be double.

Enter the base & height of the triangle: 6.5 3.7 The area of the triangle is: 12.03

1.3 Step 3: Function Definition - Introduction

The function definition is a listing of the actual instructions that should be executed in the function. For example, in above step #2, we had a function called displayMenu, this function will display the following menu on screen and it should contain several cout statements.

================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ==================================

The function definition is composed of two parts: the function header and a function body. The function header appears as: return_data_type functionName(formal_parameter_list)

The function body appears as follows: { [local variable declarations] // a phrase in [ ] means "optional" executable statements; [return //----]; //optional }

NOTE:

The function header does not end with a semicolon!

The function header closely resembles the function prototype!

The function body:

requires a '{' at the beginning and a '}' at the end.

may or may not require declare local variables. If variables are needed in the function then these variables should be declared at the beginning of the function body.

if the function is a non-void function, then there will always be the return statement(s) somewhere inside the fucntion.

For function displayMenu, since it takes no input parameter and return no value, its header will be:

void displayMenu()

Its body will simply contains a set of cout statements as follows:

{ cout << " ================================== "; cout << " ASU CSE100 Lab #8 - Functions "; cout << "1. Show a triangle of numbers "; cout << "2. Check student's letter grade "; cout << "3. Compute a triangle's area "; cout << "4. Quit the Program "; cout << " ================================== "; }

In C++, you can not put one function inside another function and the program's entry point is the main() function. The general structure of Lab8.cpp should be:

int main() { //.... }

//header of function #1 - displayMenu { //body of function #1 //.... }

//header of function #2 - showTriangle { //body of function #2 //.... } //header of function #3 - printLetterGrade { //body of function #3 //.... } //header of function #4 - triangleArea { //body of function #4 //.... }

1.4 Step 4: Function Definition - define the showTriangle function

As we described in step #2, showTriangle function takes a positive integer as input parameter and output the relevant shape on screen. For the body of the fucntion, you should use a nested for loop. See this NestedForLoop.cpp as a reference. You will use a for loop similar to shape (C) to do the task.

1.5 Step 5: Function Definition - define the printLetterGrade function

As we described in step #2, printLetterGrade function takes two input parameters, the first one is a string represents a student's name, the second one is his/her test score, then the function should calculate and print the student's corresponding letter grade. According to the description, this function has no value to return, so write code to define the header of the function first:

//Define the header of function printLetterGrade

//----

Inside the body of the function, you can use a switch or nested if..else statement to print the student's relevant letter grade on screen. Check below sample run for the output format.

1.6 Step 6: Function Definition - define the areaTriangle function

As we described in step #2, areaTriangle function takes a triangle's base and height as input parameter and returns the triangle's area. For this function, it has one return value and the value's data type should be double, so according to the description, design the header of the function first:

//Define the header of function of areaTriangle

//----

The area of a triangle can be computed by formular (base X height)/2. This function is a non-void function, so there must be a return statement which returns the area and store it inside the variable areaTriangle (function's name)

1.7 Step 7: Function Calling (or function activation)

No function in a C++ program will be executed unless it is activated (or invoked or called) by another function. The syntax for void function activation is:

FunctionName(argument_list);

In other words, void function should be called as a statement. The argument (or actual parameters) should match what are defined in formal parameters in terms of data type, number & order. To activate the function displayMenu function, we merely need to specify the name of the function and use () to indicate that the function requires no arguments:

displayMenu();

For non-void functions, they can be activated in an expression or in an output statement. For example, for function areaTriangle, assume the base and height is 4.0 & 3.5 respectively, then the following are all legal in C++,

cout << " The area of the triangle is: " << areaTriangle(4.0, 3.5) << endl;

double y = areaTriangle(4.0, 3.5);

In this step, according to the comments we put inside the skeleton of the main() program, call the functions we designed in step #2 - #6 respectively.

Sample Run

================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== Enter your choice (1-4): 1 Enter an integer: 8 8 7 8 6 7 8 5 6 7 8 4 5 6 7 8 3 4 5 6 7 8 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== Enter your choice (1-4): 2 Please enter the student's name: John Smith Enter the student's test score: 92.6 John Smith's letter grade is: A ================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== Enter your choice (1-4): 3 Enter the base & height of the triangle: 4.6 3.5 The area of the triangle is: 8.05 ================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== Enter your choice (1-4): 4 Process returned 0 (0x0) execution time : 49.233 s Press any key to continue.

Input Case:

2 Joe Dow 87.6 1 7 3 34.7 15.8 1 9 4 

Output Case:

================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== Joe Dow letter grade is: B ================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== 7 6 7 5 6 7 4 5 6 7 3 4 5 6 7 2 3 4 5 6 7 1 2 3 4 5 6 7 ================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== The area of the triangle is: 274.13 ================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== 9 8 9 7 8 9 6 7 8 9 5 6 7 8 9 4 5 6 7 8 9 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 ================================== ASU CSE100 Lab #8 - Functions 1. Show a triangle of numbers 2. Check student's letter grade 3. Compute a triangle's area 4. Quit the Program ================================== 

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

Step: 3

blur-text-image

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

Students also viewed these Databases questions

Question

What are the fundamental elements of communication?

Answered: 1 week ago