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

CSC425_Chapter5 (2)

Uploaded by

Muhammad Al-azim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CSC425_Chapter5 (2)

Uploaded by

Muhammad Al-azim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

CHAPTER 5 : FUNCTION*

• Predefined function
o Is the function which are already embedded/exist in the DevCPP library.
o #include <math.h>
▪ sqrt()
▪ pow()
▪ abs() ➔ abs(-8) ➔ cout << abs(-8) ➔ 8
▪ round(9.8) ➔ 10
▪ ceil(9.3) ➔ 10
▪ floor(9.9) ➔ 9

cout << "sqrt(25) : " << sqrt(25) << endl;


cout << "pow(11,2) : " << pow(11,2) << endl;
cout << "pow(36,1/2.0) : " << pow(36,1/2.0) << endl;
cout << "abs(-100) : " << abs(-100) << endl;
cout << "fabs(-3.142) : " << fabs(-3.142) << endl;
cout << "round(9.4) : " << round(9.4) << endl;
cout << "ceil(9.3) : " << ceil(9.3) << endl;
cout << "floor(9.9) : " << floor(9.9) << endl;
cout << "ceil(-9.3) : " << ceil(-9.3) << endl;
cout << "floor(-9.9) : " << floor(-9.9) << endl;
cout << "abs(8) : " << abs(8) << endl;
cout << "fabs(-0.256) : " << fabs(-0.256) << endl;

o #include <string.h>
▪ strcpy()

char name[100];

strcpy(name,”Ali Bin Hassan”); //OK

name = “Ali bin Hassan”//Not OK

or

string name;

name = “Ali bin Hassan”

1
▪ strlen()
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char name[200];
cout << "\n Enter your name : ";
cin.get(name,200);
cin.ignore(80,'\n');

int namelength = strlen(name);

cout << "\n The length of the name (" <<name << ") entered is "
<< namelength;
return 0;
}
▪ strcmp()
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char name1[200], name2[200];

cout << "\n Enter your name 1: ";


cin.get(name1,200);
cin.ignore(80,'\n');
cout << "\n Enter your name 2 : ";
cin.get(name2,200);
cin.ignore(80,'\n');

if (strcmp(name1,name2) == 0) // if (name1 == name2) <=== cannot, not allowed


{
cout << "\n Both names are the same ";
}
else
{
cout << "\n Both names are not same ";
}

return 0;
}
o #include <iomanip>
▪ setprecision()
▪ setfill()
o #include <ctype.h>
▪ tolower()
▪ toupper()
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

int main()
{
char code;

2
cout << "\n Enter your code please : [a,b,c,d or e] : ";
cin >> code;

code = toupper(code); //tolower(code)

if (code == 'B') //no need to put small letter condition


{
//:
//:
}
cout << "\n The code after capitalize : " << code;

//cannot apply for string


char name[200];

cout << "\n Enter your name : ";


cin.get(name,200); //may be the user entered ' jamal othman' small letter
cin.ignore(80,'\n');

name = toupper(name);//cannot be applied

return 0;
}

• User defined function


- Is a function which the programmer needs to create your own function. Or another
words, not depends on the function which is embedded in DevCPP (predefined
function).
- Advantages of why we need to create your own function/user defined function.
o Easy to do the maintenance.
o To make the program is more structured
o The program will be running more efficient
o Reusable of the same function
- Functions are divided into 4 types of function :-
o void function without parameter
o void function with parameter* (Input in the main function, process and output
will be in the function)
o void function with parameter and return value(s) (Input in the main function,
process in the function, output return to the main function)
o non void function*

3
Example 1:
main()

Hello()

structured chart
#include <iostream>
using namespace std;
//1. declaration of the function @ function prototype
void Hello(void);//void means no parameter or nothing
int main() //main function
{
//declaration of the variable

//2. caller
Hello();//hello function

return 0;//exit from the main program, will be ended


}

//3. function definition


void Hello()
{
cout << "\n Hello Malaysia...";
return;
}

4
Example 2 :
main()

Hello() Welcome()

structured chart
#include <iostream>

using namespace std;


//1. function declaration
void Hello(void);
void Welcome(void);

int main()
{
//declaration of the variable

//2. function caller


Welcome();
Hello();

return 0;
}

//3. function definition


void Hello()
{
cout << "\n Hello friends ... ";
return;
}

void Welcome()
{
cout << "\n Welcome to Malaysia";
return;
}

5
Example 3 :
main()

Line() Welcome()

=====================================
Welcome to Malaysia
=====================================
#include <iostream>

using namespace std;


//1. function declaration / function prototype
void Line(void);
void Welcome(void);

int main()
{

//2. caller function


Line();
Welcome();
Line();//reusable

return 0;
}

//3. function definition


void Line()
{
cout << "\n ================================= ";
return;
}

void Welcome()
{
cout << "\n\t Welcome to Malaysia ";
return;
}

6
Example 4 :
main()

Star() Welcome() Hash()

Expected output :

####################
***
Welcome to Malaysia
***
#####################

#include <iostream>
using namespace std;
void star(void);
void welcome(void);
void hash(void);

int main()
{
hash();
star();
welcome();
star();
hash();

return 0;
}
void star()
{
cout << "\n ****** ";
return;
}

void welcome()
{
cout << "\n\t Welcome to Malaysia ";
return;
}
void hash()
{
cout << "\n ####################### ";
return;
}

