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

DEPARTMENT OF INFORMATION TECHNOLOGY & COMMUNICATION

DFC 20013 – PROGRAMMING FUNDAMENTALS


LAB 3 – ARRAY AND STRUCTURE

LEARNING OUTCOMES :

By the end of this lab, student should be able to:


 Declare 1 dimensional array and write program using 1 dimensional array
 Declare 2 dimensional array and write program using 2 dimensional array
 Declare structures and write program using structures
QUESTIONS :

QUESTION 1 (10 marks) : Array 1 Dimension


Write a program using array that can sort the list of five number entered by user. Calculate the total
and average of that five number.
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int d,sum=0,avg=0;
int num[5];
cout<<"*************************************************\n";
cout<<"| LIST,CALCULATE AND AVERAGE NUMBER |\n";
cout<<"*************************************************\n\n";
cout<<"Please Enter Five Number: \n";

for(d=1;d<6;d++)
{
cout<<"Number[ "<<d<<" ] = ";
cin>>num[d];
}

for(d=1;d<6;d++)
{
sum+=num[d];
}

cout<<"\nSUM OF ALL NUMBER IS :"<<sum<<endl;


for(d=1;d<6;d++)
{
avg=sum/5;
}
cout<<"\nAVERAGE OF ALL NUMBER IS :"<<avg<<endl;

system("pause");
}

1|Page
Output:

2|Page
QUESTION 2 (5 marks): Array 2 Dimension
Write a programme to display values in 2 dimensional arrays as in the given table below.

0 1 2

0 9 7 5
1 6 8 10

Display of Output:

#include<iostream>
using namespace std;
void main()
{
int values[2][3]={9,7,5,6,8,10};
cout<<"NUMBER OF FOOD EATEN ACCORDING THEIR GENDER\n";
cout<<"-----------------------------------------------\n\n\n";
cout<<"\t\t\tPizza\t\tKFC\t\tMcDonalds\n";
cout<<"Man\t\t\t"<<values[0][0]<<"\t\t"<<values[0][1]<<"\t\t"<<values[0]
[2]<<endl;
cout<<"Woman\t\t\t"<<values[1][0]<<"\t\t"<<values[1][1]<<"\t\t"<<values[1]
[2]<<endl;
system("pause");
}

Output:

QUESTION 3 (5 marks): Structure

3|Page
Write the programme based on the following situations. Every situation linked with another:

a. Declare a structure named “Student”

b. Declare the following structure members:


i. Name
ii. Matrix number
iii. Age
iv. Courses

c. Declare the following structure variables:


i. Person A
ii.
iii. Person B

d. Assign following data to Person A structure members:


i. Arewardi A/P Arkom
ii. 12DIP11F1001
iii. 18
iv. DIP

e. Ask user to key in all data to Person B structure members.

f. Display all the Person A and Person B data.

#include<iostream>
4|Page
#include<string>
using namespace std;
void main()
{
struct Student
{
char name[50];
char matrixnumber[12];
int age;
char course[7];
}
PersonA,PersonB;
strcpy(PersonA.name,"Arewardi A/P Arkom");
strcpy(PersonA.matrixnumber,"12DIP11F1001");
strcpy(PersonA.course,"DIP");
PersonA.age=18;

cout<<"*STUDENT POLYTECHNIC DATA*\n\n";


cout<<"Please Key In Your Name : ";
cin>>PersonB.name;

cout<<"Please Key In Your Matrix Number : ";


cin>>PersonB.matrixnumber;

cout<<"Please Key In Your Age : ";


cin>>PersonB.age;

cout<<"Please Key In Your Course : ";


cin>>PersonB.course;

cout<<"\t\t\t\tPerson A \t\t\tPerson B\n";


cout<<"Name : \t\t"<<PersonA.name<<"\t\t"<<PersonB.name<<endl;
cout<<"Matrix Number :
\t\t"<<PersonA.matrixnumber<<"\t\t\t"<<PersonB.matrixnumber<<endl;
cout<<"Age : \t\t"<<PersonA.age<<"\t\t\t\t"<<PersonB.age<<endl;
cout<<"Course : \t\t"<<PersonA.course<<"\t\t\t\t"<<PersonB.course<<endl;

system ("pause");
}

QUESTION 4 (10 marks): Structure

5|Page
Build a programme to manage Emporium Hock Kee Seng sales data. All sales data must include
product code, product category, product name, quantity and price per unit. All the data must be insert
by the user. Lastly display all the data that inserted by the user.
This programme can manage 4 sales product. Used struct, array and looping to complete this
programme.

Example :

Product code : 12315322


Product category : Milk
Product name : Anlene Gold
Price per unit : RM 19.95
Quantity : 54

Product code : 646467434


Product category : Sweet
Product name : Lot 100
Price per unit : RM 5.99
Quantity : 121
.
.
.

#include<iostream>

6|Page
using namespace std;
struct product
{
char name[40];
long int code;
char catgory[50];
double ppu;
int qtty,roll;

} s[4];
int main()
{
cout<<"############################################\n";
cout<<"# EMPORIUM HOCK KEE SENG SALES DATA #\n";
cout<<"############################################"<<endl<<endl;

for(int p=0;p<4;p++)
{
s[p].roll=p+1;

cout<<"Enter the product code : ";


cin>>s[p].code;
cout<<"Enter the product category : ";
cin>>s[p].catgory;
cout<<"Enter the product name : ";
cin.getline(s[p].name,40);
cin.getline(s[p].name,40);
cout<<"Enter the product price per unit : RM ";
cin>>s[p].ppu;
cout<<"Enter the product quantity : ";
cin>>s[p].qtty;
cout<<endl<<endl;
}
cout<<"\n=======================================\n";
cout<<" *Display The Product Information* \n";
cout<<"==========================================\n\n";

for(int p = 0; p<4;++p)
{
cout<<"Product Code : "<<s[p].code<<endl;
cout<<"Product Category : "<<s[p].catgory<<endl;
cout<<"Product Name : "<<s[p].name<<endl;
cout<<"Product Price Per Unit : "<<s[p].ppu<<endl;
cout<<"Product Quantity : "<<s[p].qtty<<endl<<endl;
}
system("pause");
return 0;
}

7|Page

You might also like