Professional Documents
Culture Documents
Introduction to Programming
Introduction to Programming
Structures
Collection of multiple data types that can referenced with single name It may contain similar or different data types
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
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Roll No
Marks
Average
Grade
Introduction to Programming
Introduction to Programming
10
Introduction to Programming
11
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.
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
13
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;
}
14
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.
15
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
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.
17
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";
18
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; } }
19
Introduction to Programming
Introduction to Programming
21
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.
22
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
Introduction to Programming
24
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
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
Introduction to Programming
27
Introduction to Programming
Roll No
Marks
28
Introduction to Programming
Introduction to Programming
30
Introduction to Programming
31
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.
32
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;
33
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; }
34
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
Introduction to Programming
Introduction to Programming
b[0]
Book ID Pages Price Book ID
b[1]
Pages Price Book ID
b[2]
Pages Price
37
Introduction to Programming
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.
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
40
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; }
41