7
Example 5 :

Structured Chart

Main()

Line() Hello() Welcome() Star()

#include <iostream>

using namespace std;


//1. declare the function @ function declaration @ function prototype
void Hello(void); //void means no parameter
void Welcome(void);
void Line(void);
void Star(void);

int main() //main function


{
//declaration of the variable

//2. caller function


Line();
Hello();
for (int x=1;x<=10;x++)
{
Welcome();
}
Star();

return 0;
}

//3. function definition


void Star()
{
cout << "\n *********************************** ";
return;
}

void Line()
{

8
cout << "\n ==================================== ";
return;
}

void Hello()
{
cout << "\n HELLO WORLD!!! ";
return; //go back to the main program
}

void Welcome()
{
cout << "\n Welcome to Malaysia ";
return;
}

9
Example 6 :

Main()

Line() Hello() Welcome() ThankYou()

*structured chart

#include <iostream>
using namespace std;
//2. function declaration @ function prototype
void Hello(void);//void means nothing..none of parameter
void Welcome(void);
void Line(void);
void ThankYou(void);

int main()//<-- main function


{
//variable declaration

//1. function caller


Line();
Hello();

for (int x=1;x<=10;x++)


{
Welcome();
}

ThankYou();

return 0;

}//close main program

//3. function definition

void ThankYou()
{
cout << "\n Thank you...see you again ";
return;
}

10
void Line()
{
cout << "\n ======================================";
return;
}

void Welcome()
{
cout << "\n Welcome to Pulau Pinang ";
return;//back to the main program
}

void Hello()
{
cout << "\n Hello world !!! ";
return;//back to the main program
}

11
Function Type 2 : Void function with parameter

Example 1:

The function will submit argument, parameter, values to the function definition

INPUT Main()

x,y

Summation()
PROCESS, OUTPUT

#include<iostream>

using namespace std;


//1. declaration of the function @ function prototype
void Summation(int,int);

int main()//main function


{
//declaration
int x, y;

//input
cout << "\n Enter first number : ";
cin >> x;

cout << "\n Enter second number : ";


cin >> y;

Summation(x,y);//2. caller function

/*
//process
sum = x + y;

//output
cout << "\n The summation is " << sum; */

return 0;

12
}//close main

//3. function definition


void Summation(int x, int y)
{
int sum;

//process
sum = x + y;

//output
cout << "The summation is " << sum;

return;
}

13
Example 2:

Main()

length

width

height

PyramidVol(...)

/* write a complete coding to calculate the volume of


a pyramid
The specification is : enter the length, width and height in the main
program and submit the three values to the function named
PyramidVol(...).

Calculate the pyramid volume and display the result in the function.
Type 2.
*/

#include <iostream>
using namespace std;
//1. function prototype
void PyramidVol(float,float,float);

int main()
{
//declaration of the variables.
float l, w, h;

cout << "\n Enter height of the pyramid : ";


cin >> h;

cout << "\n Enter width of the pyramid : ";


cin >> w;

cout << "\n Enter length of the pyramid : ";

14
cin >> l;

//2. caller function


PyramidVol(h,w,l);

return 0;
}
//3.function definition
void PyramidVol(float h,float w, float l)
{
float vol;

vol = (1/3.0) * h * w * l;

cout << "\n The volume of the pyramid is " << vol;
return;

15
Example 3:

/* Write a complete program to calculate the area of a circle.

Enter the radius of a circle in the main program.

Submit the radius to the function named CircleArea(...).

Calculate the area and display the result in the function */

Main()

radius

CircleArea ()

#include <iostream>
using namespace std;
void AreaCircle(float);

int main()
{
//declaration of the variables.
float r;

cout << "\n Enter radius of the circle : ";


cin >> r;

//2. caller function


AreaCircle(r);

return 0;
}
//3.function definition
void AreaCircle(float r)
{
float area;
const float PI = 3.142;

area = PI * r * r;

cout << "\n The area of circle is " << area;


return;
}

16
Example 4:

Main()

radius

CalcArea ()

/* calculate the surface area of a sphere by using the following formula

area = 4 * PI * pow(radius,2)

enter the radius in the main program

then submit/send the radius to the function named CalcArea(...)

next, calculate the area of a sphere and display the result

of calculation in the function.

*/

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
void CalcArea(float);
int main()
{
float radius;
cout << "\n Enter the radius of a sphere :";
cin >> radius;
CalcArea(radius);
return 0;
}

void CalcArea(float radius)


{
float area;
const float PI = 3.142;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);

area = 4 * PI * pow(radius,2);
cout << "\n The area of a sphere is " << setprecision(2) << area;
return;
}

17
Function Type 3 : Void function with parameter and return values to the main function/program

Example 1 :

INPUT Output
Main()

x,y &sum

x,y

Summation()
PROCESS

PROCESS,
OUTPUT
#include <iostream>

using namespace std;


//function declaration @ function prototype
void Summation(int,int,int&); //type 3 function
//& - pass by reference - values are from the function
//without the & - pass by value - values are from the main function

int main()

