0% found this document useful (0 votes)
6 views

PF LAB 13

The document is a lab manual for Programming Fundamentals focusing on structure problems in C++. It covers the declaration, initialization, and usage of structures, including nested structures and arrays of structures, along with examples and exercises for practical understanding. The manual aims to enhance students' knowledge of structures in C++ programming.

Uploaded by

Ahmad Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PF LAB 13

The document is a lab manual for Programming Fundamentals focusing on structure problems in C++. It covers the declaration, initialization, and usage of structures, including nested structures and arrays of structures, along with examples and exercises for practical understanding. The manual aims to enhance students' knowledge of structures in C++ programming.

Uploaded by

Ahmad Shabbir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Fundamentals

Lab Manual (Lab 14)

Topic: Structure Problems (CLO 1,2,3)

Course Instructor:
Lab Instructor:

Session: Fall 2024

School of Systems and Technology


Department of Computer Science
UMT Lahore Pakistan
Structures
14.1 Objective:
Learn how to:
 Declaring a Structure.
 Initializing Structure Variables.
 Assigning one structure variable to other.
 Array as Member of structure.
 Array of Structures.  Nested Structure.
14.2 Scope:
The student should know the following:
 How to accessing members of Structure Variable.
 Initializing a structure with array as member.
 Initializing array of structures.
 Implementation of nested structure in C++.

14.3 Useful Concepts:


Basic Concepts about Structures:
A structure is a collection of multiple data types that can be referenced with single name. it may contain
similar or different data types. The data items in a structure are called structure elements, members or fields.
The structures are used to join simple variables together to form complex variables.

Why Structures are important?


As we know that, an array is a collection of consecutive memory location of same name and type. Arrays
can store data of same type only. But when we needs to store data of different types on single memory
location then structures help us by regarding this. Therefore structure play a significant role in C++.

Defining a Structure:
A structure is declared by using the keyword struct followed by the structure name. The structure members
are defined with their type inside the opening and closing braces { }. The closing braces is ended with a
semicolon.
The syntax for declaring a structure is as follows:
struct Struct_name
{
Data_type1 identifier1;
Data_type2 identifier2;
};

struct: It is the keyword that is used to declare a structure.


Structure name: It is the name of the structure. The name of a structure is also known as structure tag. It
can be any legal identifier.
Data_type1: it indicates data types of first member of the structure.

2
Identifier1: it indicates the name of first member of the structure.
Data_type2: it indicates data types of second member of the structure.
Identifier2: it indicates the name of the second member of the structure.
Defining a Structure Variable :
The structure variable can be defined after the declaration of a structure. The process of defining a structure
variable is same as defining a variable of basic types such as int and char. The definition tells the
compiler to allocate memory space for the variable.
Syntax:
The syntax of defining a structure variable is as follow:

struct_Name Identifier;

Struct_Name: is the name of the structure. Identifier:


is the name of the variable to be defined.

Accessing Members of Structure Variable:


Any member of a structure variable can be accessed by using dot operator. The name of the structure
variable is written on the left side of the dot. The dot operator is also known as member access operator.
Syntax:
The syntax of accessing member of structure variable is as follow:
struct_Var.Mem_Var;

Initializing Structure variable:


The process of assigning values to member variables of structure is called initialization of structure
variables. The assignment operator is used to initialization.
Syntax:
The syntax for initializing structure variables is as follow:
Struct_Name Identifier = {value 1 , value 2…};
Struct_Name: It is a name of structure.
Identifiers: It is name of structure variables.
Value1,Value2: These are values that are used to initialize the structure variable.

Assigning One Structure variable to Other:


A structure variable can be initialized by assigning another structure variables to it by using assignment
operator as follows:
Student abc = {1, 786, 89.5, ‘A’};
Student xyz = abc;

3
The first line declares a structure variable abc and initializes it. The second line declares another structure
variable xyz and initializes it with the values of abc.
Array as Member of Structure:
A structure may consist of different types of data . The members variable can be simple data type such as
int, char and float. The member variables can also be complex like arrays.
Example:
struct student

{
int rollno;

int marks[10];
};

The above example declares a structure student with two members. the first member is a single variable of
type int. The second member is an array of integers. It can be used to store the marks of five students.

Array of Structures:
An array is a collection of same type of data . An array can be of simple data type such as int, char or
float etc. Similarly, a array can be of users define type such as a structure .An array of a structure is type
of array in which each element contains a complete structure. It can be used to store many records.

Example:
struct book

{
int
book_id;
int pages;
float
price; };
book b[5];

The above example declares a structure book. It defines an array of structures b[5]. The array can store
the records of five books.

Nested Structure:
A structure within a structure is known as nested structure. A nested structure is created when the member
of a structure is itself a structure.
Example:

4
struct A{
int n;
float b;
}; struct
B{ char c;
A x;
};
The above example declares two structures A and B. The structure A contains simple member variables n
and b. The structure B also contains two member variables. The first is character variable c. and the second
is structure variable x.

14.4 Examples
Example 14.1: Write a program to store and display information using structure

#include <iostream>
using namespace std;
struct student
{ char name[50];
int roll;
float marks;
};

int main()
{ student s;
cout << "Enter information: " << endl;
cout << "Enter name: "; cin >>
s.name;
cout << "Enter roll number:
"; cin >> s.roll; cout <<
"Enter marks: "; cin >>
s.marks;
cout << "\nDisplaying Information:" << endl; cout
<< "Name: " << s.name << endl; cout <<
"Roll: " << s.roll << endl; cout << "Marks:
" << s.marks << endl; return 0;
}

5
The output of the program is:

Example 14.2: Write a program that declares a structure to store roll number and marks of
five subjects. It defines a structure variable, inputs the values and displays roll no, marks and
average marks.

#include <iostream>
using namespace std;
struct test
{ int
rno;
int
m[5];
};
int
main()
{ test r; int i, t=0;
float avg= 0.0;
cout<<"Enter Roll
No: "; cin>>r.rno;
for(i=0; i<5; i++)
{ cout<<"Enter marks for subject "<<i+1<<": ";
cin>>r.m[i]; t = t+r.m[i];
}

avg = t/5.0;
cout<<"Roll No: "<<r.rno<<endl;
cout<<"Total marks: "<<t<<endl;
cout<<"Average: "<<avg<<endl;
return
0;
}

6
14.5 Exercises for lab
Exercises 14.1: Write a program that declares a structure to store the distance covered by a player
along with the minutes and seconds taken to cover the distance. The program should input the records
of two players and then display the record of the winner.
Exercises 14.2: Write a program that declares a structure to store the code number, salary and grade
of an employee. The program defines two structure variables, input records of two employee and then
display the record of the employee with more salary.

14.6 Home Work

1. Write a program that uses two structures Result and Record. The Result structure store marks
and grade, Record structure stores roll number and a Result type. The program declares a
variable of type Record, input roll number, marks and grade. It finally displays these values on
the screen.

2. Write a program that demonstrates the passing of entire structure as parameters to the called
function.

3. Write a program that demonstrates the passing address of the structure variable to the called
function using pointers.

4. Write a structure that demonstrates the returning of array from a function.

You might also like