UECS1643 Fundamentals Of Programming
Practical 3 – Refer to Topics 05 and 06
Part A (Understanding Concepts)
1. Run the following programs and answer the related questions.
(a)
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double sqrt_num;
sqrt_num = sqrt(16.0);
cout << "Square root of 16 "
<< " is " << sqrt_num << endl;
return 0;
}
What is the output?
Function Call What is the name of the function?
sqrt ( 16.0 )
What is the value passed to the function?
What is the value returned by the function?
(b)
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double num, sqrt_num;
cout << "Enter a number: ";
cin >> num;
sqrt_num = sqrt(num);
cout << "Square root of " << num
<< " is " << sqrt_num << endl;
return 0;
}
1
UECS1643 Fundamentals Of Programming
What is the output if the number entered is 16?
What is the output if the number entered is 23?
(c)
#include <iostream>
void using namespace std;
welcome(void)
void welcome(void);
{welcome();
void <<
cout welcome(void);
"** Welcome **" //
<< function
endl; prototype
}
int main(void)
{
welcome(); // function call
return 0;
}
// function definition
void welcome(void)
{
cout << "*** Welcome ***" << endl;
}
What is the output?
Function Prototype What does the first keyword void mean?
What does the second keyword void mean?
Function Definition What does this function do?
Function Call Why do we leave the brackets empty in this
function call, that is, ( )?
(d)
#include <iostream>
2
UECS1643 Fundamentals Of Programming
using namespace std;
void func(int num); // function prototype
int main(void)
{
int number;
func(123); // first function call
number = 456;
func(number); // second function call
return 0;
}
// function definition
void func(int num)
{
cout << num << endl;
}
What is the output?
Function Definition What is the name of the formal parameter?
void func(int num)
{
cout << num << endl; What is the data type of the formal parameter?
}
First Function Call What is the actual parameter in the function call?
func(123);
What is the value passed to the formal parameter
Function Definition
num?
void func(int num)
{ Does the function func return any value?
cout << num << endl;
}
Second Function Call What is the actual parameter in the function call?
func(number);
Function Definition
What is the value passed to the formal parameter
num?
void func(int num)
{
cout << num << endl;
}
(e)
3
UECS1643 Fundamentals Of Programming
#include <iostream>
using namespace std;
void pattern(char ch, double num); // function prototype
int main(void)
{
pattern('a', 2.5); // function call
return 0;
}
// function definition
void pattern(char ch1, double num)
{
cout << ch1 << num << endl;
}
What is the output?
Function Definition What are the names of the formal parameters?
void pattern(char ch1, double num)
{
cout << ch1 << num << endl; What are the data types of the formal
} parameters?
Function Call What are the actual parameters in the function
call?
pattern('a', 2.5);
Function Definition What are the values passed to the formal
parameters for the function call above?
void pattern(char ch1, double num)
{
cout << ch1 << num << endl;
}
4
UECS1643 Fundamentals Of Programming
2. The following programs contain errors. Compile the programs and answer the related questions.
(a)
#include <iostream>
using namespace std;
void display(int num); // function prototype
int main(void)
{
display(3, 4, 5); // function call
return 0;
}
// function definition
void display(int num)
{
cout << num << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(b)
#include <iostream>
using namespace std;
void display(int num1, int num2); // function prototype
int main(void)
{
display(3); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(c)
#include <iostream>
5
UECS1643 Fundamentals Of Programming
using namespace std;
void display(int num1, num2); // function prototype
int main(void)
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(d)
#include <iostream>
using namespace std;
void display(int num); // function prototype
int main(void)
{
int number;
number = 5;
display(int number); // function call
return 0;
}
// function definition
void display(int num)
{
cout << num << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(e)
#include <iostream>
6
UECS1643 Fundamentals Of Programming
using namespace std;
void display(int num1); // function prototype
int main(void)
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(f)
#include <iostream>
using namespace std;
void display(int num1, int num2) // function prototype
int main(void)
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(g)
#include <iostream>
int main(void)
7
UECS1643 Fundamentals Of Programming
{
display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2);
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
(h)
#include <iostream>
using namespace std;
void display(int num1, int num2); // function prototype
int main(void)
{
void display(3, 8); // function call
return 0;
}
// function definition
void display(int num1, int num2)
{
cout << num1 << " and " << num2 << endl;
}
Compile the program. What compiler error message do you get? What is wrong?
3. Run the following program and answer the related question.
8
UECS1643 Fundamentals Of Programming
#include <iostream>
#include <iomanip>
#include <cmath> // for Math library functions
#include <cstdlib> // for random number generation functions
using namespace std;
int main(void)
{
double number1, number2, power;
number1 = 9.0;
number2 = 1.23;
power = 3;
cout << "Some Math library functions\n";
cout << fixed << setprecision(2);
cout << "sqrt(" << number1 << ")\t = " << setw(6) << sqrt(number1) << endl;
cout << "ceil(" << number2 << ")\t = " << setw(6) << ceil(number2) << endl;
cout << "floor(" << number2 << ")\t = " << setw(6) << floor(number2) << endl;
cout << "pow(" << number1 << "," << power << ")\t = "
<< setw(6) << pow(number1, power) << endl;
cout << "\nRandom number generation functions\n";
srand(997);
cout << "srand(997) to set the seed\n";
cout << "rand(): " << rand() << endl;
cout << "rand(): " << rand() << endl;
return 0;
}
What is the output?
4. Run the following program and answer the related question.
9
UECS1643 Fundamentals Of Programming
#include <iostream>
using namespace std;
// Function prototypes
void draw_horizontal(void);
void draw_parallel(void);
int main(void)
{
// Function calls
draw_horizontal();
draw_parallel();
draw_horizontal();
return 0;
}
// Function definitions
/*
* Draws a horizontal line
*/
void draw_horizontal(void)
{
cout << "--------\n";
}
/*
* Draws parallel lines
*/
void draw_parallel(void)
{
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
}
Structure Chart for the Program
main
draw_horizontal draw_parallel draw_horizontal
What is the output?
--------
| |
| |
| |
--------
10
UECS1643 Fundamentals Of Programming
5. Complete the main function in the program below to draw the following figure based on the structure chart
given below.
Structure Chart for the Program
main
draw_parallel draw_horizontal draw_intersect
#include <iostream>
using namespace std;
// Function prototypes
void draw_horizontal(void);
void draw_parallel(void);
void draw_intersect(void);
int main(void)
{
// write the function calls to draw the figure
draw_parallel();
draw_horizontal();
draw_intersect();
return 0;
}
/*
* Draws a horizontal line
*/
void draw_horizontal(void)
{
cout << "--------\n";
}
/*
* Draws parallel lines
*/
void draw_parallel(void)
{
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
}
11
UECS1643 Fundamentals Of Programming
/*
* Draws intersecting lines
*/
void draw_intersect(void)
{
cout << " /\\ \n";
cout << " / \\ \n";
cout << " / \\ \n";
}
Part B (Programming Exercises)
1. A program is required to draw the figure below.
The structure chart for the program is as follows:
main
draw_triangle draw_parallel
draw_intersect draw_horizontal
Write the program. Your program must have the following functions:
main
draw_horizontal
draw_parallel
draw_intersect
draw_triangle
#include <iostream>
using namespace std;
// Function prototypes
void draw_horizontal(void);
void draw_parallel(void);
void draw_intersect(void);
int main(void)
{
// write the function calls to draw the figure
draw_intersect();
draw_horizontal();
12
UECS1643 Fundamentals Of Programming
draw_parallel();
return 0;
}
/*
* Draws a horizontal line
*/
void draw_horizontal(void)
{
cout << "--------\n";
}
/*
* Draws parallel lines
*/
void draw_parallel(void)
{
cout << "| |\n";
cout << "| |\n";
cout << "| |\n";
}
/*
* Draws intersecting lines
*/
void draw_intersect(void)
{
cout << " /\\ \n";
cout << " / \\ \n";
cout << " / \\ \n";
}
2. Consider the program where the function main asks the user for the number of apples and the number of
oranges, computes, and displays the totals in the format shown below. The x’s represent values entered by the
user or computed by the program.
Output Format:
=======My Fruit Store=======
Fruit Qty Price Total
----------------------------
Apple xx RM0.85 [Link]
Orange xx RM1.00 [Link]
----------------------------
Total [Link]
The price for an apple is RM0.85 and for an orange is RM1.00.
Write the statements in function main to call the other functions and display the output.
#include <iostream>
#include <iomanip>
13
UECS1643 Fundamentals Of Programming
// defined constant for price per apple
#define PRICE_PER_APPLE 0.85
// defined constant for price per orange
#define PRICE_PER_ORANGE 1.00
using namespace std;
// function prototypes
void display_heading(void);
void display_apple_item(int qty, double total);
void display_orange_item(int qty, double total);
void display_grand_total(double grand_total);
int main(void)
{
int apple_qty, orange_qty;
double apple_total, orange_total, grand_total;
// get number of apples and oranges
cout << "Enter number of apples: ";
cin >> apple_qty;
cout << "Enter number of oranges: ";
cin >> orange_qty;
// compute total for apples and oranges
apple_total = apple_qty * PRICE_PER_APPLE;
orange_total = orange_qty * PRICE_PER_ORANGE;
// compute grand total
grand_total = apple_total + orange_total;
// write the function calls to display the result
display_heading();
display_apple_item(apple_qty, apple_total);
display_orange_item(orange_qty, orange_total);
display_grand_total(grand_total);
return 0;
}
void display_heading(void)
{
cout << "=========My Fruit Store========\n";
cout << "Fruit Qty Price Total\n";
cout << "-------------------------------\n";
}
void display_apple_item(int qty, double total)
{
cout << "Apple\t" << setw(3) << qty;
cout << " RM" << setw(4) << fixed << setprecision(2) << PRICE_PER_APPLE;
cout << "\tRM" << setw(5) << fixed << setprecision(2) << total << endl;
}
void display_orange_item(int qty, double total)
{
cout << "Orange\t" << setw(3) << qty;
cout << " RM" << setw(4) << fixed << setprecision(2) << PRICE_PER_ORANGE;
cout << "\tRM" << setw(5) << fixed << setprecision(2) << total << endl;
}
14
UECS1643 Fundamentals Of Programming
void display_grand_total(double total)
{
cout << "-------------------------------\n";
cout << "Total\t\t\tRM" << setw(5) << fixed << setprecision(2) << total << endl;
}
Part C (Self-Review / Revision)
1. What is a function call?
2. What is a function definition?
3. What is a function prototype?
4. What is the purpose of a parameter? Where do we write a formal parameter? Where do we write an actual
parameter?
Part D (Practice Exercises)
1. Write a program that displays the initials of your name. Use functions to display each letter. A sample run of
the program is as follows (assuming the initials are HES):
H H
H H
HHHHH
H H
H H
EEEEE
E
EEEEE
E
EEEEE
SSSSS
S
SSSSS
S
SSSSS
#include <iostream>
using namespace std;
// Function prototypes
void draw_h(void);
15
UECS1643 Fundamentals Of Programming
void draw_e(void);
void draw_s(void);
int main(void)
{
// write the function calls to draw the figure
draw_h();
draw_e();
draw_s();
return 0;
}
/*
* Draws h
*/
void draw_h(void)
{
cout << "H H\n";
cout << "H H\n";
cout << "HHHHH\n";
cout << "H H\n";
cout << "H H\n\n";
}
/*
* Draws e
*/
void draw_e(void)
{
cout << "EEEEE\n";
cout << "E \n";
cout << "EEEEE\n";
cout << "E \n";
cout << "EEEEE\n\n";
}
/*
* Draws s
*/
void draw_s(void)
{
cout << "SSSSS\n";
cout << "S \n";
cout << "SSSSS\n";
cout << " S\n";
cout << "SSSSS\n\n";
}
16