{
int x, y, sum=100;

cout << "\n Enter first number : ";


cin >> x;

cout << "\n Enter second number : ";


cin >> y;

Summation(x,y,sum);//caller function
cout << "\n The summation result is " << sum;

return 0;

//function definition
void Summation (int x, int y, int& sum)

18
{

sum = x + y;//process

return;
}

Example 2 :

Main()

Radius, height radius, height volume

ConeVolT2(…) ConeVolT3(…)

/* The purpose of this program is to calculate the volume

of a cone */

#include <iostream>

#include <math.h>//pow

#include <iomanip>//setprecision

using namespace std;

//function prototype

void ConeVolT2(float,float); //type2

void ConeVolT3(float,float,float&);//type3

int main()

float radius, height, vol=0;

cout << "\n Enter radius of a cone : ";

cin >> radius;

19
cout << "\n Enter height of a cone : ";

cin >> height;

//ConeVolT2(radius,height);//function caller;

ConeVolT3(radius,height,vol);

cout << "\n The volume of cone is " << setprecision(2) << vol;

return 0;

//function definition

void ConeVolT3(float radius, float height, float& vol)

const float PI = 3.142;

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

vol = (1/3.0) * PI * pow(radius,2) * height;

return;

void ConeVolT2(float radius, float height)

const float PI = 3.142;

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

float vol;

vol = (1/3.0) * PI * pow(radius,2) * height;

cout << "\n The volume of cone is " << setprecision(2) << vol;

return;

Example 3 :

20
Main()

length,width,height length,width,height volume

CalcVolT2 (…) CalcVolT3 (…)

#include <iostream>
using namespace std;
//function prototype
void CalcVolT2(float,float,float); //type2
void CalcVolT3(float,float,float,float&);//type3

int main()
{
float length,width, height, vol=0;

cout << "\n Enter length of a cuboid : ";


cin >> length;

cout << "\n Enter width of a cuboid : ";


cin >> width;

cout << "\n Enter height of a cuboid : ";


cin >> height;

//function caller;
CalcVolT3(length,width,height,vol);
cout << "\n The volume of cuboid is "<< vol;

return 0;

}
//function definition
void CalcVolT3(float length, float width,float height, float& vol)
{
//float vol;

vol = length *width * height;


return;
}

21
void CalcVolT2(float length,float width, float height)
{
float vol;

vol = length * width * height;


cout << "\n The volume of cuboid is " << vol;
return;
}

22
Function Type 4: non void function with parameter and return values to the main
function/program

Example 1 :

INPUT Output

Main()

x,y &sum

x,y
Summation()
PROCESS

#include <iostream>
PROCESS,
using namespace std; OUTPUT
//1. declaration function @ function prototyoe
int summation(int,int);

int main()
{
//declaration
int x, y,sum;

//input
cout << "\n Enter first number : ";
cin >> x;

cout << "\n Enter second number : ";


cin >> y;

//process
sum = summation(x,y);//2. caller
//sum = x + y;

//output
cout << "\n The summation is " << sum;

return 0;

}
//3. function definition
int summation(int a, int b)
{

23
int sum;
sum = a + b;
//cout << "\n The summation is " << sum;
return sum;
}

Example 2 :

INPUT Output

Main()

radius &vol

x,y
SphereVol ()
PROCESS

PROCESS,
#include <iostream> OUTPUT
#include <math.h>
#include <iomanip>

using namespace std;


float SphereVol(float);//function prototype / declaration
const float PI = 3.142;//global variable

int main()
{
//declaration @ local variable
float radius, vol;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

//input
cout << "\n Enter radius of a sphere : ";
cin >> radius;

//process
vol = SphereVol(radius); //caller

//output
cout << "\n Volume of a sphere is " << setprecision(2) << vol;

return 0;

24
//3. function definition
float SphereVol(float r,float& vol) //local variable
{
//float vol;
//const float PI = 3.142;//local variable
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

vol = (4/3.0) * PI * pow(r,3);


//cout << "\n Volume of a sphere is " << setprecision(2) << vol;

return;
}

Implementation of all types of function

Example 1:

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

//function prototype
void SurfaceAreaT2(float,float);
void SurfaceAreaT3(float,float,float&);
float SurfaceAreaT41(float,float);
float SurfaceAreaT42(float,float);

//global variable
const float PI = 3.142;

int main()
{

float radius, height, area; //local variable

//input
cout << "\n Enter the radius of a cone : ";
cin >> radius;

cout << "\n Enter the height of a cone : ";


cin >> height;

//caller
//SurfaceAreaT2(radius,height);
//SurfaceAreaT3(radius,height,area);
//area = SurfaceAreaT41(radius,height);

25
area = SurfaceAreaT42(radius,height);

//output
cout << "\n Area of the cone is " << setprecision(2) << area;
return 0;
}

//3. function definition


void SurfaceAreaT2(float r,float h)
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
float area;
area = PI * r * h + PI * pow(r,2);
cout << "\n Area of the cone is " << setprecision(2) << area;
return;
}

void SurfaceAreaT3(float r,float h,float& a)


{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
a = PI * r * h + PI * pow(r,2);

return;
}

float SurfaceAreaT41(float r,float h)


{
float a;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
a = PI * r * h + PI * pow(r,2);

return a;
}

float SurfaceAreaT42(float r,float h)


{

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
return (PI * r * h + PI * pow(r,2));

26
Example 2 :

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;


//function declaration @ function prototype
void VolCylinderT2(float, float);
void VolCylinderT3(float, float, float&);
float VolCylinderT41(float, float);
float VolCylinderT42(float, float);
const float PI = 3.142;//global variable

int main()
{
//declare
float radius, height, vol;//local variable
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

//input
cout << "\n Enter the radius of a cylinder : ";
cin >> radius;

cout << "\n Enter the height of a cylinder : ";


cin >> height;

//process //caller
//VolCylinderT2(radius, height);
//VolCylinderT3(radius, height, vol);
//vol = VolCylinderT41(radius, height);
vol = VolCylinderT42(radius, height);
cout << "\n The volume of cylinder is " << setprecision(2) << vol;

return 0;
}
//function definition

void VolCylinderT2(float r, float h) //r, h are also local variable


{
float vol;//local variable
//const float PI = 3.142;//local variable
cout.setf(ios::fixed);
cout.setf(ios::showpoint);

vol = PI * pow(r,2) * h;

cout << "\n The volume of cylinder is " << setprecision(2) << vol;

27
return;
}

void VolCylinderT3(float r, float h, float& vol) //r, h are also local variable
{
//const float PI = 3.142;//local variable
//cout.setf(ios::fixed);
//cout.setf(ios::showpoint);

vol = PI * pow(r,2) * h;
return;
}

float VolCylinderT41(float r, float h) //r, h are also local variable


{
//const float PI = 3.142;//local variable
//cout.setf(ios::fixed);
//cout.setf(ios::showpoint);
float vol;

vol = PI * pow(r,2) * h;

return vol;
}

float VolCylinderT42(float r, float h) //r, h are also local variable


{
return (PI * pow(r,2) * h);
}

28
Example 3 :

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//function prototype
void CalcVolSphereT2(float);
void CalcVolSphereT3(float,float&);
float CalcVolSphereT4(float);
const float PI = 3.142;//global variable

int main()
{
//declare
float radius, vol;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
//const float PI = 3.142;

//input
cout << "\n Enter the radius of a sphere : ";
cin >> radius;

//caller
//CalcVolSphereT2(radius);
//CalcVolSphereT3(radius,vol);
vol = CalcVolSphereT4(radius);

//output
cout << "\n The volume of a sphere is " << setprecision(2) << vol;

return 0;
}
//function definition
float CalcVolSphereT4(float r)
{
float vol;

vol = 4/3.0 * PI * pow(r,3);

return vol;
}

void CalcVolSphereT3(float r,float& vol)


{

vol = 4/3.0 * PI * pow(r,3);

29
return;
}

void CalcVolSphereT2(float r)
{
float vol;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
//const float PI = 3.142;

vol = 4/3.0 * PI * pow(r,3);


cout << "\n The volume of a sphere is " << setprecision(2) << vol;
return;
}

Example 4:

#include <iostream>
#include <iomanip>

using namespace std;


//function declaration
void VolCuboidT2(int,int,int);
void VolCuboidT3(int,int,int, int&);
int VolCuboidT4(int,int,int);

int main()
{
//declaration
int length, width, height, volume;

cout << "\n Enter the length of cuboid : ";


cin >> length;
cout << "\n Enter the width of cuboid : ";
cin >> width;
cout << "\n Enter the height of cuboid : ";
cin >> height;

VolCuboidT2(length,width,height); //caller
VolCuboidT3(length,width,height,volume);
volume = VolCuboidT4(length, width, height);
cout << "\n The volume of cuboid is " <<volume;

return 0;

}
//function deifinition
void VolCuboidT2(int length, int width, int height)
{

30
int volume;

volume = length * width * height;


cout << "\n The volume of cuboid is " <<volume;
return;
}

void VolCuboidT3(int length, int width, int height, int& volume)


{
volume = length * width * height;
return;
}

int VolCuboidT4(int length, int width, int height)


{
int volume;
volume = length * width * height;
return volume;
}

31
CASE STUDY 1

IPO Documentation

Input firstNum, secondNum, option, sambung

Process
while sambung = ‘y’ then
if option = 1 then
result = firstNum + secondNum
else if option = 2 then
result = firstNum – secondNum
else if option = 3 then
result = firstNum * secondNum
else if option = 4 then
result = firstNum / secondNum
else
display “wrong option”

Output result

32
Pseudocode and the flowchart Documentation

Name : Main() Program

Pseudocode Flowchart

1. begin 1
2. declare firstNum, secondNum,
result, option as integer
3. declare sambung as char and 2,3
assign ‘y’

4. while (sambung = ‘y’)


4.1 begin while y
4.2 enter firstNum, secondNum 4 4.2

4.3 call function Menu() 4.3

4.4 enter option 4.4

4.5 if (option = 1) then


4.5.1 begin if 4.5 y 4.5.2
4.5.2 call function
Summation(…) n
4.5.3 end if n
4.6 else if (option = 2) then
4.6.1 begin else if 4.6
y y4.6.2 4.6.3
4.6.2 call function
Subtraction (…) n
4.6.3 display result
4.6.4 end else if
4.7 else if (option = 3) then y y
4.7 4.7.2 4.7.3
4.7.1 begin else if
4.7.2 call function
Multiplication (…) and assign n
to result
4.7.3 display result
4.7.4 end else if 4.8 y 4.8.2
4.8 else if (option = 4) then
4.8.1 begin else if n
4.8.2 call function Division
(…)
4.8.3 end else if 4.9.2
4.9 else
4.9.1 begin else
4.9.2 call ErrorMessage() 3
4.9.3 end else 1
2

33
3 1
2

4.10
4.10 enter sambung
4.11 End while
5. Display thank you
5

6. end 6

Name : Menu

Pseudocode Flowchart

1. begin 1

2. Display List of menu 2

3. End 3

34
Name : void Summation(…)

Pseudocode Flowchart

1. begin 1

2. declare sum as integer


2,3
3. calculate sum = a + b

4. display sum
4

5. End 5

Name : void Subtraction (…)

Pseudocode Flowchart

1. begin 1

2. calculate c = a - b 2

3. End
3

35
Name : int Multiplication(…)

Pseudocode Flowchart

1. begin 1

2. declare multi as integer


2,3,4
3. calculate multi = a * b

4. return multi

5. End 5

Name : void Division(…)

Pseudocode Flowchart

1. begin 1

2. declare result as integer


2,3
3. calculate result = a / b

4. display result
4

5. End 5

Name : ErrorMessage()

Pseudocode Flowchart

1. begin 1

2. Display “Invalid option “ 2

3. End 3

36
Coding :

#include <iostream>

using namespace std;


//function declaration @ function prototype
void Menu(void); //type 1 function
void Summation(int,int);//type 2 function
void Subtraction(int,int,int&);//type 3 function
int Multiplication(int,int);//type 4 function
void Division(int,int);//type 2 function
void ErrorMessage(void);//type 1 function

int main()
{
//declaration of the variable
int firstNum, secondNum, result, option;
char sambung='y';

while ((sambung == 'y') || (sambung == 'Y'))


{

//input
cout << "\n Enter first number : ";
cin >> firstNum;

cout << "\n Enter second number : ";


cin >> secondNum;

//caller
Menu();
cout << "\n Please make your option :";
cin >> option;

if (option == 1)
Summation(firstNum,secondNum);//Type 2
else if (option == 2)
{
Subtraction(firstNum,secondNum,result);//type 3
cout << "\n " << firstNum << " - " << secondNum << " = " << result;
}
else if (option == 3)
{
result = Multiplication(firstNum, secondNum);//type 4
cout << "\n " << firstNum << " x " << secondNum << " = " << result;
}
else if (option == 4)
Division(firstNum,secondNum);//type 2
else

37
ErrorMessage();//type 1

cout << "\n Do you want to continue [y/n] : ";


cin >> sambung;
cin.ignore(80,'\n');

}//close while

cout << "\n Thank you.... ";

return 0;

}
//function definition
void Menu()//type 1
{
cout << "\n *** Calculator Application *** ";
cout << "\n 1. Summation ";
cout << "\n 2. Subtraction ";
cout << "\n 3. Multiplication ";
cout << "\n 4. Division ";

return;
}

void Summation(int a, int b) //type 2


{
int sum;

sum = a + b;
cout << "\n The summation of " << a << " and " << b << " is " << sum;
return;
}

void Subtraction(int a, int b, int& c)//type 3


{
c = a - b;
return;
}

int Multiplication(int a, int b)//type 4


{
int multi;
multi = a * b;
return multi;
}

void Division(int a, int b)


{

38
int result;

result = a / b;

cout << "\n The division of " << a << " / " << b << " is " << result;
return;
}

void ErrorMessage()
{
cout << "\n You have entered the wrong option ";
return;
}

Sample output :

Sample 1

Sample 2

39
Sample 3

40
CASE STUDY 2

i) Documentation of IPO (Program specification)

Input height, weight, sambung


Process while sambung = ‘y’

bmi = weight / (height * height)

if (bmi > 0 and bmi <=18.5)


status = “underweight”
else if (bmi > 18.5 and bmi <=24.9)
status = “normal”
else if (bmi > 24.9 and bmi <=29.9)
status = “overweight”
else
status = “obese”

Output bmi, status

41
ii) Documentation of Pseudocode and Flowchart
a) Main program

