Dfc20113 - Workbook Activity Questions Pbu Sesi120212022 Abcdpdf PDF To Word (Autorecovered)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 52

WORKBOOK ACTIVITY

DFC20113 PROGRAMMING FUNDAMENTALS

Table of Contents
TOPICS PAGES

Introduction to Fundamentals of Programming


ACTIVITY 1: 2

ACTIVITY 2: Basic C++ Program 10

ACTIVITY 3A: Program Control Structures (I) 17

ACTIVITY 3B: Program Control Structures (II) 22

ACTIVITY 4A: Array 24

ACTIVITY 4B: Array and Pointer 28

ACTIVITY 4C: Structure 33

ACTIVITY 5A: Function (I) 36

ACTIVITY 5B: Function (II) 41

ACTIVITY 5C: Function(III) 46

1|Page
DFC20113 PROGRAMMING FUNDAMENTALS

LAB ACTIVITY 1: INTRODUCTION TO


FUNDAMENTALS OF PROGRAMM

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 1A, 1B and 1C

By the end of this practical session, you should be able to :

 Develop C++ program using Integrated Development Environment (IDE)


 Debug simple programs to demonstrate syntax/compile time, run time and logical error

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

SCENARIO:

Welcome to C++ program using Integrated Development Environment (IDE).

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

Activity Outcome: Write and compile a program using C++.


Duration : 30 minutes

Task 1: Follow the procedure below step by step.

PROCEDURE OUTPU
T
Program 1: Hello World
Step 1: Type the programs given below

// Hello world program using C++

#include <iostream>
using namespace std;

// main() is where program execution begins.

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

// operating with variables

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

// print out the result:


cout << result;

// terminate the program:


return 0;
}
Step 2: Compile the program.
Step 3: Write the output.

Program 4:

Step 1: Type the programs given below


// input/output example

#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.

Step 1: Type the programs given below


#include <iostream> #include <string> using
namespace std; int main ()
{
string mystring = "This is a string"; cout << mystring;
return 0;
}

Step 2: Compile and run the program.


Step 3: Write the output.

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 :

Step 1: Find the errors in following programs.

#include “iostream”

using namespace std;

int main
{
char name[50];

cout >> "Please enter your name: ";


cin <<name;
cout << Your name is: " name << endl;

}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.

Program 2 :

Step 1: Find the errors in following programs.

/ my second program in C++

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 :

Step 1: Find the errors in following programs.

#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 :

Step 1: Find the errors in following programs.

#include <iostream>
using namespace std;

#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'

int main
{

int area

area = LENGTH * WIDTH;


cout >> area;
cout >> NEWLINE;
return 0;
}
Step 2: Write the correct answers
Step 3: Compile and run the program.
Step 4: Write the output.

7|Page
DFC20113 PROGRAMMING FUNDAMENTALS

Program 5 :

Step 1: Find the errors in following programs.

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

Step 2: Write the correct answers


Step 3: Compile and run the program.
Step 4: Write the output.

8|Page
DFC20113 PROGRAMMING FUNDAMENTALS

Program 6 :

Step 1: Find the errors in following programs.

#include <iostream> using namespace std;

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

LAB ACTIVITY 2: BASIC C++ PROGRAM

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 2A, 2B, 2C, 2D and 2E By

the end of this practical session, you should be able to :

 Declare variables
 Use input output statements
 Apply operators and expression

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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

Task 1: Fill in the blanks with the correct answer.

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)

Type Variable List Valid Declaration


int i,j,k
char c,ch
float f,salary
double d

Activity 2B
Activity Outcome: Write and compile a program using C++.
Duration : 20 minutes

Task 2 : Follow the procedure below step by step.

PROCEDURE OUTPU
T
Program 1:

Step 1: Type the programs given below

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

cout << c << endl

; f = 70.0/3.0;
cout << f << endl ;

return 0;
}

Step 2: Compile the program.


Step 3: Write the output.

Activity 2C
Activity Outcome: Apply operators and expression
Duration : 10 minutes

Task 3 : Identify and match the types of operator with their example correctly.

Types of operator Examples


Arithmetic (A & B) will give 12
Operators which is 0000 1100
Relational
A++ will give 11
Operators
C *= A is equivalent
Logical Operators
to C = C * A
Bitwise Operators (A && B) is false.
Assignment
(A == B) is not true.
Operators

12 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Activity 2D
Activity Outcome: Apply operators and expression
Duration : 20 minutes

Task 4 : Follow the procedure below step by step.

PROCEDURE OUTPU
T
Program 1:

Step 1: Type the programs given below

#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:

Step 1: Type the programs given below

#include <iostream> using namespace std;

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.

INPUT PROCESS OUTPUT

Activity 2E.2
Activity Outcome: Constructing Flowchart
Duration : 10 minutes

