0% found this document useful (0 votes)
59 views

Programming Language 2 (Eee) : Lab Manual

This document is a lab manual for a programming language course. It discusses several key concepts in C++ functions including: 1. The need for subprograms (functions) to break problems into smaller subproblems. 2. How to define user-defined functions with parameters and return types. Functions can take value or reference parameters. 3. Value parameters pass the value to the function safely without side effects, while reference parameters allow modifying the original variable. The document provides examples to illustrate functions, parameters, and good programming style using functions. It also briefly mentions splitting programs across multiple files.

Uploaded by

Voot Artist
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Programming Language 2 (Eee) : Lab Manual

This document is a lab manual for a programming language course. It discusses several key concepts in C++ functions including: 1. The need for subprograms (functions) to break problems into smaller subproblems. 2. How to define user-defined functions with parameters and return types. Functions can take value or reference parameters. 3. Value parameters pass the value to the function safely without side effects, while reference parameters allow modifying the original variable. The document provides examples to illustrate functions, parameters, and good programming style using functions. It also briefly mentions splitting programs across multiple files.

Uploaded by

Voot Artist
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PROGRAMMING

LANGUAGE 2 [EEE]
LAB MANUAL

Lab Manual # 3
Faculty:
M. Arifur Rahman
Table of Contents
I. 1.0 The Need for sub-programs ...................................................................................... 2

II. 2.0 User-defined Functions ............................................................................................ 2

III. 3.0 Value and Reference Parameters ............................................................................ 4


3.1 Functions which use Value Parameters are Safe ............................................................................................. 5
3.2 Reference Parameters .......................................................................................................................................... 6
IV. 4.0 Polymorphism and Overloading ............................................................................. 7

V. 5.0 Procedural Abstraction and Good Programming Style ........................................... 8

VI. 6.0 Splitting Programs into Different Files ................................................................... 8

VII. 7.0 Exercises ................................................................................................................ 11


Question 1.................................................................................................................................................................. 11
Example output: ....................................................................................................................................................... 12
Question 2.................................................................................................................................................................. 12
Question 3.................................................................................................................................................................. 12

PAGE 1 OF 12
1.0 The Need for sub-programs

A natural way to solve large problems is to break them down into a series of sub problems, which can be solved
more or less independently and then combined to arrive at a complete solution. In programming, this
methodology reflects itself in the use of subprograms, and in C++ all subprograms are called functions
(corresponding to both "functions" and "procedures" in Pascal and some other programming languages).

We have already been using subprograms. For example, in the program which generated a table of square roots,
we used the following "for loop":

...
#include<cmath>
... ... for (number = 1 ; number <= 10 ; number =
number + 1)
{ cout.width(20);
cout << number << sqrt(number) << "\n";
}
...
...

The function "sqrt(...)" is defined in a subprogram accessed via the library file cmath (old header style
math.h). The subprogram takes "number", uses a particular algorithm to compute its square root, and then
returns the computed value back to the program. We don't care what the algorithm is as long as it gives the
correct result. It would be ridiculous to have to explicitly (and perhaps repeatedly) include this algorithm in the
"main" program.

2.0 User-defined Functions

Here's a trivial example of a program which includes a user defined function, in this case called "area(...)".
The program computes the area of a rectangle of given length and width.

#include<iostream>
using namespace std;

int area(int length, int width); /* function declaration */

/* MAIN PROGRAM: */
int main()
{ int this_length, this_width;
cout << "Enter the length: "; /* < line 9
*/ cin >> this_length;

PAGE 2 OF 12
cout << "Enter the width: "; cin
>> this_width;
cout << "\n"; /* < line 13 */

cout << "The area of a " << this_length << "x" << this_width; cout
<< " rectangle is " << area(this_length, this_width);
return 0;
}

/* END OF MAIN PROGRAM */ /*


FUNCTION TO CALCULATE AREA: */
int area(int length, int width) /* start of function definition */
{ int number;

number = length * width;

return number;
} /* end of function definition */
/* END OF FUNCTION */

Program 2.0.1: Use of Function

Although this program is not written in the most succinct form possible, it serves to illustrate a number of features
concerning functions:

• The structure of a function definition is like the structure of "main()", with its own list of variable
declarations and program statements.
• A function can have a list of zero or more parameters inside its brackets, each of which has a separate
type.
• A function has to be declared in a function declaration at the top of the program, just after any global
constant declarations, and before it can be called by "main()" or in other function definitions.
• Function declarations are a bit like variable declarations they specify which type the function will return.