Pseudocode Flowchart

1. Begin 1

2. Declare height, weight, bmi as float


3. Declare sambung as char and assign 2,3
‘y’ to it

4. while (sambung = ‘y’ or sambung =’Y’) y


4.1 begin while 4
4.2 call function Welcome() 4.2

4.3 enter height and weight 4.3

4.4 bmi = call function 4.4


CalcBMI(height,weight)

n
4.5 display bmi 4.5

4.6 call function 4.6


DetermineStatus(bmi)

4.7 enter sambung 4.7


4.8 end while

5. display ‘ thank you’ 5

6
6. end

42
b) welcome() function

Pseudocode Flowchart
1.begin
1

2. display “WELCOME TO BMI


CALCULATOR” 2

3
3. end

c) CalcBMI (…) function

Pseudocode Flowchart
1.begin
1

2. declare bmi as float 2,3


3. calculate bmi = w / (h * h)

4. end 4

43
d) DetermineStatus (…) function

Pseudocode Flowchart
1.begin
1

2. declare status as char 2

3. if (bmi > 0 and bmi <= 18.5) then y


3.1 begin if 3 3.2
3.2 assign status = “underweight”
3.3 end if n

4. else if (bmi > 18.5 and bmi <= 24.9) then


4.1 begin else if 4 y 4.2
4.2 assign status = “normal”
4.3 end else if
n
5. else if (bmi > 24.9 and bmi <= 29.9) then
5.1 begin else if y 5.2
5
5.2 asign status = “Overweight”
5.3 end else if
6. else n
6.1 begin else
6.2 assign status = “Obese” 6.2
6.3 end else