Task 1: Construct a flowchart of the given task based on 2E.1.

Flowchart

15 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Activity 2E.3
Activity Outcome: Constructing Pseudocode
Duration : 10 minutes

Task 1: Construct a pseudocode of the given task based on 2E.2.

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

LAB ACTIVITY 3A: PROGRAM CONTROL


STRUCTURES (I)

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 3A.1 and 3A.2

By the end of this practical session, you should be able to :

 Understand program control structures.


 Solve problems using selection control structures.

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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:

Create program using appropriate control structure.

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;

cout<<"Input entry time :";


cin>>entryT;
cout<<"Input exit time :";
cin>>exitT;

period=exitT-entryT;
if(period>1)
{ period=period-1;
charge=2+( period *1);}
else
{ charge=2; }

cout<< "Charge :RM"<<charge;


}

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;

cout << "Enter symbol = ";


cin >> symbol;
cout << "Enter value number 1 and number 2 "<<endl;
cin >>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

LAB ACTIVITY 3B: PROGRAM CONTROL


STRUCTURES (II)

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 3B

By the end of this practical session, you should be able to :

 Solve problems using looping control structures.

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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

1. Based on the algorithm below, write a program using :


a. For loop
b. While loop
c. Do..while loop

22 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

1. Initialize Counter = 1; average = 0; Total


= 0;
2. Input number.
3. Add Total using formula:
Total = Total + number
4. Add Counter using formula:
Counter = Counter + 1
5. Compare whether Counter is greater than 5.
If yes, go to step 6.
If no, go to step 2.
6. Calculate Average of numbers using
formula:
Average = Total / 5
7. Display Average.

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

LAB ACTIVITY 4A: ARRAY

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 4A(i), 4A(ii), 4A(iii) and 4A(iv). By

the end of this practical session, you should be able to :

 Demonstrate understanding the use of arrays Declare one and


 two dimensional
 Initialize one and two dimensional array IIIustrate the one and
 two dimensional array
 Access individual element of one and two dimensional array

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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

Step 1: Type the program given below:

#include <iostream.h> #define NUM 10

void main()
{
int salary[NUM] = {2000, 3400, 1900, 2500, 3300, 1238, 3200, 2700,
3600, 4500};

cout<<"Staff ID \tSalary"<<endl; for (int i=0; i<NUM; i++)


{
cout<<i<<"\t\t"<<salary[i]<<endl;
}
}

24 | P a e
g
DFC20113 PROGRAMMING FUNDAMENTALS

Step 2: Compile the program.


Step 3: Write the output.

Activity 4A(ii)
The following code illustrates how to input values into a one dimensional array, and access its
elements.
Duration : 30 minutes

Step 1: Type the program given below:

#include <iostream.h>
#define NUM 10

void main()
{
int salary[NUM];

for (int i=0; i<NUM; i++)


{
cout<<"Input salary for staff ID "<<i<<" : ";
cin>>salary[i];
}

cout<<"Staff ID \tSalary"<<endl;
for ( i=0; i<NUM; i++)
{
cout<<i<<"\t\t"<<salary[i]<<endl;
}
}

Step 2: Compile the program.


Step 3: Write the output.

Activity 4A(iii)
The following code illustrates how to input values into a two dimensional array, and access its
elements.
Duration : 30 minutes

Step 1: Type the program given below:

25 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

#include <iostream.h>

void main()
{
int OT[10][3];

for (int i=0; i<=9; i++)


{
for (int j=0; j<=2; j++)
{
cout<<"Input OT payment "<<j+1<<" for staff ID "<<i<<" : ";
cin>>OT[i][j];
}
}

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

Step 2: Compile the program.


Step 3: Write the output.

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;

for (int i=0; i<=9; i++)


{
for (int j=0; j<=2; j++)
{
cout<<"Input OT payment "<<j+1<<" for staff ID "<<i<<" : ";
cin>>OT[i][j];
total = ;
}
}

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

Step 2: Compile and run the program.


Step 3: Write the output.

27 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

LAB ACTIVITY 4B: ARRAY AND POINTER

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 4B(i), 4B(ii) and 4B(iii). By

the end of this practical session, you should be able to :

 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

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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:

int rate = 100;

28 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

2. Type the programs given below and trace the output.

a) #include <iostream>
using namespace std;

int main()
{
int x; int *p; // A normal integer
// A pointer to an integer

p = &x; // Read it, "assign the address of x to p"


cout<<"Insert a value:";
cin>> x; // Put a value in x, we could also use *p here
cout<< *p <<"\n"; // Note the use of the * to get the value
system("pause"); return
0;
}

Output:

b) #include <iostream> using namespace std;

