Lab 6 DSA
Lab 6 DSA
Objectives:
The objectives of this lab are to Understand the structure, its members and how to use structures
in a program.
Introduction:
Structures form a very large building block with which to collect like data into one collective
unit.Arrays are one of the most widely used data structures in programming languages. One
downfall of using such a data type is that one must use homogeneous data types, an array can
only hold multiple items of the same type. Structures overcome this problem by allowing the
programmer to have an unlimited number of items of different data types.
The struct keyword defines a structure type followed by an identifier(name of the structure).
Then inside the curly braces, you can declare one or more members (declare variables inside
curly braces) of that structure. For example:
Here a structure person is defined which has three members: name, age and salary.
When a structure is created, no memory is allocated. The structure definition is only the blueprint
for the creating of variables. You can imagine it as a datatype.
Once you declare a structure person as above. You can define a structure variable as:
Here, a structure variable bill is defined which is of type structure person. When structure
variable is defined, then only the required memory is allocated by the compiler. Considering you
have either 32-bit or 64-bit system, the memory of float is 4 bytes, memory of int is 4 bytes and
memory ofchar is 1 byte. Hence, 58 bytes of memory is allocated for structure variable bill.
The members of structure variable is accessed using dot operator. Suppose, you want to
access ageof structure variable bill and assign it 50 to it. You can perform this task by using
following code below:
Example 1: C++ Program to assign data to members of a structure variable and display it.
Here a structure person is declared which has three members. Inside main() function, a structure
variable p1 is defined. Then, the user is asked to enter information and data entered by user is
displayed.
A structure variable can be passed to a function in similar way as normal argument. Consider this
example:
#include<iostream>
usingnamespacestd;
struct person {
char name[50];
int age;
float salary;
};
int main(){
person p;
cin.get(p.name,50);
cin>>p.age;
return0;
voiddisplayData(person p1){
cout<<"\nDisplaying Information."<<endl;
In this program, user is asked to enter the data for the members of a structure variable
inside main()function. Then that structure variable to passed to a function using code.
The return type of displayData() is void and one argument of type structure person is passed. The
function prototype also should match the function definition. Then the members of
structure p1 is displayed from this function.
Returning structure from function in C++:
#include<iostream>
usingnamespacestd;
struct person {
char name[50];
int age;
float salary;
};
person getData(person);
voiddisplayData(person);
int main(){
person p;
p =getData(p);
displayData(p);
return0;
cin.get(p1.name,50);
cin>> p1.age;
cin>> p1.salary;
return p1;
voiddisplayData(person p1){
cout<<"\nDisplaying Information."<<endl;
Then the structure variable p is passed to displayData() function, which displays the information.
Lab Tasks
Program #1: Write a program that declare a structure to store Name, Roll_no, marks and section
of a student. The program should define a structure variable, input values of two students and
then display their values.
Program #2: Write a program that declares a structure to store book author, pages and its cost. It
defines two structure variables. The program displays the book author and the pages of costly
book.
Program # 3: Write a program that declares a structure to store book author, pages and its cost.
It defines a structure array of size five. The program displays the book author and the pages of
costly book.
Program # 4: Write a program that declares a structure to store Name, Roll_no, marks and
section of a student. The program should define a structure variable and take input values from
user. It passes the variable to function that displays its contents.