7. Display status 7

8. end
8

44
iii) Coding

#include <iostream>

#include <string.h>

#include <iomanip>

using namespace std;

//function declaration @ function prototype

void welcome(void);//type1

float CalcBMI(float,float);//type4

void DetermineStatus(float);//type2

int main()

//declaration

float height, weight, bmi;//local variables

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

char sambung ='y';

//while (!(sambung =='n' or sambung =='N')) //sentinel loop

while (sambung == 'y' or sambung == 'Y')

welcome();//caller - function type1

//input

cout << "\n Enter height(m) : ";

cin >> height;

cout << "\n Enter weight (kg) : ";

cin >> weight;

45
bmi = CalcBMI(height,weight);//caller - function type4

cout << "\n The bmi is " <<setprecision(2) << bmi;

DetermineStatus(bmi);//caller - function type2

cout << "\n Do you want to continue[y/n] : ";

cin >> sambung;

cin.ignore(80,'\n');

}//close while

cout << "\n Thank you...";

return 0;

//function definition

void welcome()

cout << "\n\t\t WELCOME TO BMI CALCULATOR ";

return;

float CalcBMI(float h, float w)//w,h are local variable

float bmi;

bmi = w / (h * h);

return bmi;

46
void DetermineStatus(float bmi)// bmi is local variable

