CSC425_Chapter5 (2)
CSC425_Chapter5 (2)
• 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
o #include <string.h>
▪ strcpy()
char name[100];
or
string name;
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');
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];
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;
return 0;
}
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
4
Example 2 :
main()
Hello() Welcome()
structured chart
#include <iostream>
int main()
{
//declaration of the variable
return 0;
}
void Welcome()
{
cout << "\n Welcome to Malaysia";
return;
}
5
Example 3 :
main()
Line() Welcome()
=====================================
Welcome to Malaysia
=====================================
#include <iostream>
int main()
{
return 0;
}
void Welcome()
{
cout << "\n\t Welcome to Malaysia ";
return;
}
6
Example 4 :
main()
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()
#include <iostream>
return 0;
}
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()
*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);
ThankYou();
return 0;
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>
//input
cout << "\n Enter first number : ";
cin >> x;
/*
//process
sum = x + y;
//output
cout << "\n The summation is " << sum; */
return 0;
12
}//close main
//process
sum = x + y;
//output
cout << "The summation is " << sum;
return;
}
13
Example 2:
Main()
length
width
height
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;
14
cin >> 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:
Main()
radius
CircleArea ()
#include <iostream>
using namespace std;
void AreaCircle(float);
int main()
{
//declaration of the variables.
float r;
return 0;
}
//3.function definition
void AreaCircle(float r)
{
float area;
const float PI = 3.142;
area = PI * r * r;
16
Example 4:
Main()
radius
CalcArea ()
area = 4 * PI * pow(radius,2)
*/
#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;
}
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>
int main()
{
int x, y, sum=100;
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()
ConeVolT2(…) ConeVolT3(…)
of a cone */
#include <iostream>
#include <math.h>//pow
#include <iomanip>//setprecision
//function prototype
void ConeVolT3(float,float,float&);//type3
int main()
19
cout << "\n Enter height of a cone : ";
//ConeVolT2(radius,height);//function caller;
ConeVolT3(radius,height,vol);
cout << "\n The volume of cone is " << setprecision(2) << vol;
return 0;
//function definition
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
return;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
float vol;
cout << "\n The volume of cone is " << setprecision(2) << vol;
return;
Example 3 :
20
Main()
#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;
//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;
21
void CalcVolT2(float length,float width, float height)
{
float vol;
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;
//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>
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);
return;
}
Example 1:
#include <iostream>
#include <math.h>
#include <iomanip>
//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()
{
//input
cout << "\n Enter the radius of a cone : ";
cin >> radius;
//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;
}
return;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
a = PI * r * h + PI * pow(r,2);
return a;
}
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>
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;
//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
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;
}
vol = PI * pow(r,2) * h;
return vol;
}
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;
return vol;
}
29
return;
}
void CalcVolSphereT2(float r)
{
float vol;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
//const float PI = 3.142;
Example 4:
#include <iostream>
#include <iomanip>
int main()
{
//declaration
int length, width, height, volume;
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;
31
CASE STUDY 1
IPO Documentation
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
Pseudocode Flowchart
1. begin 1
2. declare firstNum, secondNum,
result, option as integer
3. declare sambung as char and 2,3
assign ‘y’
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
3. End 3
34
Name : void Summation(…)
Pseudocode Flowchart
1. begin 1
4. display sum
4
5. End 5
Pseudocode Flowchart
1. begin 1
2. calculate c = a - b 2
3. End
3
35
Name : int Multiplication(…)
Pseudocode Flowchart
1. begin 1
4. return multi
5. End 5
Pseudocode Flowchart
1. begin 1
4. display result
4
5. End 5
Name : ErrorMessage()
Pseudocode Flowchart
1. begin 1
3. End 3
36
Coding :
#include <iostream>
int main()
{
//declaration of the variable
int firstNum, secondNum, result, option;
char sambung='y';
//input
cout << "\n Enter first number : ";
cin >> firstNum;
//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
}//close while
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;
}
sum = a + b;
cout << "\n The summation of " << a << " and " << b << " is " << sum;
return;
}
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
41
ii) Documentation of Pseudocode and Flowchart
a) Main program
Pseudocode Flowchart
1. Begin 1
n
4.5 display bmi 4.5
6
6. end
42
b) welcome() function
Pseudocode Flowchart
1.begin
1
3
3. end
Pseudocode Flowchart
1.begin
1
4. end 4
43
d) DetermineStatus (…) function
Pseudocode Flowchart
1.begin
1
7. Display status 7
8. end
8
44
iii) Coding
#include <iostream>
#include <string.h>
#include <iomanip>
void welcome(void);//type1
float CalcBMI(float,float);//type4
void DetermineStatus(float);//type2
int main()
//declaration
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
//input
45
bmi = CalcBMI(height,weight);//caller - function type4
cin.ignore(80,'\n');
}//close while
return 0;
//function definition
void welcome()
return;
float bmi;
bmi = w / (h * h);
return bmi;
46
void DetermineStatus(float bmi)// bmi is local variable
strcpy(status,"Underweight");
strcpy(status,"Normal");
strcpy(status,"Overweight");
else
strcpy(status,"Obese");
return;
47
iv) Sample of output
Sample 1
Sample 2
48
Sample 3
49
REVISION 1:
Answer 1:
#include <iostream>
float rectArea(float,float);//type4
float triArea(float,float);//type4
int main()
50
cin >> length;
return 0;
//function definition
return area;
area = 0.5*base*height;
return area;
51
Answer 2:
#include <iostream>
int main()
return 0;
52
//function definition
return;
area = 0.5*base*height;
return;
Answer 3:
#include <iostream>
//function definition
return;
53
area = 0.5*base*height;
return;
int main()
return 0;
54
REVISION 2:
Answer 1:
#include <iostream>
55
int main ()
cout << " \n Legal fee charge (RM) : " << legFee ;
cout << " \n Total money to be prepared (RM) : " << tot ;
cout << " \n Thank you for using the program... " ;
double deposit ;
56
return deposit ;
double legFee ;
else
return legFee ;
double tot ;
return tot ;
57
Answer 2:
#include<iostream>
void calDeposit(double,double&);
int main()
cin>>hPrice;
calDeposit(hPrice, dep);
calLegalfee(hPrice, legFee);
cout<<"\n\n *******************";
cout<<"\n *******************";
return 0;
58
//a
//receive:double hPrice;
//return:double dep;
dep=0.1*hPrice;
return;
//b
//receive:double hPrice;
//return:double legFee;
if(hPrice<=200000)
legFee=0.01*hPrice;
else
legFee=0.005*hPrice;
return;
59
//c
//return:double totMon;
totMon=dep+legFee;
return;
60
REVISION 3:
61
Answer 1 :
#include <iostream>
//function prototype
int calcCoins1(int,int,int,int);
int calcCoins2(int,int);
int main()
int totAllCoins;
cout << "\n Enter the number of 10 kg laundry machine used : ";
cout << "\n Enter the number of 15 kg laundry machine used : ";
62
cout << "\n Enter the number of 10 kg dryer machine used : ";
cout << "\n Enter the number of 15 kg dryer machine used : ";
cout << "\n The total number of 50 cents coins needed : " << totAllCoins;
return 0;
//function definition
int totCoins;
float price;
return totCoins;
63
int calcCoins2(int detergent, int softener)
int totCoins;
float price;
return totCoins;
Answer 2:
#include <iostream>
//function definition
int totCoins;
float price;
return totCoins;
64
int calcCoins2(int detergent, int softener)
int totCoins;
float price;
return totCoins;
int main()
int totAllCoins;
cout << "\n Enter the number of 10 kg laundry machine used : ";
cout << "\n Enter the number of 15 kg laundry machine used : ";
cout << "\n Enter the number of 10 kg dryer machine used : ";
cout << "\n Enter the number of 15 kg dryer machine used : ";
65
cout << "\n Enter the number of detergent : ";
cout << "\n The total number of 50 cents coins needed : " << totAllCoins;
return 0;
66
REVISION 4:
67
Answer 1 :
#include <iostream>
#include <iomanip>
float carType(char);
float getDiscount(int,float);
int main()
//declaration
int days;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cin.get(name,100);
cin.ignore(80,'\n');
cin.get(address,100);
cin.ignore(80,'\n');
68
cout << "\n Please enter telephone number : ";
cin.get(telNumber,100);
cin.ignore(80,'\n');
cin.ignore(80,'\n');
cout << "\n Please enter car code (V-Viva, M-MyVi, P-Proton) : ";
cin.ignore(80,'\n');
rentalRate = carType(code);//caller
cout << "\n Rental rate : RM " << setprecision(2) << rentalRate;
afterDiscount = getDiscount(days,rentalRate);
cout << "\n Rental after discount : RM " << setprecision(2) << afterDiscount;
cout << "\n Price after discount : RM " << setprecision(2) << priceAfterDiscount;
return 0;
69
}
//function definition
float discount;
if (days >= 4)
else
discount = 0;
return discount;
float rental;
rental = 75.00;
rental = 90.00;
70
}
rental = 110.00;
else
rental = 0;
return rental;
71
Answer 2 :
#include <iostream>
#include <iomanip>
int main()
//declaration
int days;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cin.get(name,100);
cin.ignore(80,'\n');
cin.get(address,100);
cin.ignore(80,'\n');
72
cin.get(telNumber,100);
cin.ignore(80,'\n');
cin.ignore(80,'\n');
cout << "\n Please enter car code (V-Viva, M-MyVi, P-Proton) : ";
cin.ignore(80,'\n');
carType(code, rentalRate);//caller
cout << "\n Rental rate : RM " << setprecision(2) << rentalRate;
getDiscount(days,rentalRate, afterDiscount);
cout << "\n Rental after discount : RM " << setprecision(2) << afterDiscount;
cout << "\n Price after discount : RM " << setprecision(2) << priceAfterDiscount;
return 0;
73
//function definition
if (days >= 4)
else
discount = 0;
return;
rental = 75.00;
rental = 90.00;
74
cout << "\n Car type : Proton ";
rental = 110.00;
else
rental = 0;
return;
75
Discussion/Revision on Part B (Function)
void boxVolume(int,int,int,int&);//type 3
int boxVolume(int,int,int);//type 4
int main()
boxVolume(length,width,height,vol);//type 3
vol = boxVolume(length,width,height);//type 4
//function definition
v = l * w * h;
return;
int v;
v = l * w * h;
return v;
76
int main()
//answer 1:
float amount,discount,afterdiscount;
amount = getBillAmount();
discount = getDiscount(amount);
//answer 2 :
float amount;
amount = getBillAmount();
77
Answer :
i) int findRadius()
ii) void displayKM(int a)
iii) void calArea(int width, int& area)
int main()
Answer 1 :
areaOuter = areaSquare(sideOuter);
areaInner = areaSquare(sideInner);
78
shaded = areaOuter – areaInner;
Answer 2 :
79
int main()
80
81