You are on page 1of 41

Lecture 14: Structures

Introduction to Programming

COMSATS Institute of Information Technology


Introduction to Programming Spring - 2010
Lec14 Structures Prepared by Saadia Aziz
2010 saadia aziz saadia_aziz@comsats.edu.pk COMSATS Institute of Information Technology 1

Lecture 14: Structures

Introduction to Programming

Structures
Collection of multiple data types that can referenced with single name It may contain similar or different data types

Difference between array and structure


Array consists of a set of variables of same data type

Structure may consist of different data types

COMSATS Institute of Information Technology

Lecture 14: Structures

Introduction to Programming

Declaring a Structure
The syntax for declaring a structure is as follows: struct struct_name { Data_type1 Identifier1; Data_type2 Identifier2; : : };
The declaration tells the compiler about the details of the structure. The compiler does not allocate memory
COMSATS Institute of Information Technology 3

Lecture 14: Structures

Introduction to Programming

Declaring a Structure (Contd)


struct: It is the keyword that is used to declare a structure struct_name: It is the name of the structure. The name of the structure also known as structure tag Data_type1: It indicates data type of first member of the structure Identifier1: It indicates the name of first member of the structure Data_type2: It indicates data type of second member of the structure Identifier2: It indicates the name of second member of the structure The structure declaration is terminated by a semicolon. It is also called as structure specifier
COMSATS Institute of Information Technology 4

Lecture 14: Structures

Introduction to Programming

Declaring a Structure (Contd)


Example: struct Student { int RollNo, Marks; float Average; char Grade; };
COMSATS Institute of Information Technology 5

Lecture 14: Structures

Introduction to Programming

Defining Structure Variable (Contd)


Syntax: Struct_Name Identifier; Struct_Name: Name of the structure Identifier: Name of the variable to be identified

COMSATS Institute of Information Technology

Lecture 14: Structures

Introduction to Programming

Defining Structure Variable


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 data type such as int and char The definition tells the compiler to allocate memory space for the variable

COMSATS Institute of Information Technology

Lecture 14: Structures

Introduction to Programming

Defining Structure Variable (Contd)


Example: Student Ali; The above example defines a structure variable Ali. The variable consists of four members as specified in structure declaration
Ali

Roll No

Marks

Average

Grade

COMSATS Institute of Information Technology

Lecture 14: Structures

Introduction to Programming

Accessing Members of Structure Variable


struct_var.mem_var; struct_var: Name of structure variable

mem_var: Name of the member variable to be accessed


COMSATS Institute of Information Technology 9

Lecture 14: Structures

Introduction to Programming

Accessing Members of Structure Variable (Contd)


Student Ali; Ali.RollNo=10; Ali.Marks=786; Ali.Average=89.52; Ali.Grade=A;

COMSATS Institute of Information Technology

10

Lecture 14: Structures

Introduction to Programming

Accessing Members of Structure Variable (Contd)


cin>>Ali.RollNo; cin>>Ali.Marks; cin>>Ali.Average; cin>>Ali.Grade; cout<<Ali.RollNo; cout<<Ali.Marks; cout<<Ali.Average; cout<<Ali.Grade;

COMSATS Institute of Information Technology

11

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store Roll No, Marks, Average and Grade of a student. The program should define a structure variable, input the values and then displays these values.

COMSATS Institute of Information Technology

12

Lecture 14: Structures struct Student { int RollNo, Marks; float Average; char Grade; }; void main( ) { Student s; cout<<"Enter Roll No"; cin>>s.RollNo; cout<<"Enter Marks"; cin>>s.Marks; cout<<"Enter Average"; cin>>s.Average; cout<<"Enter Grade"; cin>>s.Grade;

Introduction to Programming

Solution

COMSATS Institute of Information Technology

13

Lecture 14: Structures

Introduction to Programming

Solution (Contd)
cout<<"You entered the following details\n"; cout<<"RollNo"<<s.RollNo<<endl; cout<<"Marks"<<s.Marks<<endl; cout<<"Average"<<s.Average<<endl; cout<<"Grade"<<s.Grade<<endl;
}