char status[20];//local variable

if ((bmi > 0) and (bmi <= 18.5 ))

strcpy(status,"Underweight");

else if ((bmi > 18.6) and (bmi <= 24.9 ))

strcpy(status,"Normal");

else if ((bmi > 24.9) and (bmi <= 29.9 ))

strcpy(status,"Overweight");

else

strcpy(status,"Obese");

cout << "\n The status of BMI is " << status;

return;

47
iv) Sample of output

Sample 1

Sample 2

48
Sample 3

49
REVISION 1:

Answer 1:

#include <iostream>

using namespace std;

float rectArea(float,float);//type4

float triArea(float,float);//type4

int main()

float height, length, base;

float rArea, tArea, rtArea;

cout << "\n Enter height : ";

cin >> height;

cout << "\n Enter length : ";

50
cin >> length;

cout << "\n Enter base : ";

cin >> base;

rArea = rectArea(length,height);//caller function

tArea = triArea(base, height);

rtArea = rArea + tArea;

//rtArea = rectArea(length,height) + triArea(base, height);

cout << "\n Rectangle area " << rArea;

cout << "\n Triangle area " << tArea;

cout << "\n Overall area " << rtArea;

return 0;

//function definition

float rectArea(float length, float height)

float area;//local variable

area = length * height;

return area;

float triArea(float base,float height)

float area;//local variable

area = 0.5*base*height;

return area;

51
Answer 2:

#include <iostream>

using namespace std;

void rectArea(float,float, float&);//type3

void triArea(float,float, float&);//type3

int main()

float height, length, base;

float rArea, tArea, rtArea;

cout << "\n Enter height : ";

cin >> height;

cout << "\n Enter length : ";

cin >> length;

cout << "\n Enter base : ";

cin >> base;

rectArea(length,height, rArea);//caller function

triArea(base, height, tArea);

rtArea = rArea + tArea;

//rtArea = rectArea(length,height) + triArea(base, height);

cout << "\n Rectangle area " << rArea;

cout << "\n Triangle area " << tArea;

cout << "\n Overall area " << rtArea;

return 0;

52
//function definition

void rectArea(float length, float height, float& area)

area = length * height;

return;

void triArea(float base,float height, float& area)

area = 0.5*base*height;

return;

Answer 3:

#include <iostream>

using namespace std;

//function definition

void rectArea(float length, float height, float& area)//function header

area = length * height;

return;

void triArea(float base,float height, float& area)

53
area = 0.5*base*height;

return;

int main()

float height, length, base;

float rArea, tArea, rtArea;

cout << "\n Enter height : ";

cin >> height;

cout << "\n Enter length : ";

cin >> length;

cout << "\n Enter base : ";

cin >> base;

rectArea(length,height, rArea);//caller function

triArea(base, height, tArea);

rtArea = rArea + tArea;

//rtArea = rectArea(length,height) + triArea(base, height);

cout << "\n Rectangle area " << rArea;

cout << "\n Triangle area " << tArea;

cout << "\n Overall area " << rtArea;

return 0;

54
REVISION 2:

Answer 1:

#include <iostream>

using namespace std ;

double calDeposit (double) ;

double calLegalfee (double) ;

double calTotal (double, double) ;

55
int main ()

double hPrice, deposit, legFee, tot ;

cout << " \n Please enter house price (RM) : " ;

cin >> hPrice ;

deposit = calDeposit (hPrice) ;

legFee = calLegalfee (hPrice) ;

tot = calTotal (deposit, legFee) ;

cout << " \n ***************** " ;

cout << " \n HOMEBUYING CALCULATOR " ;

cout << " \n " ;

cout << " \n House Price (RM) : " << hPrice ;

cout << " \n Amount of deposit (RM) : " << deposit ;

cout << " \n Legal fee charge (RM) : " << legFee ;

cout << " \n Total money to be prepared (RM) : " << tot ;

cout << " \n " ;

cout << " \n Thank you for using the program... " ;

cout << " \n ***************** " ;

double calDeposit (double hPrice)

double deposit ;

deposit = 0.10 * hPrice ;

56
return deposit ;

double calLegalfee (double hPrice)

double legFee ;

