DFC 20113 PROGRAMMING FUNDAMENTALS
LAB ACTIVITY 4A: ARRAY
Duration: 4 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)
Activity 4A(i)
The following code illustrates how to declare and initial values into a one dimensional array,
and access its elements.
Duration : 10 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;
}
}
Step 2: Compile the program.
Step 3: Write the output.
25 | P a g e
DFC 20113 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
Step 1: Type the program given below and fill in the blanks with the correct code.
#include <iostream.h>
void main()
{
int salary[__];
for (______________)
{
cout<<"Input salary for staff ID "<<i<<" : ";
cin>>__________;
}
cout<<"Staff ID \tSalary"<<endl;
for (______________)
{
cout<<i<<"\t\t"<<salary[i]<<endl;
}
}
Step 2: Compile the program.
Step 3: Write the output.
26 | P a g e
DFC 20113 PROGRAMMING FUNDAMENTALS
Activity 4A(iii)
1. Complete the following program to input 30 OT values for staff into a two dimensional array,
and access its elements.
Duration : 80 minutes
Step 1: Type the program given below and fill in the blanks with the correct code.
#include <iostream.h>
void main()
{
int OT[___][___];
for (int i=__; i___; i++)
{
for (int j=__; j__; j++)
{
cout<<"Input OT payment "<<j+1<<" for staff ID "<<i<<" : ";
cin>>OT[__][__];
}
}
cout<<"Staff ID \tOT 1 \t\tOT 2 \t\tOT 3"<<endl;
for ( i=__; i__; i++)
{
cout<<i<<"\t";
for (int j__; j__; j++)
{
cout<<"\t"<<OT[__][__]<<" \t";
}
cout<<endl;
}
}
Step 2: Compile the program.
Step 3: Write the output.
27 | P a g e
DFC 20113 PROGRAMMING FUNDAMENTALS
2. Suria want to calculate average OT payment for 30 staffs but she did not know how to edit
the program. Help Suria to complete the program below:
Step 1: Type the program given below and fill in the blanks with the correct code.
#include <iostream.h>
void main()
{
int OT[10][3], ___________;
double avg;
for (int i______________________)
{
for (int j______________________)
{
cout<<"Input OT payment "<<j+1<<" for staff ID "<<i<<" : ";
cin>>OT_______;
total = _______________;
}
}
______ = ________ / 30;
cout<<"Staff ID \tOT 1 \t\tOT 2 \t\tOT 3"<<endl;
for (i______________________)
{
cout<<i<<"\t";
for (int j______________________)
{
cout<<"\t"<<OT[__][__]<<" \t";
}
cout<<endl;
}
cout<<"Average OT payment is : RM "<<_______<<endl;
}
Step 2: Compile and run the program.
Step 3: Write the output.
28 | P a g e
DFC 20113 PROGRAMMING FUNDAMENTALS
Activity 4A(iv)
Solve a given scenario by writing a program using array.
Duration : 2 hours
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 in Payroll System.
Procedure :
Step 1: Write a program based on the scenario given.
Step 2: Compile the program.
Step 3: Write the output.
Step 4: Save the program as ________________
29 | P a g e