CSC425 Chapter5 VoidWithout WithParameter
CSC425 Chapter5 VoidWithout WithParameter
int main()
{
int num1, num2, option; //local variable
welcome(); //caller
cout << "\n Which operation that you prefer to process both numbers?";
cout << "\n 1. Summation ";
cout << "\n 2. Multiplication ";
cout << "\n Option : ";
cin >> option;
if (option == 1)
{
summation(num1,num2);
}
else if (option == 2)
{
1
multiplication(num1,num2);
}
else
cout << "\n You select the wrong option";
}//close while
thankyou();
//function definition
void welcome()
{
cout << "\n Welcome to calculator application ";
return;
}
//3 2
void summation(int num1, int num2)
{
int result; //local variable
//5 3 + 2
result = num1 + num2;
// 3 2 5
cout << "\n\nThe summation of " << num1 << " and " << num2 << " is " << result;
return;
2
void multiplication (int num1, int num2)
{
int result;
cout << "\n\nThe multiplication of " << num1 << " and " << num2 << " is " << result;
return;
void thankyou()
{
cout << "\n Thank you for using the calculator application ";
return;
}
3
CSC425 [CHAPTER 5 - SAMPLE OF CODING 2]
#include <iostream>
#include <iomanip>
void welcome(void);
void menu (void);
void calculate(float, int);
void thankyou (void);
int main()
{
if (foods == 1)
price = 6.50;
else if (foods == 2)
price = 6.70;
else if (foods == 3)
price = 7.00;
else{
price = 0.00;
cout << "\n You have selected the wrong menu " ;
}
4
cout << "\n Quantity of foods ordered : ";
cin >> quantity;
calculate(price, quantity);//caller
cout << "\n Do you want to continue for the next order [y/n]: ";
cin >> cont;
}
thankyou();
return 0;
}
void welcome()
{
cout << "\n Welcome to ABC Restaurant Ordering Systems ";
return;
}
void menu()
{
cout << "\n List of menu ";
cout << "\n 1. Mee Goreng ";
cout << "\n 2. Bihun Goreng ";
cout << "\n 3. Nasi Goreng ";
return;
}
5
void thankyou()
{
cout << "\n Thank you, please come again";
return;
6
Preparation of IPO and Algorithm Documentations
IPO
Input : foodOption, quantity, continue
function welcome()
display ” Welcome to ABC Restaurant Ordering Systems”
function menu()
display “List of menu”
display “1. Mee Goreng”
display “2. Bihun Goreng”
display “3. Nasi Goreng”
function calculate(price,quantity)
totPrice = price * quantity
display totPrice
function thankyou()
display “thank you, please come again”
Output : totPrice
7
Pseudocode & Flowchart
Main Program()
Pseudocode Flowchart
1
1. Begin
2. Declare function void
welcome(), void menu(), void
calculate(int,int) and void
thankyou()
3. Declare quantity, foodOption as 2,3,4,5
integer
4. Declare price as float
5. Declare cont as char and assign
‘y’
6. while cont = ‘y’ then y
6 6.2, 6.3
6.1 begin while
6.2 call welcome() function
6.3 call menu() function
6.4 enter foodOption
6.4
6.5 if foodOption = 1 then
6.5.1 begin if
6.5.2 assign price = 6.50 y
6.5 6.5.2
6.5.3 end if
6.6 else if foodOption = 2 then n
6.6.1 begin else if y
6.6.2 assign price = 6.70 6.6 6.6.2
6.6.3 end else if
6.7 else if foodOption = 3 then n
6.7.1 begin else if y
6.7 6.7.2
6.7.2 assign price = 7.00
6.7.3 end else if n
6.8 else
6.8.1 begin else 6.8.2
6.8.2 assign price = 0
6.8.3 display “You have 6.8.3
selected wrong
menu”
6.8.4 end else O3 O1 O2
8
O3 O1 O2
6.9
6.9 enter quantity
8. end 8
9
Pseudocode & Flowchart
Function void Welcome()
Pseudocode Flowchart
1. begin 1
2. display “Welcome to ABC
Restaurant Ordering Systems”
2
3. end
Pseudocode Flowchart
1. begin 1
2. display “List of menu”
3. display “1. Mee Goreng “
2,3,4,5
4. display “2. Bihun Goreng”
5. display “3. Nasi Goreng”
6. end 6
Pseudocode Flowchart
1. begin 1
2. display “Thank you, please
come again”
2
3. end
10
Pseudocode & Flowchart
Function void calculate(price,quantity)
Pseudocode Flowchart
1. begin 1
2. declare totPrice as float
3. calculate totPrice = price *
2, 3
quantity
4. display totprice
5. end 4
11
Example of Coding 3 : Send parameters to the function and return the result of
calculation from the function to the main program through its parameter.
#include <iostream>
#include <iomanip>
void welcome(void);
void menu (void);
void calculate(float, int);//
void calculate2(float,int,float&); //without & means the value goes from main program
to the function
void thankyou (void); //with & means the result goes from function to the main
program
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
int foods, quantity;
float price, totPrice;
char cont = 'y';
welcome();
menu();
cout << "\n Choose your menu : ";
cin >> foods;
if (foods == 1)
price = 6.50;
else if (foods == 2)
price = 6.70;
else if (foods == 3)
12
price = 7.00;
else{
price = 0.00;
cout << "\n You have selected the wrong menu " ;
}
cout << "\n Do you want to continue for the next order [y/n]: ";
cin >> cont;
}
thankyou();
return 0;
}
void welcome()
{
cout << "\n Welcome to ABC Restaurant Ordering Systems ";
return;
}
void menu()
{
cout << "\n List of menu ";
cout << "\n 1. Mee Goreng ";
cout << "\n 2. Bihun Goreng ";
cout << "\n 3. Nasi Goreng ";
return;
}
13
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
float totPrice;
void thankyou()
{
cout << "\n Thank you, please come again";
return;
14
Example of Coding 4 : Send parameters to the function and return the result of
calculation from the function to the main program through its parameter.
#include <iostream>
int main()
{
//summation(num1,num2);//caller
summation2(num1,num2,result);
cout << "\n The summation of " << num1 << " and " << num2 << " is " << result;
return 0;
}
//function definition
void summation (int num1, int num2)
{
int result;
result = num1 + num2;
cout << "\n The summation of " << num1 << " and " << num2 << " is " << result;
return;
}
15
Example of Coding 5 : Send parameters to the function and return the result of
calculation from the function to the main program.
#include <iostream>
//declaration
void summation1(int,int);
void summation2(int,int,int&);
int summation3(int,int);
int main()
{
//caller
//summation1(num1,num2);
//summation2(num1,num2, sum);
sum = summation3(num1,num2);
cout << "\n The summation of " << num1 << " and " << num2 << " is " << sum;
return 0;
//function definition
void summation1(int a, int b) //the first type of function
16
{
//local variable
sum = a + b;
cout << "\n The summation of " << a << " and " << b << " is " << sum;
return;
}
17
Example of real question 1
You are appointed to develop simple application to calculate salary for each employee.
Given the rate of wages per day is RM 65. Your program is supposed to enter number of
working days in a month. Write a complete codes to perform the following tasks:
a) Write a function named calcSalary(...) to calculate the gross salary. This function will
receive number of working days from the main program. Return the gross salary to the
main program. (Type 3)
b) Write a function named calcEPF(...) which receives the gross salary as calculated from
(a) to determine the total EPF that should be paid. Given the EPF rate is 9.5%. Return
the calculate EPF through it's parameter back to the main program. (Type 2)
c) Write a function named calcNetSalary(...) which receives the gross salary and total EPF
as calculated from question (a) and (b) respectively to calculate the net salary. The net
salary is the gross salary deducts total epf. Display the net salary in the function.(Type 1)
d) Show in your main program, how you are going to call the three functions in the main
program. Show the complete steps in the main program.
Answer :
#include <iostream>
using namespace std;
//main program
int main()
{
int days;
float gross, epf;
18
gross=calcSalary(days);
cout << "\n The gross salary is RM " << gross ;
calcEPF(gross,epf);
cout << "\n The EPF is RM " << epf ;
calcNetSalary(gross,epf);
return 0;
}
//function definition
float calcSalary(int days) //type 3
{
float wagesRate = 65.00;
float grossSalary;
grossSalary = days * wagesRate;
return grossSalary;
}
cout << "\n The net salary received by the employee is RM " << netSalary;
return;
19
Real Question of Pass Years :
20
Answer :
#include <iostream>
int main()
{
float housePrice, deposit, legalFee, totalFee;
return 0;
}
//function definition
void calDeposit1(float housePrice, float& deposit) //type 2
{
float depositRate = 0.1; //cannot assign to 10%
deposit = housePrice * depositRate;
return;
}
21
float calDeposit2(float housePrice) //type 3
{
float depositRate = 0.1, deposit; //cannot assign to 10%
deposit = housePrice * depositRate;
return deposit;
}
return legalFee;
22
Question for you, to be prepared for next class on 13 Jan 2025
Drop DropperPerfume has 10 distributors. Each distributor is given 15% commission based on his total sales.
The price for each brand of perfume is given in the following table.
a) Function calTotalSales() that receives the number of bottles for each product and returns the total
sales.
b) Function calCommission() that receives the total sales and returns the total commission received.
c) Write a complete C++ program to calculate the total sales and the total commission earned by each
distributor.
i. Display the total commission given out by the Drop DropperPerfume to all 10 distributors.
ii. Find the name of the distributor with the highest total commission.
The sample of input and output for the program is based on the following figure:
Distributer Number 2:
Please enter name 2: Yusof
Please enter number of bottles sold for AA brand: 7
Please enter number of bottles sold for BB brand: 10
Please enter number of bottles sold for CC brand: 10
Total Sales: RM1795.00
Total Commission: RM269.25
23