DSA - Lab6 Strcture & Pointers
DSA - Lab6 Strcture & Pointers
Lab Manual 06
CS201: Data Structure and Algorithms
Class: BSCS-2k22
Lab 6: Strcture and pointers
Instructors:
Mr. Awais Mehmood
[email protected]
&
Mr. M. Faheem Saleem
[email protected]
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:
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>>s1.name;
cout<<"enter age: "<<endl;
cin>>s1.age;
cout<<"enter roll no: "<<endl;
cout<<"enetr dep: "<<endl;
cin>>s1.rn.dep;
cout<<"enter session: "<<endl;
cin>>s1.rn.session;
cout<<"enter number: "<<endl;
cin>>s1.rn.no;
cout<<"enter degree: "<<endl;
cin>>s1.rn.degree;
cout<<"name is: "<<s1.name<<endl;
cout<<"age is: "<<s1.age<<endl;
cout<<"roll no is: "<<endl;
cout<<"deartment: "<<s1.rn.dep<<endl;
cout<<"session is: "<<s1.rn.session<<endl;
cout<<"degree is: "<<s1.rn.degree<<endl;
cout<<"number is: "<<s1.rn.no<<endl;
}
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.