DFC20113 PROGRAMMING FUNDAMENTALS
LAB ACTIVITY 4D:
ARRAY, POINTER and STRUCTURE
Activity 4D (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;
int 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.
DFC20113 PROGRAMMING FUNDAMENTALS
Activity 4D (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}};
int 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 4D (iii)
Suria wish to input all data for 10 staff by using a structure. By referring Activity 4D(ii), help Suria to
edit the program code and lastly display back all the data members of the structure.
Duration: 60 minutes
Step 1: Write the complete program.
Step 2: Compile the program.
Step 3: Write the output.