Sunteți pe pagina 1din 7

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() {
3200, 2700,

int salary[NUM] = {2000, 3400, 1900, 2500, 3300, 1238,


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.

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:

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

#include <iostream.h>
void main()
{
int OT[10][3], total, average;
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 =total+OT[i][j];
}
}
average = 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 "<<average<<endl; }

Step 2: Compile and run the program.


Step 3: Write the output.

S-ar putea să vă placă și