COMSATS Institute of Information Technology

14

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store day, month and year of birth date. It inputs three values and displays date of birth in dd/mm/yy format.

COMSATS Institute of Information Technology

15

Lecture 14: Structures

Introduction to Programming

Solution
struct birth { int day, month, year; }; void main( ) { birth b; cout<<"Enter day of birth"; cin>>b.day; cout<<"Enter month of birth"; cin>>b.month; cout<<"Enter year of birth"; cin>>b.year; cout<<"\n Your date of birth is "<<b.day<<"/"<<b.month<<"/"<<b.year<<endl; }
COMSATS Institute of Information Technology 16

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store Book ID, price and pages of a book. It define three structure identifier and two member variable and input values. It displays the record of most costly book.

COMSATS Institute of Information Technology

17

Lecture 14: Structures

Introduction to Programming

Solution
struct Book { int ID, pages; float price; }; void main( ) { Book b1,b2; cout<<"Enter ID, Pages and Price of Book 1:"<<endl; cin>>b1.ID>>b1.pages>>b1.price; cout<<"Enter ID, Pages and Price of Book 2:"<<endl; cin>>b2.ID>>b2.pages>>b2.price; cout<<"The most costly book is as follows\n";

COMSATS Institute of Information Technology

18

Lecture 14: Structures

Introduction to Programming

Solution (Contd)
if(b1.price>b2.price) { cout<<"Book ID: "<<b1.ID<<endl; cout<<"Pages: "<<b1.pages<<endl; cout<<"Price: "<<b1.price<<endl; } else { cout<<"Book ID: "<<b2.ID<<endl; cout<<"Pages: "<<b2.pages<<endl; cout<<"Price: "<<b2.price<<endl; } }

COMSATS Institute of Information Technology

19

Lecture 14: Structures

Introduction to Programming

Initializing Structure Variables


