Dfc20113 - Workbook Activity Questions Pbu Sesi120212022 Abcdpdf PDF To Word (Autorecovered)
Dfc20113 - Workbook Activity Questions Pbu Sesi120212022 Abcdpdf PDF To Word (Autorecovered)
Dfc20113 - Workbook Activity Questions Pbu Sesi120212022 Abcdpdf PDF To Word (Autorecovered)
Table of Contents
TOPICS PAGES
1|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 1A, 1B and 1C
SCENARIO:
You have impressed Miss Suria on your capability to solve the task handed to you in 4A and 4B
in DFC1043. Therefore, Infinity Design Solution Sdn. Bhd, with the supervision of Miss Suria
has given you the task to develop payroll system by using C++ program in Integrated
Development Environment (IDE) to increase employees salary by 13% from gross pay and 5%
from allowance received (Please refer Lab Activity 4B in DFC1043).
INSTRUCTION:
Your task is to convince Miss Suria that you are familiar with Integrated Development
Environment (IDE) by writing and compiling a program using C++ before you proceed to
develop payroll system.
2|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 1A
PROCEDURE OUTPU
T
Program 1: Hello World
Step 1: Type the programs given below
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
Step 2: Compile the program.
Step 3: Write the output.
Program 2: a:1
b:2
Step 1: Type the programs given below
sum=a+b : 3
#include<iostream.h>
void main()
{
int a, b, sum;
cin >> a;
cin >> b;
sum = a + b;
cout << sum << endl;
}
Step 2: Compile the program.
Step 3: Write the output.
3|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Program 3: 4
Step 1: Type the programs given below
#include <iostream>
using namespace std;
int main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
Program 4:
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
Step 2: Compile and run the program.
Step 3: Write the output.
4|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Program 5 :
//The following program displays string using
appropriate header file.
Activity 1B
Activity Outcome: Write and compile a program using C++.
Duration : 30 minutes
Task 2 : Please refer Activity 4B.1 until Activity 4B.4 in DFC 1043. Write and compile the
program using C++. Write the output.
Output
5|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 1C
Activity Outcome: Detect the errors in following programs. Write the correct answers, compile
and run a program using C++
Duration : 60 minutes
Task 3 : Miss Suria has provided you with extra tasks. Follow the procedure below step by step.
PROCEDUR OUTPUT
E
Program 1 :
#include “iostream”
int main
{
char name[50];
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.
Program 2 :
include <iostream>
using namespace std;
int main ()
{
cout << Hello World!
cout >> "I'm a C++ program";
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.
6|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Program 3 :
#include <iostream>
using namespace std;
main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.
Program 4 :
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main
{
int area
7|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Program 5 :
#include <iostream>
using namespace std;
main()
{
int a = 21
int b = 10;
int c ;
if( a == b )
{
cout << "Line 1 - a is equal to b" << endl
}
else
{
cout << "Line 1 - a is not equal to b" << endl
}
if ( a < b )
{
cout << "Line 2 - a is less than b" << endl
}
else
{
cout << "Line 2 - a is not less than b" << endl
}
if ( a > b )
{
cout << "Line 3 - a is greater than b" << endl
}
else
{
cout << "Line 3 - a is not greater than b" << endl
}
8|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Program 6 :
main()
{
a = 21;
b = 10;
c;
c = a + b;
cout >> "Line 1 - Value of c is :" << c << endl ; c = a - b;
cout >> "Line 2 - Value of c is :" << c << endl ; c = a * b;
cout >>"Line 3 - Value of c is :" << c << endl ; c = a / b;
cout >> "Line 4 - Value of c is :" << c << endl ; c = a % b;
cout >> "Line 5 - Value of c is :" << c << endl ; c = a++;
cout >> "Line 6 - Value of c is :" << c << endl ; c = a--;
cout >> "Line 7 - Value of c is :" << c << endl ; return 0;
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.
9|Page
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 2A, 2B, 2C, 2D and 2E By
Declare variables
Use input output statements
Apply operators and expression
SCENARIO:
Congratulations to you!!
You have impressed Miss Suria on your capability to solve the task handed to you in Activity 1.
Therefore, Infinity Design Solution Sdn. Bhd, with the supervision of Miss Suria has given you
the task to help Finance Unit in completing payroll system. The scenario is that; In January of
each year, Infinity Design Solution Sdn. Bhd pays a 5% bonus to each of its salespeople. The
bonus is based on the amount of sales made by the sales person during the previous year.
INSTRUCTION:
Before you continue to write codes in C++, Miss Suria gives you some exercises. Complete the
exercises below and follow the process in completing your codes.
10 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 2A
Activity Outcome: Declare variables
Duration : 10 minutes
a)
Type Description
bool
Typically a single octet(one byte). This is an integer type.
int
A single-precision floating point value.
double
Represents the absence of type.
wchar_t
b)
Activity 2B
Activity Outcome: Write and compile a program using C++.
Duration : 20 minutes
PROCEDURE OUTPU
T
Program 1:
#include <iostream>
using namespace std;
// Variable declaration:
int a, b;
int c;
11 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
float f;
int main ()
{
// Variable definition:
int a, b;
int c;
float f;
// actual initialization
a = 10;
b = 20;
c = a + b;
; f = 70.0/3.0;
cout << f << endl ;
return 0;
}
Activity 2C
Activity Outcome: Apply operators and expression
Duration : 10 minutes
Task 3 : Identify and match the types of operator with their example correctly.
12 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 2D
Activity Outcome: Apply operators and expression
Duration : 20 minutes
PROCEDURE OUTPU
T
Program 1:
#include <iostream>
using namespace std;
main()
{
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;
}
Step 2: Compile the program.
Step 3: Write the output.
13 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Program 2:
main()
{
int a = 5; int b = 20; int c ;
if ( a && b )
{
cout << "Line 1 - Condition is true"<< endl ;
}
if ( a || b )
{
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */ a = 0;
b = 10;
if ( a && b )
{
cout << "Line 3 - Condition is true"<< endl ;
}
else
{
cout << "Line 4 - Condition is not true"<< endl
;
}
if ( !(a && b) )
{
cout << "Line 5 - Condition is true"<< endl ;
}
return 0;
}
Step 2: Compile the program.
Step 3: Write the output.
14 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 2E.1
Activity Outcome: Identify input, process and output
Duration : 10 minutes
Task 1: You must identify the input, process and output from the given task in Case Study.
Activity 2E.2
Activity Outcome: Constructing Flowchart
Duration : 10 minutes
Flowchart
15 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 2E.3
Activity Outcome: Constructing Pseudocode
Duration : 10 minutes
Pseudocode
Activity 2E.4
Activity Outcome: Write a program code
Duration : 30 minutes
Task 1: Write a complete code of the given task based on 2E.3. Then, compile the program
using C++ and write the output.
Output
16 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 3A.1 and 3A.2
SCENARIO:
Infinity Design Solution Sdn. Bhd, an advertising company wants to automate the system of
managing Human Resources (HR) data. Cik Suria was selected to be an IT programmer for
Infinity Design Solution. En. Mohamed insist to develop a system to manage staff payroll and
also other system. But before develop the system Suria try to do some exercises to improve
herself on selection control structures. These are some activities she has done.
INSTRUCTION:
Activity 3A.1
Activity Outcome: Write a program using selection control structure in solving the given
problems.
Duration : 60 minutes
Based on the problems given, write a program using selection control structure.
17 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
1. A car park has the following charges. The 1st hours cost RM2.00. The sub sequent
hours cost RM1.00 per hour. Write a program for this problem.
#include <iostream.h>
void main()
{
double entryT, exitT, period, charge;
period=exitT-entryT;
if(period>1)
{ period=period-1;
charge=2+( period *1);}
else
{ charge=2; }
2. Program that receives the current temperature as input. If the temperature is 80 degrees
or more, output a message telling the user to go swimming, otherwise, if the temperature
is 50 degrees or more, output a message to go running, otherwise stay inside.
#include <iostream.h>
void main()
{
double temperature;
cout<<”Input Temperature :”;
cin>>temperature;
if (temperature<=80)
{cout<<”go swimming”;}
else if (temperature<=50)
{cout<<”go running”;}
else
{cout<<”stay inside”;}
}
18 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
3. Jusco Supermarket is now offering some discount on selected items for Carnival
Mega sale. Write program to help the supermarket cashiers to calculate the net price
of the selected product’s below.
Position Discount
1. Food Item 5%
2. Health Item 6%
3. Electric Item 9%
4. Cloth Item 4%
#include <iostream.h>
void main()
{
char position;
double price, discount, net_price;
cout<<”Please input price:”;
cin>>price;
cout<<”Please input position for food:\n”;
cout<<”Input F for food item\n”;
cout<<”Input H for health item\n”;
cout<<”Input E for electric item\n”;
cout<<”Input C for cloth item\n”;
cin>>position;
if(position==F)
{ discount=(0.05)*price;}
else if (position==H)
{ discount=(0.06)*price; }
else if (position==E)
{ discount=(0.09)*price; }
else if (position==C)
{ discount=(0.04)*price; }
else
{ cout<<”Wrong Input”; }
net_price=price-discount;
cout<<” Net Price : “<<net_price;
19 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
4. Write a program that prompts the user to input two number and symbol ( + , - , * , /). The
program should then output the number and message saying result of the operation.
#include <iostream.h>
void main()
{
char symbol;
int a, b;
switch (symbol)
{ case '+':
cout<<" a + b = "<< a+b;
break;
case '-':
cout<<" a - b = "<< a-b;
break;
case '*':
cout<<" a * b = "<< a*b;
break;
case '/':
cout<<" a / b = "<< a/b;
break;
default:
cout<<"wrong symbol input " ;
}
}
20 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 3A.2
Activity Outcome: Build a program using selection control structure to solve the problem.
Duration : 60 minutes
PROBLEM
Case Senario:
Now Suria is ready to develop a system that will automate the process of calculating payroll for
employees worked at Infinity Design Solution Sdn. Bhd. Each employee will be recognized by
employee ID. The system will receive gross income, allowance, overtime, income tax and loan
deduction. The system will automatically calculate the total income, total deduction and net salary
for the employee. Help Suria to create the program.
21 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 3B
SCENARIO:
Help Suria to do some exercises to improve her skills in solving problems related to looping
control structures.
Activity 3B
Activity Outcome: Write a program using repetition control structures in solving the given
problems.
Duration : 120 minutes
22 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
2. A class of ten students took a quiz. The mark is between the ranges 0 - 25 for this quiz is
available to you. Determine the average mark for this quiz. Based on this problem, you
are required to create a program.
3. Calculate the salary that employee get based on number of working. Rate of the
payment is RM8.00 per hour but the salary will be deducted with 10% KWSP. By using a
repetition control structure, write a program to calculate the total salary of 5 employees
and the average salary of the employee.
23 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 4A(i), 4A(ii), 4A(iii) and 4A(iv). By
SCENARIO:
En. Mohamed (IT Manager) ask Suria to manage staff salary so that process of calculating,
storing and accessing the data can be done easily. So Suria decide to use an array to manage
the data.
Activity 4A(i)
The following code illustrates how to declare and initial values into a one dimensional array, and
access its elements.
Duration : 30 minutes
void main()
{
int salary[NUM] = {2000, 3400, 1900, 2500, 3300, 1238, 3200, 2700,
3600, 4500};
24 | P a e
g
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 4A(ii)
The following code illustrates how to input values into a one dimensional array, and access its
elements.
Duration : 30 minutes
#include <iostream.h>
#define NUM 10
void main()
{
int salary[NUM];
cout<<"Staff ID \tSalary"<<endl;
for ( i=0; i<NUM; i++)
{
cout<<i<<"\t\t"<<salary[i]<<endl;
}
}
Activity 4A(iii)
The following code illustrates how to input values into a two dimensional array, and access its
elements.
Duration : 30 minutes
25 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
#include <iostream.h>
void main()
{
int OT[10][3];
Activity 4A(iv)
Suria want to calculate average OT payment for all staffs but she did not know how to edit the
program. Help Suria to complete the program below:
Duration : 30 minutes
Step 1: Type the program given below and fill in the blanks with the correct code.
26 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
#include <iostream.h>
void main()
{
int OT[10][3], ;
double avg;
= / 30;
cout<<"Staff ID \tOT 1 \t\tOT 2 \t\tOT 3"<<endl;
for ( i=0; i<=9; i++)
{
cout<<i<<"\t";
for (int j=0; j<=2; j++)
{
cout<<"\t"<<OT[i][j]<<" \t";
}
cout<<endl;
}
cout<<"Average OT payment is : RM "<< <<endl;
}
27 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 4B(i), 4B(ii) and 4B(iii). By
Declare pointer
Assign the address of variable to pointer Manipulate the
value of variables using pointer Explain new and delete
operator
Design, write, run, test and debug program using pointer
SCENARIO:
Suria found that by using a pointer it is easier to do the input output process and access to the
data can be done efficiently. So Suria decides to change the process of input and storing
salaries into array by using pointer. But Suria still not yet proficient in the use of pointer. Suria
should put more effort to learn topics related to pointer. Therefore Suria has taken steps to
study examples of programs that use pointers.
Activity 4B(i)
Declaring a pointer, assigning the address of variable and manipulate the value of variables
using pointer.
Duration : 90 minutes
1. Fill in the blank (memory block) if one variable named rate of type integer and assign an
initial value as follows:
28 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
a) #include <iostream>
using namespace std;
int main()
{
int x; int *p; // A normal integer
// A pointer to an integer
Output:
int main ()
{
int var = 20;// actual variable declaration. int
*ip;// pointer variable
cout << "Value of var variable: "; cout << var << endl;
Output:
29 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
3. For each of the following, write a program that performs the indicated task.
a) Declare the variable fptr to be a pointer to an object of type float.
b) Declare the floating point variables num1 and num2.
c) Assign 100.20 to num1 as initial value.
d) Assign the address of variable num1 to pointer variable fptr.
e) Print the value of object pointed to by fptr.
f) Assign the value of the object pointed to by fptr to variable num2.
g) Print the value of num2.
h) Print the address of num1.
i) Print the address stored in fptr.
Your program:
30 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 4B(ii)
Explain new and delete operator.
Duration : 30 minutes
int main()
{
int n;
cout << "Enter total number of students: ";
cin >> n;
float* ptr;
Output:
31 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 4B(iii)
The following code illustrates how to declare and initial values into a one dimensional array, and
access its elements by using a pointer.
void main()
{
int salary[NUM] = {2000, 3400, 1900, 2500, 3300, 1238, 3200, 2700,
3600, 4500};
int *ptr;
ptr = salary;
cout<<"Staff ID \tSalary"<<endl;
for (int i=0; i<NUM; i++)
{
cout<<i<<"\t\t"<<*ptr<<endl;
ptr++;
}
}
32 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 4C(i), 4C(ii) and 4C(iii). By
SCENARIO:
After attended a workshop to improve her skills in C++ programming, Suria decided to upgrade
the system so that the data will be kept neatly and the program will be easy to read and
understand. So Suria will use structures to manage the data.
Activity 4C(i)
The following code illustrates how to declare a structure and how to input and access values
into/from members of the structure.
Duration : 30 minutes
#include <iostream.h>
struct Employee
{
char name[20];
int staffID;
float salary;
float OT1, OT2, OT3;
} staff;
33 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
void main()
{
cout<<"Input name : ";
cin>>staff.name;
cout<<"Input staff ID : ";
cin>>staff.staffID;
cout<<"Input salary : ";
cin>>staff.salary;
cout<<"Input OT 1 : ";
cin>>staff.OT1;
cout<<"Input OT 2 : ";
cin>>staff.OT2;
cout<<"Input OT 3 : ";
cin>>staff.OT3;
cout<<"\nName : "<<staff.name<<endl;
cout<<"Staff ID : "<<staff.staffID<<endl;
cout<<"Salary : RM "<<staff.salary<<endl;
cout<<"Overtime 1 : "<<staff.OT1<<endl;
cout<<"Overtime 2 : "<<staff.OT2<<endl;
cout<<"Overtime 3 : "<<staff.OT3<<endl;
}
Activity 4C(ii)
The following code illustrates how to declare an array structure and how to initial and access
values into/from members of the structure.
Duration : 30 minutes
struct Employee
{
char name[20];
int staffID;
double salary;
double OT1, OT2, OT3;
} ;
34 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
void main()
{
for(int i=0; i<2; i++)
{
cout<<"\nName : "<<staff[i].name<<endl;
cout<<"Staff ID : "<<staff[i].staffID<<endl;
cout<<"Salary : RM "<<staff[i].salary<<endl;
cout<<"Overtime 1 : "<<staff[i].OT1<<endl;
cout<<"Overtime 2 : "<<staff[i].OT2<<endl;
cout<<"Overtime 3 : "<<staff[i].OT3<<endl<<endl;
}
}
Activity 4C(iii)
Suria wish to input all data for 10 staff by using a structure. By referring Activity 4C(ii), help
Suria to edit the program code and lastly display back all the data members of the structure.
Duration : 60 minutes
35 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 5A(i), 5A(ii), 5A(iii), 5A(iv) and 5A(v). By
SCENARIO:
Suria has been given the task of developing a system of salary for the company Millennium
Cyber Sdn. Bhd. Suria only has been given a month to complete the task. Based on IT
Manager of Infinity Design Solution Sdn Bhd, Suria will be helped by a practical student.
Unfortunately, the student is not familiar with the concept of function in C ++. Hence, Suria need
to implement workshop for students related to the concept of function. These are some
exercises the student will do to improve her skills in function.
Activity 5A(i)
Activity Outcome: Write and compile a program using C++ using functions in programming
Duration : 15 minutes
Procedure:
36 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
// function example
#include <iostream>
using namespace std;
Activity 5A(ii)
Activity Outcome: Write and compile a program using C++ using functions prototype
Duration : 15 minutes
Procedure:
Activity 5A(iii)
Activity Outcome: Write and compile a program using C++ using function concept
Duration : 30 minutes
Procedure:
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main ()
{ // local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return
Step result; the program.
2: Compile
}
38 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 5A(iv)
Activity Outcome: Write and compile a program using C++ using passing parameter by
reference
Duration : 30 minutes
Procedure:
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
Activity 5A(v)
Activity Outcome: Write and compile a program using C++ using passing parameter by value
Duration : 30 minutes
Procedure:
39 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
int main ()
{
cout << divide (12) << '\n';
cout << divide (20,4) << '\n';
return 0;
}
40 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 5B(i), 5B(ii) and 5B(iii). By
Activity 5B(i)
Activity Outcome: Write and compile a program in user-defined function.
Duration: 30 minutes
Procedure:
//This is a program to receive input from the user and display the output
using user-defined function
#include<iostream>
using namespace std;
//function prototype/declaration
void RepeatChar(char, int);
int main()
{
char character;
int count;
41 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 5B(ii)
The following example illustrates how to write program using user-defined function and identify
function calls using call by value.
Procedure:
#include <iostream>
using namespace std;
//Function prototype/declaration
int sum(int, int, int);
int main()
{
int p = 11, q = 22, r = 33, total;
42 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
return 0;
//function definition
int sum(int p, int q, int r)
{
int result;
result = p + q + r;
return (result);
Step 5: Change the initial value of p to 15, q to 10, and r to 50. Then compile the program and
write the output.
43 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 5B(iii)
The following example illustrates how to write program using user-defined function and identify
function calls using call by reference.
Procedure:
#include<iostream>
using namespace std;
//function prototype/declaration
void funct_ref(int &z1, int z2);
int main()
{
int x = 1;
int y = 1;
cout<<"x is "<<x<<endl;
cout<<"y is "<<y<<endl;
//function definition
void funct_ref(int &z1, int z2)
{
z1++;
z2++;
}
44 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Step 5: Change the initial value of x to 10, and y to 20. Then compile the program and write the
output.
45 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Duration: 2 Hours
Learning Outcomes
This lab activity encompasses activities 5C(i), 5C(ii), 5C(iii) and 5C(iv). By
Activity 5C(i)
Activity Outcome: Write and compile a program in user-defined function.
Duration: 30 minutes
The following example illustrates how to write program to describe the concept of recursion.
Procedure:
#include<iostream>
using namespace std;
//function prototype/declaration
int fibonacci(int);
int main()
{
for(int c = 0; c<=10; c++)
{
cout<<"fibonacci("<<c<<")="<<fibonacci(c)<<endl; //call function
46 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
//function definition
int fibonacci(int number)
{
if((0==number)||(1==number))
return number;
else
return fibonacci(number - 1) + fibonacci(number - 2);
}
Activity 5C(ii)
The following example illustrates how to write program using user-defined function.
Procedure:
47 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
#include<iostream>
using namespace std;
int main()
{
int height,width;
int height2,base;
//call function
cout<<"\nTHE AREA OF RECTANGLE IS:"<<RECTANGLEAREA(height,width);
cout<<endl<<endl;
//call function
cout<<"\nTHE AREA OF TRIANGLE IS:"<<TRIANGLEAREA(height2,base);
cout<<endl<<endl;
return 0;
}
//function definition
int RECTANGLEAREA(int height, int width)
{
int area=height*width;
return area;
}
//function definition
int TRIANGLEAREA(int height2, int base)
{
double area3=0.5*height2*base;
return area3;
}
Step 2: Compile the program.
Activity 5C(iii)
Procedure:
49 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 5C(iv)
Activity Outcome: Write and compile a program using C++ using recursive function
Duration: 30 minutes
Procedure:
#include <iostream>
using namespace std;
void numberFunction(int i) {
cout << "The number is: " << i << endl; i+
+;
if(i<10) {
numberFunction(i);
}
}
int main() {
int i = 0;
numberFunction(i);
return 0;
}
50 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS
#include <iostream>
using namespace std;
void numberFunction(int i) {
cout << "The number is: " << i << endl;
}
int main() {
return 0;
}
51 | P a g e