Professional Documents
Culture Documents
Objective(s):
Upon completion of this lab session, learners will be able to:
• Define structures
• Define & Implement an Array of Structures
• Pass Pointers to Structures
• Pass Structure to Function
Lab Tasks:
Task 1
Write a program to create structure named employee. Take information of employee from
user as input (EmpID, EmpName, EmpAge, EmpSalary) Display the output.
Code:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct Employee
{
int EmpID;
string Empname;
string Empage;
int Empsalary;
}Emp;
void main()
{
Employee E;
system("pause");
Display/Output:
Task 2
Perform Task 1 using pointer structure.
Code:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct Employee
{
int EmpID;
string Empname;
string Empage;
int Empsalary;
}Emp;
int main()
{
Employee E;
Employee *ptr;
Page 2 of 12
Enrollment Number: 03-134202-052
ptr=&E;
return 0;
Display/Output:
Task 3
Perform Task 1 and display the output using function. Pass the structure object in function
first by value and then by reference.
Code:
#include<iostream>
using namespace std;
struct employee
{
int EmpID;
char Empname[20];
Page 3 of 12
Enrollment Number: 03-134202-052
int Empage;
int Empsalary;
};
void pass_reference(employee &value)
{
int main()
{employee data;
cout << "Give Us Information about Employee :) " << endl;
pass_reference(data);
showdata(data);
return 0;
Display/output:
Page 4 of 12
Enrollment Number: 03-134202-052
Task 4
Enter the marks of 5 students in Computer Programming, ICT and Object-Oriented
Programming (each out of 100) using a structure named Marks having elements roll no.,
name, cp_marks, ict_marks and oop_marks and then display the percentage of each student.
Code:
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct Student
{
int Enum;
int CPmarks;
int ICTmarks;
int OOPmarks;
}s1,s2,s3,s4,s5;
void student1()
{
Student s1;
Page 5 of 12
Enrollment Number: 03-134202-052
void student2()
{
cout << "Enter S2 Enum : " << endl;
cin >> s2.Enum;
cout << "Enter S2 CP Marks : " << endl;
cin >> s2.CPmarks;
cout << "Enter S2 ICT Marks : " << endl;
cin >> s2.ICTmarks;
cout << "Enter S2 OOP Marks : " << endl;
cin >> s2.OOPmarks;
void student3 ()
{
Page 6 of 12
Enrollment Number: 03-134202-052
void student4()
{
cout << "Enter S4 Enum : " << endl;
cin >> s4.Enum;
cout << "Enter S4 CP Marks : " << endl;
cin >> s4.CPmarks;
cout << "Enter S4 ICT Marks : " << endl;
cin >> s4.ICTmarks;
cout << "Enter S4 OOP Marks : " << endl;
cin >> s4.OOPmarks;
void student5()
{
cout << "Enter S5 Enum : " << endl;
cin >> s5.Enum;
cout << "Enter S5 CP Marks : " << endl;
cin >> s5.CPmarks;
cout << "Enter S5 ICT Marks : " << endl;
cin >> s5.ICTmarks;
cout << "Enter S5 OOP Marks : " << endl;
cin >> s5.OOPmarks;
int main()
{
cout <<"***********************************"<<endl;
Page 7 of 12
Enrollment Number: 03-134202-052
cout <<"***********************************"<<endl;
cout <<"***********************************"<<endl;
switch (num) {
case 1:
student1();
break;
case 2:
student2();
break;
case 3:
student3();
break;
case 4:
student4();
break;
case 5:
student5();
break;
default:
cout << "Student Doesn't Exist" << endl;
}
return 0;
}
Display/Output:
Page 8 of 12
Enrollment Number: 03-134202-052
Task 5
Write a structure to store the name, account number and balance of 50 customers and store
their information.
1 - Write a function to print the names of all the customers having balance less than $200.
2 - Write a function to add $100 in the balance of all the customers having more than $1000
in their balance and then print the incremented value of their balance.
Code:
#include<iostream>
#include<string>
using namespace std;
struct data
{
string username;
int Acc_no;
float Balance;
}U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18,
U19, U20;
void showData(data U)
{
if (U.Balance < 200)
{
cout << "Account having balance less than 200 ";
cout <<"is : "<< U.username << endl;
cout<<endl;
}
if (U.Balance>1000)
{
int addBalance = U.Balance + 100;
cout <<"Account having Balance more then 1000 : "<< addBalance << "With Username '"<<
U.username << "'" << endl;
cout<<endl;
}
int main(){
Page 9 of 12
Enrollment Number: 03-134202-052
Page 10 of 12
Enrollment Number: 03-134202-052
Page 11 of 12
Enrollment Number: 03-134202-052
showData(U14);
showData(U15);
showData(U16);
showData(U17);
showData(U18);
showData(U19);
showData(U20);
return 0;
}
Display/output:
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 12 of 12