Process of assigning values to member variables of structure is called initialization of structure variable Struct_Name Identifier = {value1, value 2 ..}; Struct_Name: Name of the structure Identifier: Name of the structure variable Value1, Value2: Values that are used to initialize the structure variable `
COMSATS Institute of Information Technology 20

Lecture 14: Structures

Introduction to Programming

Initializing Structure Variables (Contd)


Student Ali = {001, 786, 89.52, A};

COMSATS Institute of Information Technology

21

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store employee ID and salary of an employee. It defines and initialize a structure variable and displays it.

COMSATS Institute of Information Technology

22

Lecture 14: Structures

Introduction to Programming

Solution (Contd)
struct Emp { int Emp_ID, Sal; }; void main( ) { Emp e ={20,18500}; cout<<"Employee ID: " <<e.Emp_ID<<endl; cout<<"Salary: " <<e.Sal<<endl; } COMSATS Institute of Information Technology 23

Lecture 14: Structures

Introduction to Programming

Assigning One Structure Variable to Other


Student Ali = { 1, 1786, 89.52, A}; Student Abdullah = Ali;

COMSATS Institute of Information Technology

24

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store marks and grades of a student. It defines two structure variables. It input the values in one variable and assign that variable to the second variable. Finally, it displays the value of both variables

COMSATS Institute of Information Technology

25

Lecture 14: Structures Introduction to Programming struct Marks { int m; char g; }; void main( ) { Marks a,b; cout<<"Enter Marks: "; cin>>a.m; cout<<"Enter Grades: "; cin>>a.g; b=a; cout<<"The first record is as follows:\n"; cout<<"Marks: " <<a.m<<endl; cout<<"Grades: " <<a.g<<endl; cout<<"The second record is as follows:\n"; cout<<"Marks: " <<b.m<<endl; cout<<"Grades: " <<b.g<<endl; COMSATS Institute of Information Technology 26 }

Solution

Lecture 14: Structures

Introduction to Programming

Array as a Member of Structure


struct Student { int RollNo; int Marks[4]; };

COMSATS Institute of Information Technology

27

Lecture 14: Structures

Introduction to Programming

Array as a Member of Structure (Contd) Ali


[0] [1] [2] [3]

Roll No

Marks

COMSATS Institute of Information Technology

28

Lecture 14: Structures

Introduction to Programming

Array as a Member of Structure (Contd)


Array stored in a structure can be accessed by using dot operator and index
The dot operator is used to refer to the array

Index is used to access the individual element of the array


COMSATS Institute of Information Technology 29

Lecture 14: Structures

Introduction to Programming

Array as a Member of Structure (Contd)


Student Ali; Ali.RollNo=10; Ali.Marks[0]=89; Ali.Marks[1]=93; Ali.Marks[2]=79; Ali.Marks[3]=95;

COMSATS Institute of Information Technology

30

Lecture 14: Structures

Introduction to Programming

Initializing a Structure with Array as Member

Student Ali = {10, {89, 93, 79, 95} };

COMSATS Institute of Information Technology

31

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store Roll No and marks of five subjects. It defines a structure variable, inputs the values and displays Roll No, Marks and Average Marks.

COMSATS Institute of Information Technology

32

Lecture 14: Structures

Introduction to Programming

Solution
struct Test { int R_No; int m[5]; }; void main( ) { Test r; int i, t=0; float avg=0.0; cout<<"Enter Roll No: "; cin>>r.R_No;

COMSATS Institute of Information Technology

33

Lecture 14: Structures

Introduction to Programming

Solution (Contd)
for(i=0; i<5; i++) { cout<<"Enter Marks "<<i+1<<" "; cin>>r.m[i]; t=t+r.m[i]; } avg=t/5.0; cout<<"Roll No: "<<r.R_No<<endl; cout<<"Total Marks: " <<t<<endl; cout<<"Average: " <<avg; }

COMSATS Institute of Information Technology

34

Lecture 14: Structures

Introduction to Programming

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 An array can be of user defined data type such as structures An array of structure is a type of array in which each element contains a complete structure. It can be used to store many records
COMSATS Institute of Information Technology 35

Lecture 14: Structures

Introduction to Programming

Array of Structures (Contd)


struct Book { int BookID, pages; float price; }; Book b[3]; b[0].BookID=1; b[0].pages=230; b[0].price=125;
COMSATS Institute of Information Technology 36

Lecture 14: Structures

Introduction to Programming

Array of Structures (Contd)


b[3]

b[0]
Book ID Pages Price Book ID

b[1]
Pages Price Book ID

b[2]
Pages Price

COMSATS Institute of Information Technology

37

Lecture 14: Structures

Introduction to Programming

Initializing Array of Structures


struct Book { int BookID, pages, float price; }; Book b[3]={{1,230,125.50},{2,480,185.7}, {3,360,145.50}};
COMSATS Institute of Information Technology 38

Lecture 14: Structures

Introduction to Programming

Example
Write a program that declares a structure to store ID, pages and price of a book. It defines an array of structures to store the records of five books. It inputs the records of five books and displays the record of most costly book.

COMSATS Institute of Information Technology

39

Lecture 14: Structures struct Book { int id, pages; float price; }; void main( ) { Book b[3]; float max; int m; for(int i=0; i<5; i++) { cout<<"Enter Book ID "<<i+1<<" :"; cin>>b[i].id; cout<<"Enter Pages "<<i+1<< " :"; cin>>b[i].pages; cout<<"Enter price "<<i+1<<" :"; cin>>b[i].price; }

Introduction to Programming

Solution

COMSATS Institute of Information Technology

40

Lecture 14: Structures

Introduction to Programming

Solution (Contd)
max=b[0].price; m=0; for(int j=0; j<5; j++) { max=b[j].price; m=j; } cout<<"\n The record of most costly Book \n"; cout<<"Book ID: " <<b[m].id<<endl; cout<<"Pages: "<<b[m].pages<<endl; cout<<"Price: " <<b[m].price<<endl; }

COMSATS Institute of Information Technology

41

You might also like