UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
[Link]
Lab Manual 06
CS201: Data Structure and Algorithms
Class: BSCS-2k22
Lab 6: Strcture and pointers
Instructors:
Mr. Awais Mehmood
[Link]@[Link]
&
Mr. M. Faheem Saleem
[Link]@[Link]
CS201: Data Structures and Algorithms Page 1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
[Link]
Lab Manual 6
Introduction
This lab is about the pointers and structures.
Objectives
The objectives of this session is to recall and understand the usage of pointers and structures in
C++ and apply these to data strctures.
Tools/Software Requirement
Dev C++
Goals for today’s lab:
• Understanding pointers and structures.
CS201: Data Structures and Algorithms Page 2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
[Link]
Exercise
Write a C++ program to make a structure of a student consisting of integer age, char name and
structure roll number that further divides into department, session, registration number and
degree. Set and display all the values from main function and display the record on screen to
present the access of structure within structure.
Solution
#include<iostream>
using namespace std;
struct student{
int age;
char name[15];
struct roll_no{
char dep[5];
int session;
int no;
char degree[5];
}rn;
};
int main()
{
student s1;
cout<<"eneter name: "<<endl;
cin>>[Link];
cout<<"enter age: "<<endl;
cin>>[Link];
cout<<"enter roll no: "<<endl;
cout<<"enetr dep: "<<endl;
cin>>[Link];
cout<<"enter session: "<<endl;
cin>>[Link];
cout<<"enter number: "<<endl;
cin>>[Link];
cout<<"enter degree: "<<endl;
cin>>[Link];
cout<<"name is: "<<[Link]<<endl;
cout<<"age is: "<<[Link]<<endl;
cout<<"roll no is: "<<endl;
cout<<"deartment: "<<[Link]<<endl;
cout<<"session is: "<<[Link]<<endl;
cout<<"degree is: "<<[Link]<<endl;
cout<<"number is: "<<[Link]<<endl;
}
CS201: Data Structures and Algorithms Page 3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
[Link]
Output:
Exercise 2
Modify above stated program by accessing structure using pointer type variable of structure.
Exercise 3
Write a program using structures that first inputs the data for 3 employees including their names,
emp-ids and salary. Apply bubble sort on the data entered and sort the employees on basis of
highly paid salary. Also apply linear search on this data to search the employee with his name
and display its further record on screen.
CS201: Data Structures and Algorithms Page 4