0% found this document useful (0 votes)
28 views23 pages

CSC425 Chapter5 VoidWithout WithParameter

Uploaded by

Muhammad Al-azim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views23 pages

CSC425 Chapter5 VoidWithout WithParameter

Uploaded by

Muhammad Al-azim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CSC425 [CHAPTER 5 - SAMPLE OF CODING 1]

#include <iostream> //preprocessor @ library

using namespace std;

void welcome(void); //declaration of the function @ function prototype


void thankyou(void);
void summation(int,int);
void multiplication(int,int);

int main()
{
int num1, num2, option; //local variable

welcome(); //caller

char cont = 'y';

while (cont == 'y'){

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


cin>> num1;

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


cin >> num2;

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";

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


cin >> cont;

}//close while

thankyou();

return 0; //exit from the main program

}//close main program

//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;

result = num1 * num2;

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>

using namespace std;

void welcome(void);
void menu (void);
void calculate(float, int);
void thankyou (void);

int main()
{

int foods, quantity;


float price;
char cont = 'y';

while (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)
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;
}

void calculate(float price, int quantity)


{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
float totPrice;

totPrice = price * quantity;


cout << "\n The total price is RM " << setprecision(2) << totPrice;
return;
}

5
void thankyou()
{
cout << "\n Thank you, please come again";
return;

6
Preparation of IPO and Algorithm Documentations

IPO
Input : foodOption, quantity, continue

Process : while continue is yes then


Call welcome() function
Call menu() function
if foodOption = 1 then
price = 6.50
else foodOption= 2 then
price = 6.70
else foodOption= 3 then
price = 7.00
else
price = 0
display “wrong option”

call calculate(price, quantity) function

call thankyou() function

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

6.10 call function


6.10
calculate(price, quantity)

6.11 enter cont 6.11


6.12 end while

7. call thankyou() function 7

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


Function void Menu()

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


Function void thankyou()

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>

using namespace std;

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';

while (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 Quantity of foods ordered : ";


cin >> quantity;
//calculate(price, quantity);//caller
calculate2(price,quantity,totPrice);
cout << "\n The total price is RM " << setprecision(2) << totPrice;

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;
}

void calculate(float price, int quantity)

13
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
float totPrice;

totPrice = price * quantity;


cout << "\n The total price is RM " << setprecision(2) << totPrice;
return;
}

void calculate2(float price, int quantity, float& totPrice)


{

totPrice = price * quantity;


return;
}

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>

using namespace std;


void summation(int,int); //function declaration @ function prototype
void summation2(int,int,int&);

int main()
{

int num1, num2, result;

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


cin >> num1;
cout << "\n Enter second number : ";
cin >> num2;

//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;
}

void summation2(int num1, int num2, int& result){

result = num1 + num2;


}

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>

using namespace std;

//declaration
void summation1(int,int);
void summation2(int,int,int&);
int summation3(int,int);

int sum; //global variable

int main()
{

int num1, num2;//local variable

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


cin >> num1;

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


cin >> num2;

//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;
}

void summation2(int a, int b, int& c)//the second type of function


{ //& pass by reference/result
c = a + b; //without & pass by value
return ;
}

int summation3(int a, int b)//the third type of function


{
int c;
c = a + b;
return c;

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;

//function declaration @ function prototype


float calcSalary(int);
void calcEPF(float,float&);
void calcNetSalary(float,float);

//main program
int main()
{
int days;
float gross, epf;

cout << "\n Enter number of working days : ";


cin >> days;//20

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;
}

void calcEPF(float gross, float& epf)//type 2


{
float epfRate = 0.095; //cannot assign 9.5%

epf = gross * epfRate;


return;
}

void calcNetSalary(float gross, float epf)


{
float netSalary;

netSalary = gross - epf;

cout << "\n The net salary received by the employee is RM " << netSalary;

return;

19
Real Question of Pass Years :

20
Answer :

#include <iostream>

//function declaration or prototype


void calDeposit1(float,float&);
float calDeposit2(float);//recommended
float calLegalFee(float);
float calTotal(float,float);

using namespace std;

int main()
{
float housePrice, deposit, legalFee, totalFee;

cout << "\n Enter house price RM ";


cin >> housePrice;
//Caller
calDeposit1(housePrice,deposit);
deposit = calDeposit2(housePrice);
legalFee = calLegalFee(housePrice);
totalFee = calTotal(deposit,legalFee);

cout << "\n The house Price RM " << housePrice;


cout << "\n Deposit - 10% , RM " << deposit;
cout << "\n Legal fee - 0.5 to 1%, RM " << legalFee;
cout << "\n Total fee RM " << 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;
}

float calLegalFee(float housePrice)


{
float rate, legalFee;

if (housePrice > 200000)


rate = 0.005;
else
rate = 0.01;

legalFee = rate * housePrice;

return legalFee;

float calTotal(float deposit, float legalFee)


{
float totFee = deposit + legalFee;
return totFee;
}

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.

Brand Price (per bottle)


AA 35.00
BB 56.00
CC 99.00

The following functions must be used in your solutions:

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:

Enter Number of Distributor: 2


Distributer Number 1:
Please enter name 1: Alisa
Pleas enter number of bottles sold for AA brand: 10
Pleas enter number of bottles sold for BB brand: 5
Pleas enter number of bottles sold for CC brand: 5
Total Sales: RM1125.00
Total Commission: RM168.75

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

Total Commission: RM438.00


Highest Commission: RM269.25
Distributor name: Yusof
The total commission is RM269.25

23

You might also like