if (hPrice <= 200000)

legFee = 0.01 * hPrice ;

else

legFee = 0.005 * hPrice ;

return legFee ;

double calTotal (double deposit, double legFee)

double tot ;

tot = deposit + legFee ;

return tot ;

57
Answer 2:

#include<iostream>

using namespace std;

void calDeposit(double,double&);

void calLegalfee(double, double&);

void calTotal(double, double, double&);

int main()

double hPrice, dep, legFee, totMon;

cout<<"\n Please enter house price (RM): ";

cin>>hPrice;

calDeposit(hPrice, dep);

calLegalfee(hPrice, legFee);

calTotal(dep, legFee, totMon);

cout<<"\n\n *******************";

cout<<"\n HOMEBUYING CALCULATOR";

cout<<"\n\n House Price (RM) : "<<hPrice;

cout<<"\n Amount of deposit (RM) : "<<dep;

cout<<"\n Legal fee charge (RM) : "<<legFee;

cout<<"\n Total money to be prepared (RM) : "<<totMon;

cout<<"\n\n Thank you for using the program...";

cout<<"\n *******************";

return 0;

58
//a

//receive:double hPrice;

//return:double dep;

void calDeposit(double hPrice, double& dep)

dep=0.1*hPrice;

return;

//b

//receive:double hPrice;

//return:double legFee;

void calLegalfee(double hPrice, double& legFee)

if(hPrice<=200000)

legFee=0.01*hPrice;

else

legFee=0.005*hPrice;

return;

59
//c

//receive:double dep, double legFee;

//return:double totMon;

void calTotal(double dep, double legFee, double& totMon)

totMon=dep+legFee;

return;

60
REVISION 3:

61
Answer 1 :

#include <iostream>

using namespace std;

//function prototype

int calcCoins1(int,int,int,int);

int calcCoins2(int,int);

int main()

int ml10, ml15, md10, md15, detergent, softener; //local variable

int totAllCoins;

cout << "\n Enter the number of 10 kg laundry machine used : ";

cin >> ml10;

cout << "\n Enter the number of 15 kg laundry machine used : ";

cin >> ml15;

62
cout << "\n Enter the number of 10 kg dryer machine used : ";

cin >> md10;

cout << "\n Enter the number of 15 kg dryer machine used : ";

cin >> md15;

cout << "\n Enter the number of detergent : ";

cin >> detergent;

cout << "\n Enter the number of softener : ";

cin >> softener;

totAllCoins = calcCoins1(ml10,ml15,md10,md15) + calcCoins2(detergent, softener);

cout << "\n The total number of 50 cents coins needed : " << totAllCoins;

return 0;

//function definition

int calcCoins1(int ml10,int ml15,int md10,int md15)

int totCoins;

float price;

price = ml10 * 4.50 + ml15 * 6.00 + md10 * 3.50 + md15 * 4.00;

totCoins = price / 0.50;

return totCoins;

63
int calcCoins2(int detergent, int softener)

int totCoins;

float price;

price = detergent * 1 + softener * 1;

totCoins = price / 0.50;

return totCoins;

Answer 2:

#include <iostream>

using namespace std;

//function definition

int calcCoins1(int ml10,int ml15,int md10,int md15)

int totCoins;

float price;

price = ml10 * 4.50 + ml15 * 6.00 + md10 * 3.50 + md15 * 4.00;

totCoins = price / 0.50;

return totCoins;

64
int calcCoins2(int detergent, int softener)

int totCoins;

float price;

price = detergent * 1 + softener * 1;

totCoins = price / 0.50;

return totCoins;

int main()

int ml10, ml15, md10, md15, detergent, softener; //local variable

int totAllCoins;

cout << "\n Enter the number of 10 kg laundry machine used : ";

cin >> ml10;

cout << "\n Enter the number of 15 kg laundry machine used : ";

cin >> ml15;

cout << "\n Enter the number of 10 kg dryer machine used : ";

cin >> md10;

cout << "\n Enter the number of 15 kg dryer machine used : ";

cin >> md15;

65
cout << "\n Enter the number of detergent : ";

cin >> detergent;

cout << "\n Enter the number of softener : ";

cin >> softener;

totAllCoins = calcCoins1(ml10,ml15,md10,md15) + calcCoins2(detergent, softener);

cout << "\n The total number of 50 cents coins needed : " << totAllCoins;

return 0;

66
REVISION 4:

67
Answer 1 :

#include <iostream>

#include <iomanip>

using namespace std;

//function declaration or function prototype

float carType(char);

float getDiscount(int,float);

int main()

//declaration

char name[100], address[100],telNumber[20],code;

int days;

float rentalRate, afterDiscount, priceAfterDiscount;

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout << "\n KENYALANG CAR RENTAL ";

cout << "\n ----------------------";

cout << "\n Please enter customer name : ";

cin.get(name,100);

cin.ignore(80,'\n');

cout << "\n Please enter address : ";

cin.get(address,100);

cin.ignore(80,'\n');

68
cout << "\n Please enter telephone number : ";

cin.get(telNumber,100);

cin.ignore(80,'\n');

cout << "\n Please enter number of days : ";

cin >> days;

cin.ignore(80,'\n');

cout << "\n Please enter car code (V-Viva, M-MyVi, P-Proton) : ";

cin >> code;

cin.ignore(80,'\n');

rentalRate = carType(code);//caller