A function may have more than one "return" statement, in which case the function definition will end execution
as soon as the first "return" is reached. For example:

double absolute_value (double number)


{ if (number >= 0)
return number;
else return 0 number;
}

3.0 Value and Reference Parameters

PAGE 4 OF 12
The parameters in the functions above are all value parameters. When the function is called within the main
program, it is passed the values currently contained in certain variables. For example, "area(...)" is passed
the current values of the variables "this_length" and "this_width". The function "area(...)" then
stores these values in its own private variables, and uses its own private copies in its subsequent computation.

3.1 Functions which use Value Parameters are Safe


The idea of value parameters makes the use of functions "safe", and leads to good programming style. It helps
guarantee that a function will not have hidden side effects. Here is a simple example to show why this is important.
Suppose we want a program which produces the following dialogue:

Enter a positive integer:


4
The factorial of 4 is 24, and the square root of 4 is 2.
It would make sense to use the predefined function "sqrt(...)" in our program, and write another function
"factorial(...)" to compute the factorial n! = (1 x 2 x ... x n) of any given positive integer
n. Here's the complete program:

#include<iostream>
#include<cmath>
using namespace std;

int factorial(int number);

/* MAIN PROGRAM: */
int main()
{ int whole_number;
cout << "Enter a positive integer:\n"; cin
>> whole_number;

cout << "The factorial of " << whole_number << " is "; cout
<< factorial(whole_number);
cout << ", and the square root of " << whole_number << " is "; cout
<< sqrt(whole_number) << ".\n";

return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO CALCULATE FACTORIAL: */


int factorial(int number)
{ int product = 1;
for ( ; number > 0 ; number
) product *= number;

return product;
}
/* END OF FUNCTION */

PAGE 5 OF 12
Program 3.1.1: Functions with Arguments

By the use of a value parameter, we have avoided the (correct but unwanted) output

Enter a positive integer:


4
The factorial of 4 is 24, and the square root of 0 is 0.

which would have resulted if the function "factorial(...)" had permanently changed the value of the variable
"whole_number".

3.2 Reference Parameters


Under some circumstances, it is legitimate to require a function to modify the value of an actual parameter that it
is passed. For example, going back to the program which inputs the dimensions of a rectangle and calculates the
area, it would make good design sense to package up lines 9 to 13 of the main program into a
"getdimensions" subprogram (i.e. a C++ function). In this case, we require the function to alter the values
of "this_length" and "this_width" (passed as parameters), according to the values input from the
keyboard.

We can achieve this as follows using reference parameters, whose types are post-fixed with an "&":

#include<iostream>
using namespace std;

int area(int length, int width);

void get_dimensions(int& length, int& width);

/* MAIN PROGRAM: */
int main()
{ int this_length, this_width;
get_dimensions(this_length, this_width);
cout << "The area of a " << this_length << "x" << this_width; cout
<< " rectangle is " << area(this_length, this_width);
return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO INPUT RECTANGLE DIMENSIONS: */


void get_dimensions(int& length, int& width)
{ cout << "Enter the length: ";
cin >> length;

cout << "Enter the width: "; cin


>> width;

cout << "\n";


}
PAGE 6 OF 12
/* END OF FUNCTION */

/* FUNCTION TO CALCULATE AREA: */


int area(int length, int width)
{ return length * width;
}
/* END OF FUNCTION */

Program 3.2.1: Functions with reference parameters

Notice that, although the function "get_dimensions" permanently alters the values of the parameters
"this_length" and "this_width", it does not return any other value (i.e. is not a "function" in the
mathematical sense). This is signified in both the function declaration and the function heading by the reserved
word "void".

4.0 Polymorphism and Overloading

C++ allows polymorphism, i.e. it allows more than one function to have the same name, provided all functions
are either distinguishable by the typing or the number of their parameters. Using a function name more than
once is sometimes referred to as overloading the function name. Here's an example:

#include<iostream>
using namespace std;

int average(int first_number, int second_number, int third_number);

int average(int first_number, int second_number);

/* MAIN PROGRAM: */
int main()
{ int number_A = 5, number_B = 3, number_C = 10;

cout << "The integer average of " << number_A << " and "; cout
<< number_B << " is ";
cout << average(number_A, number_B) << ".\n\n";

cout << "The integer average of " << number_A << ", ";
cout << number_B << " and " << number_C << " is ";
cout << average(number_A, number_B, number_C) <<
".\n";

return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */


int average(int first_number, int second_number, int third_number)
PAGE 7 OF 12
{ return ((first_number + second_number + third_number) / 3);
}
/* END OF FUNCTION */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */


int average(int first_number, int second_number)
{ return ((first_number + second_number) / 2);
}
/* END OF FUNCTION */

Program 4.0.1: Polymorphism & Overloading

This program produces the output:

The integer average of 5 and 3 is 4.


The integer average of 5, 3 and 10 is 6.

5.0 Procedural Abstraction and Good Programming Style

One of the main purposes of using functions is to aid in the top down design of programs. During the design
stage, as a problem is subdivided into tasks (and then into subtasks, sub-subtasks, etc.), the problem solver
(programmer) should have to consider only what a function is to do and not be concerned about the details of
the function. The function name and comments at the beginning of the function should be sufficient to inform
the user as to what the function does. (Indeed, during the early stages of program development, experienced
programmers often use simple "dummy" functions or stubs, which simply return an arbitrary value of the correct
type, to test out the control flow of the main or higher level program component.)

Developing functions in this manner is referred to as functional or procedural abstraction. This process is aided
by the use of value parameters and local variables declared within the body of a function. Functions written in
this manner can be regarded as "black boxes". As users of the function, we neither know nor care why they work.

6.0 Splitting Programs into Different Files

As we have seen, C++ makes heavy use of predefined standard libraries of functions, such as "sqrt(...)". In
fact, the C++ code for "sqrt(...)", as for most functions, is typically split into two files:

• The header file "cmath" contains the function declarations for "sqrt(...)" (and for many other
mathematical functions). This is why in the example programs which call "sqrt(...)" we are able to
write "#include<cmath>", instead of having to declare the function explicitly.

PAGE 8 OF 12
• The implementation file "math.cpp" contains the actual function definitions for "sqrt(...)" and
other mathematical functions. (In practice, many C++ systems have one or a few big file(s) containing all
the standard function definitions, perhaps called "ANSI.cpp" or similar.)

It is easy to extend this library structure to include files for user-defined functions, such as "area(...)",
"factorial(...)" and "average(...)". As an example, Program 6.0.1 below is the same as Program
4.0.1, but split into a main program file, a header file for the two average functions, and a corresponding
implementation file.

The code in the main program file is as follows:

#include<iostream>
#include"averages.h"
using namespace std;

int main()
{ int number_A = 5, number_B = 3, number_C = 10;

cout << "The integer average of " << number_A << " and "; cout
<< number_B << " is ";
cout << average(number_A, number_B) << ".\n\n";

cout << "The integer average of " << number_A << ", ";
cout << number_B << " and " << number_C << " is ";
cout << average(number_A, number_B, number_C) <<
".\n";

return 0;
}

Program 6.0.1: Splits

Notice that whereas "include" statements for standard libraries such as "iostream" delimit the file name
with angle ("<>") brackets, the usual convention is to delimit user-defined library file names with double quotation
marks hence the line "#include"averages.h" " in the listing above.

The code in the header file "averages.h" is listed below. Notice the use of the file identifier "AVERAGES_H",
and the reserved words "ifndef" ("if not defined"), "define", and "endif". "AVERAGES_H" is a (global)
symbolic name for the file. The first two lines and last line of code ensure that the compiler (in fact, the
preprocessor) only works through the code in between once, even if the line "#include"averages.h"" is
included in more than one other file.

Constant and type definitions are also often included in header files.

PAGE 9 OF 12
#ifndef AVERAGES_H

#define AVERAGES_H

/* (constant and type definitions could go here) */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */


int average(int first_number, int second_number, int third_number);
/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */ int
average(int first_number, int second_number);

#endif

Program 6.0.2: averages.h

Finally, the code in the implementation file "averages.cpp" is as follows:

#include<iostream>
#include"averages.h"
using namespace std;

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 3 INTEGERS: */


int average(int first_number, int second_number, int third_number)
{ return ((first_number + second_number + third_number) / 3);
}
/* END OF FUNCTION */

/* FUNCTION TO COMPUTE INTEGER AVERAGE OF 2 INTEGERS: */


int average(int first_number, int second_number)
{ return ((first_number + second_number) / 2);
}
/* END OF FUNCTION */

Program 6.0.3: averages.cpp

Note the modularity of this approach. We could change the details of the code in "averages.cpp" without
making any changes to the code in "averages.h" or in the main program file.

PAGE 10 OF 12
7.0 Exercises

Question 1
Add 6 appropriate function declarations for

celsius_of absolute_value_of
print_preliminary_message input_table_specifications
print_message_echoing_input print_table

to the following program, and add the 4 missing function definitions, so that it produces output such as that given
below. Test your program with various inputs.

/* This program prints out a conversion table of temperatures, after


prompting the user for upper and lower bounds of the table in
Fahrenheit, and the temperature difference between table entries. */
#include <iostream> using
namespace std;

/* START OF MAIN PROGRAM */ int


main()
{ int lower = 0; /* the lowest Fahrenheit entry in the table */ int
upper = 0; /* the highest Fahrenheit entry in the table */ int
step = 1; /* difference in Fahrenheit between entries */

/* print a message explaining what the program does: */


print_preliminary_message();

/* prompt the user for table specifications in Fahrenheit: */


input_table_specifications(lower, upper, step);

/* print appropriate message including an echo of the input: */


print_message_echoing_input(lower, upper, step);

/* Print the table (including the column headings): */


print_table(lower, upper, step);
return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO CONVERT FAHRENHEIT TO CELSIUS */ double


celsius_of(int fahr)
{ return (static_cast<double>(5)/9) * (fahr 32);
}
/* END OF FUNCTION */

PAGE 11 OF 12
/* FUNCTION TO CONVERT FAHRENHEIT TO ABSOLUTE VALUE */ double
absolute_value_of(int fahr)
{ return ((static_cast<double>(5)/9) * (fahr ‐ 32)) + 273.15;
}
/* END OF FUNCTION */

Program 7.1.1: Question 1

Example output:
This program prints out a conversion table of temperatures.

Enter the minimum (whole number) temperature


you want in the table, in Fahrenheit: 0
Enter the maximum temperature you want in the table: 100
Enter the temperature difference you want between table entries: 20
Tempertature conversion table from 0 Fahrenheit
to 100 Fahrenheit, in steps of 20 Fahrenheit:

Fahrenheit Celsius Absolute Value


0 ‐17.78 255.37
20 ‐6.67 266.48
40 4.44 277.59
60 15.56 288.71
80 26.67 299.82
100 37.78 310.93

Question 2
Split your answer program to Question 1 into three files: (i) a main program file, (ii) a header file called
"conversions.h" for the functions "celsius_of(...)" and "absolute_value_of(...)", and
(iii) an implementation file for these two functions. Again, test your program with various inputs.

Question 3
(a) Create a header file "statistics.h" and a corresponding implementation file
"statistics.cpp" containing the functions "average(...)" and
"standard_deviation(...)". The functions should
return the average and standard deviation respectively of 1, 2, 3 or 4 real number values. The standard deviation
of the numbers r1, ..., rN is defined as the square root of the average of the expressions

((𝑟1 − 𝑎) × (𝑟1 − 𝑎)), ((𝑟2 − 𝑎) × (𝑟2 − 𝑎)), … , ((𝑟𝑁 − 𝑎) × (𝑟𝑁 − 𝑎))

PAGE 12 OF 12
where a is the average value of r1, ..., rN.

Hints:

i. You should take advantage of C++'s facility for polymorphic functions, and overload the
function names.
ii. It is possible to call one function from inside another. iii. You should be doing plenty
of cutting and pasting (not usually desirable!)

(b) Write a program which tests the functions in "statistics.h" again and again with various inputs
until you tell the program that you are finished. Your program should be able to reproduce the following sample
input/output session:

This program tests the functions in the 'statistics.h' header file.


Do you wish to test 1, 2, 3 or 4 numbers (enter 0 to end the program): 3
Enter first value: 5
Enter second value: 7
Enter third value: 9
Average: 7. Standard deviation: 1.63299.
Do you wish to test 1, 2, 3 or 4 numbers (enter 0 to end the program): 1
Enter first value: 5.8
Average: 5.8. Standard deviation: 0.
Do you wish to test 1, 2, 3 or 4 numbers (enter 0 to end the program): 8
Sorry, the program can only test 1, 2, 3 or 4 values.
Do you wish to test 1, 2, 3 or 4 numbers (enter 0 to end the program): 0
Finished testing 'statistics.h' header file.

Hints:

i. Design your program "top down". Begin by writing a short main program which calls functions such
as "test_three_values()", which you can define in detail later, after "main".
ii. As a top level control structure, you might want to use a "for loop" with empty initialization and
update statements (this is equivalent to a "while statement").

PAGE 13 OF 12

You might also like