Lecture 10
Lecture 10
Lecture 11
Structure
Structure Concept
• Although arrays greatly improved our ability to
store data, there is one major drawback to their
use ... each element (each box) in an array must be
of the same data type.
• It is often desirable to group data of different types
and work with that grouped data as one entity.
• We now have the power to accomplish this
grouping with a new data type called a structure.
Structure Concept
• A structure is a collection of variable types
grouped together.
• You can refer to a structure as a single variable, and
to its parts as members of that variable by using
the dot (.) operator.
• The power of structures lies in the fact that once
defined, the structure name becomes a user-
defined data type and may be used the same way
as other built-in data types, such as int, double,
char.
University Institute of Information Technology, PMAS-AAUR 3
Lecture 10: Programming Fundamentals:2012
Example
• Three variables:
– custnum of type int,
– salary of type int,
– commission of type float
–Are structure members and the structure name is Customer. This
structure is declared as follows:
Example
• Write a program that declares a structure to store Roll No., Marks, average and grade of student. The program should
define a structure variable, inputs the values and then displays values.
main()
{
struct Student cout<<"You enter the following data "<<endl;
{ cout<<"Roll No. "<<scr.rno<<endl;
int rno;
cout<<"Marks "<<scr.marks<<endl;
int marks;
cout<<"Average "<<scr.avg<<endl;
float avg;
cout<<"Grade "<<scr.grade<<endl;
char grade;
}
};
Student scr;
cout<<"Enter Roll No. ";
cin>>scr.rno;
cout<<"Enter Marks ";
cin>>scr.marks;
cout<<"Enter Average ";
cin>>scr.avg;
cout<<"Enter Grade ";
cin>>scr.grade;
struct result
{
Char name[15];
int s1, s2, s3, s4;
};
result student = {“Ali”, 70, 40, 90, 78};
int total = student. s1 + student.s2 + student.s3 + student.s4;
struct birth
{
int day;
Int month;
float year;
};
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<<“your date of birth is”<<b.day<<“/”<<b.month<<“/”<<b.year;
University Institute of Information Technology, PMAS-AAUR 12
Lecture 10: Programming Fundamentals:2012
struct book
{
int id;
Int pages;
float price;
};
book b1, b2;
cout<<“ Enter id, pages and price of book 1“;
cin>>b1.id>>b1.pages>>b1.price;
cout<<“ Enter id, pages and price of book 2“;
cin>>b2.id>>b2.pages>>b2.price;
cout<<“ The most costly book is “;
if(b1.price>b2.price)
{
cout<<“First Book”<<b1.id<<b1.pages<<b1.price<< “ has high cost”;
}
Else
{
cout<<“Second Book”<<b2.id<<b2.pages<<b2.price<< “ has high cost”;
}
University Institute of Information Technology, PMAS-AAUR 14
Lecture 10: Programming Fundamentals:2012
struct record
{
char name [15];
int Roll_No;
int sub[5];
};
Ahmed
0 1 2 3 4
Roll No.
cin>> student.sub[i]; }
}
University Institute of Information Technology, PMAS-AAUR 19
Lecture 10: Programming Fundamentals:2012
Example
NESTED STRUCTURE
Example
struct A
{
int n;
float b;
};
struct B
{
char c;
A x;
};
University Institute of Information Technology, PMAS-AAUR 24
Lecture 10: Programming Fundamentals:2012
Example
struct result
{ cout<<"Roll No.
int marks;
char grades;
"<<rec.rno<<endl;
};
struct record
cout<<"Marks
{
int rno;
"<<rec.r.marks<<endl;
};
result r;
cout<<"Grade
record rec; "<<rec.r.grade<<endl;
cout<<"Enter Roll No. ";
cin>>rec.rno;
cout<<"Enter Marks ";
cin>>rec.marks;
cout<<"Enter grade ";
cin>>rec.grade;
Syntax
· The syntax for declaring an enumeration data type is:
Example
enum year {january, feburary, march, april,
may, june, july, august, september, october,
november, december};
year y;
y = march;
cout<<“ The value of y is “<<y;
Output:
The value of y is 2
Example Continue
Example No. 2
Example No. 3
Example Continue
• . Example No. 4
Example No. 5