cout << "\n PAYMENT INFO CAR RENTAL ";

cout << "\n ----------------------";

cout << "\n Customer name : " << name;

cout << "\n Address : " << address;

cout << "\n Telephone number : " << telNumber;

cout << "\n Number of days : " << days;

cout << "\n Car code : " << code;

cout << "\n Rental rate : RM " << setprecision(2) << rentalRate;

afterDiscount = getDiscount(days,rentalRate);

cout << "\n Rental after discount : RM " << setprecision(2) << afterDiscount;

priceAfterDiscount = rentalRate * days - afterDiscount;

cout << "\n Price after discount : RM " << setprecision(2) << priceAfterDiscount;

return 0;

69
}

//function definition

float getDiscount(int days,float rentalRate)

float discount;

if (days >= 4)

discount = 0.10 * rentalRate * days;

else

discount = 0;

return discount;

float carType(char code)

float rental;

if (code == 'V' or code == 'v')

cout << "\n Car type : Viva ";

rental = 75.00;

else if (code == 'M' or code == 'm')

cout << "\n Car type : MyVi ";

rental = 90.00;

70
}

else if (code == 'P' or code == 'p')

cout << "\n Car type : Proton ";

rental = 110.00;

else

cout << "\n Car type : Invalid Code ";

rental = 0;

return rental;

71
Answer 2 :

#include <iostream>

#include <iomanip>

using namespace std;

//function declaration or function prototype

void carType(char, float&);

void getDiscount(int,float, float&);

int main()

//declaration

char name[100], address[100],telNumber[20],code;

int days;

float rentalRate, afterDiscount, priceAfterDiscount;

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout << "\n KENYALANG CAR RENTAL ";

cout << "\n ----------------------";

cout << "\n Please enter customer name : ";

cin.get(name,100);

cin.ignore(80,'\n');

cout << "\n Please enter address : ";

cin.get(address,100);

cin.ignore(80,'\n');

cout << "\n Please enter telephone number : ";

72
cin.get(telNumber,100);

cin.ignore(80,'\n');

cout << "\n Please enter number of days : ";

cin >> days;

cin.ignore(80,'\n');

cout << "\n Please enter car code (V-Viva, M-MyVi, P-Proton) : ";

cin >> code;

cin.ignore(80,'\n');

carType(code, rentalRate);//caller

cout << "\n PAYMENT INFO CAR RENTAL ";

cout << "\n ----------------------";

cout << "\n Customer name : " << name;

cout << "\n Address : " << address;

cout << "\n Telephone number : " << telNumber;

cout << "\n Number of days : " << days;

cout << "\n Car code : " << code;

cout << "\n Rental rate : RM " << setprecision(2) << rentalRate;

getDiscount(days,rentalRate, afterDiscount);

cout << "\n Rental after discount : RM " << setprecision(2) << afterDiscount;

priceAfterDiscount = rentalRate * days - afterDiscount;

cout << "\n Price after discount : RM " << setprecision(2) << priceAfterDiscount;

return 0;

73
//function definition

void getDiscount(int days,float rentalRate, float& discount)

if (days >= 4)

discount = 0.10 * rentalRate * days;

else

discount = 0;

return;

void carType(char code, float& rental)

if (code == 'V' or code == 'v')

cout << "\n Car type : Viva ";

rental = 75.00;

else if (code == 'M' or code == 'm')

cout << "\n Car type : MyVi ";

rental = 90.00;

else if (code == 'P' or code == 'p')

74
cout << "\n Car type : Proton ";

rental = 110.00;

else

cout << "\n Car type : Invalid Code ";

rental = 0;

return;

75
Discussion/Revision on Part B (Function)

i) Function prototype /declare

void boxVolume(int,int,int,int&);//type 3

int boxVolume(int,int,int);//type 4

ii) Statement to call the function

int main()

boxVolume(length,width,height,vol);//type 3

vol = boxVolume(length,width,height);//type 4

iii) Function header

//function definition

void boxVolume(int l, int w, int h, int& v) ➔ function header //type 3

v = l * w * h;

return;

int boxVolume(int l, int w, int h) ➔ function header //type 4

int v;

v = l * w * h;

return v;

76
int main()

//answer 1:

float amount,discount,afterdiscount;

amount = getBillAmount();

discount = getDiscount(amount);

afterdiscount = amount – discount;

cout << afterdiscount;

//answer 2 :

float amount;

amount = getBillAmount();

cout << amount - getDiscount(amount);

77
Answer :

i) int findRadius()
ii) void displayKM(int a)
iii) void calArea(int width, int& area)

int main()

Answer 1 :

int sideOuter, sideInner, areaOuter, areaInner, shaded;

cin >> sideOuter;

cin >> sideInner;

areaOuter = areaSquare(sideOuter);

areaInner = areaSquare(sideInner);

78
shaded = areaOuter – areaInner;

cout << shaded;

Answer 2 :

int sideOuter, sideInner;

cin >> sideOuter;

cin >> sideInner;

cout << areaSquare(sideOuter) – areaSquare(sideInner);

i) float calcComm(float amount, float rate)

void calcComm(float amount, float rate, float& salesComm)

ii) void displayMonth(int month, int year)


iii) void convertLetter(char& letter)

79
int main()

float radius1, radius2;

cin >> radius1;//outercircle

cin >> radius2;//innercircle

cout << circleArea(radius1) – circleArea(radius2);

80
81

You might also like