int main ()
{
int var = 20;// actual variable declaration. int
*ip;// pointer variable

ip = &var; // store address of var in pointer variable

cout << "Value of var variable: "; cout << var << endl;

// print the address stored in ip pointer variable cout


<< "Address stored in ip variable: ";
cout << ip << endl;

// access the value at the address available in pointer


cout << "Value of *ip variable: ";
cout << *ip << endl; system("pause"); return 0;

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

Type the programs given below and trace the output.

#include <iostream> #include <cstring> using


namespace std;

int main()
{
int n;
cout << "Enter total number of students: ";
cin >> n;
float* ptr;

ptr = new float[n]; // memory allocation for n number of floats

cout << "Enter GPA of students." <<endl; for (int i = 0; i


< n; ++i)
{
cout << "Student" << i+1 << ": "; cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl; for (i =
0; i < n; ++i)
{
cout << "Student" << i+1 << " :" << *(ptr + i) << endl;
}

delete [] ptr;// ptr memory is released system("pause");


return 0;}

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.

Step 1: Type the program given below:


#include <iostream.h>
#define NUM 10

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

Step 2: Compile the program.


Step 3: Write the output.

32 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

LAB ACTIVITY 4C: STRUCTURE

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 4C(i), 4C(ii) and 4C(iii). By

the end of this practical session, you should be able to :

 Demonstrate understanding the use of structure.


 Declare structure variable
 Assign values to a structure variable Manipulate
 values of a structure

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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

Step 1: Type the program given below:

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

Step 2: Compile the program.


Step 3: Write the output.

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

Step 1: Type the program given below:


#include <iostream.h>

struct Employee
{
char name[20];
int staffID;
double salary;
double OT1, OT2, OT3;
} ;

Employee staff[2] = {{"Ahmad", 1234, 3500.65, 45.00, 25.00,


56.55},{"Albab", 3556, 2300.55, 45.45, 55.00, 65.20}};

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

Step 2: Compile the program.


Step 3: Write the output.

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

LAB ACTIVITY 5A: FUNCTION(I)

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 5A(i), 5A(ii), 5A(iii), 5A(iv) and 5A(v). By

the end of this practical session, you should be able to :

 Apply the functions in programming.


 Declare function prototypes
 Identify the scope of variables
 Use the parameters passing techniques

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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:

Step 1: Type the programs given below:

36 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)


{ int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

Step 2: Compile the program.


Step 3: Write the output.

Activity 5A(ii)
Activity Outcome: Write and compile a program using C++ using functions prototype
Duration : 15 minutes

Procedure:

Step 1: Type the programs given below

#include < iostream>


using namespace std;
int sum (int x, int y); //declaring function
int main()
{int a = 10;
int b = 20;
int c = sum (a, b); //calling function
cout << c;
return 0;
}
int sum (int x, int y) //defining function
{
return (x + y);
} 37 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Step 2: Compile the program.


Step 3: Write the output.

Activity 5A(iii)
Activity Outcome: Write and compile a program using C++ using function concept
Duration : 30 minutes

Procedure:

Step 1: Type the programs given below

#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

Step 3: Write the output.

Activity 5A(iv)
Activity Outcome: Write and compile a program using C++ using passing parameter by
reference
Duration : 30 minutes

Procedure:

Step 1: Type the programs given below


// passing parameters by reference
#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)


{
a*=2;
b*=2;
c*=2;
}

int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

Step 2: Compile the program.


Step 3: Write the output.

Activity 5A(v)
Activity Outcome: Write and compile a program using C++ using passing parameter by value
Duration : 30 minutes
Procedure:

Step 1: Type the programs given below

39 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

// default values in functions


#include <iostream>
using namespace std;

int divide (int a, int b=2)


{
int r;
r=a/b;
return (r);
}

int main ()
{
cout << divide (12) << '\n';
cout << divide (20,4) << '\n';
return 0;
}

Step 2: Compile the program.


Step 3: Write the output.

40 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

LAB ACTIVITY 5B: FUNCTION(II)

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 5B(i), 5B(ii) and 5B(iii). By

the end of this practical session, you should be able to :

 Apply the functions in programming.


 Declare function prototypes
 Identify the scope of variables
 Use the parameters passing techniques

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

Activity 5B(i)
Activity Outcome: Write and compile a program in user-defined function.
Duration: 30 minutes

The following example illustrates how to passing arguments to function.

Procedure:

Step 1: Type the programs given below

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

cout<<"Enter a character: ";


cin>>character;

41 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

cout<<"Enter repeat count: ";


cin>>count;

RepeatChar(character,count); //call function


return 0;
}
//function definition
void RepeatChar(char ch, int n)
{
for(int j=0; j<n; j++)
cout<<ch<<"\t";
cout<<endl;
}

Step 2: Compile the program.

Step 3: Write the output.

Step 4: Save the program as

Activity 5B(ii)

Activity Outcome: Write and compile a program in user-defined function.


Duration: 60 minutes

The following example illustrates how to write program using user-defined function and identify
function calls using call by value.

Procedure:

Step 1: Type the programs given below.

//This is a program to identify the function calls using call by value

#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

total = sum(p, q, r); //call function


cout<<"Total value is: "<<total<<endl;

total = sum(p, q=55, r); //call function


cout<<"Total value is: "<<total<<endl;

return 0;

//function definition
int sum(int p, int q, int r)
{
int result;

result = p + q + r;
return (result);

Step 2: Compile the program.


Step 3: Write the output.

Step 4: Save the program as

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.

Step 6: What is the return type of a function sum?

Step 7: List all parameter(s) involve in the function sum?

43 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Activity 5B(iii)

Activity Outcome: Write and compile a program in user-defined function.


Duration: 30 minutes

The following example illustrates how to write program using user-defined function and identify
function calls using call by reference.

Procedure:

Step 1: Type the programs given below.

//This is a program to identify the function calls using call by reference

#include<iostream>
using namespace std;

//function prototype/declaration
void funct_ref(int &z1, int z2);

int main()
{
int x = 1;
int y = 1;

funct_ref(x, y); //call function

cout<<"x is "<<x<<endl;
cout<<"y is "<<y<<endl;

//function definition
void funct_ref(int &z1, int z2)
{
z1++;
z2++;
}

Step 2: Compile the program.


Step 3: Write the output.

44 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Step 4: Save the program as

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

LAB ACTIVITY 5C: FUNCTION(III)

Duration: 2 Hours

Learning Outcomes
This lab activity encompasses activities 5C(i), 5C(ii), 5C(iii) and 5C(iv). By

the end of this practical session, you should be able to :

 Apply the functions in programming.


 Declare function prototypes
 Identify the scope of variables
 Use the parameters passing techniques
 Understand recursive functions

Hardware/Software: C++ software (Microsoft Visual Studio, Turbo C++ 5.0/6.0)

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:

Step 1: Type the programs given below.

//This is a program that shows the concept of recursion

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

Step 2: Compile the program.


Step 3: Write the output.

Step 4: Save the program as

Activity 5C(ii)

Activity Outcome: Write and compile a program in user-defined function.


Duration: 30 minutes

The following example illustrates how to write program using user-defined function.

Procedure:

Step 1: Type the programs given below.

47 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

//Count the area of a rectangle and triangle by using the function

#include<iostream>
using namespace std;

//function prototype/function declaration


int RECTANGLEAREA(int,int);
int TRIANGLEAREA(int,int);

int main()
{
int height,width;
int height2,base;

cout<<"INSERT RECTANGLE HEIGHTS:";


cin>>height;

cout<<"\nINSERT RECTANGLE WIDTH:";


cin>>width;

//call function
cout<<"\nTHE AREA OF RECTANGLE IS:"<<RECTANGLEAREA(height,width);

cout<<endl<<endl;

cout<<"\nINSERT TRIANGLE HEIGHTS:";


cin>>height2;

cout<<"\nINSERT TRIANGLE WIDTH:";


cin>>base;

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

Step 3: Write the output.


48 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Step 4: Save the program as

Activity 5C(iii)

Activity Outcome: Write and compile a program in user-defined function.


Duration : 30 minutes
The following example illustrates how to write program using user-defined function.

Procedure:

Step 1: Type the programs given below.

1- //This is a program to display Area of circle using a function


2-
3- #include<iostream>
4- using namespace std;
5- void displayArea(double);
6-
7- int main()
8- {
9- double radius = 2;
10- displayArea(radius);
11- displayArea(3);
12-
13- return 0;
14- }
15-
16- void displayArea(double radius)
17- {
18- double area = radius * radius * 3.142;
19- cout<<"area is "<<area<<endl;
20- }

Step 2: Compile the program.

Step 3: Write the output.

49 | P a g e
DFC20113 PROGRAMMING FUNDAMENTALS

Step 4: Save the program as

Step 5: Which line(s) represent function prototype/declaration?

Step 6: Which line(s) represent calling function?


Step 7: Which line(s) represent function definition?

Activity 5C(iv)
Activity Outcome: Write and compile a program using C++ using recursive function
Duration: 30 minutes

Procedure:

Step 1: Type the programs given below with recursive function

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

Step 2: Compile the program.

Step 3: Write the output.

Step 4 : Write the programs given below with function only

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() {

for(int i=0; i<10; i++) {


numberFunction(i);
}

return 0;
}

Step 5: Compile the program.

Step 6: Write the output.

Step 7: Discuss the differences between two program above

51 | P a g e

You might also like