Professional Documents
Culture Documents
Objectives
1. Declaring structures.
2. Traversing through structures.
Introduction:
Structure is a collection of variables that can be of the same or different data types.
The data items in a structure are called the members of the structure.
A user can define its own data types using structures.
A structure in C/C++ is like a record of other languages
Syntax Example:
struct line{
point p1, p2;
};
struct triangle{
point p1, p2, p3;
};
int main()
{
point P; //point declaration
line Le; //Line declaration
triangle T;
//point assignment
P.x = 4;
P.y = 11;
//Line assignment : Line struct has two point variables
Le.p1.x = 2;
Le.p1.y = 7;
Le.p2.x = 10;
Le.p2.y = 9;
//Triangle assignment : Line struct has Three point variables
T.p1.x = 2;
T.p1.y = 0;
T.p2.x = 6;
T.p2.y = 5;
T.p3.x = 8;
T.p3.y = 3;
}
Structures and Functions
Exercise 14.1
Write a C++ program for maintaining the library record of 100 books. Each record is
composed of 6 fields which include book’s library number, book’s name, author’s name,
edition number, year of publishing and price of the book. Choose appropriate data types
to represent each field. Your program should prompt the user to populate 3 records of this
database with some values and then print these values on screen. You must use array of
structures in your program. Your program should have the following interface.
Exercise 14.2:
Add a functionality to search by author in array of structures you made in 14.1 to and
display all matched records.
Web Resources
www.cplusplus.com
msdn.microsoft.com/en-us/library/64973255.aspx
www.cprogramming.com/tutorial/lesson7.html
int main()
{
student s[5];
s[0].name = "Ahmed";
s[0].regno = 35;
s[0].marks = 7.5;
s[0].db.day = 18;
s[0].db.year = 2000;
s[0].db.month="Jan";
s[1].name = "Zubair";
s[1].regno = 8;
s[1].marks = 9;
s[1].db.day = 12;
s[1].db.year = 2000;
s[1].db.month="Dec";
s[2].name = "Ali";
s[2].regno = 38;
s[2].marks = 6;
s[2].db.day = 12;
s[2].db.year = 1999;
s[2].db.month="Feb";
display(s);
sorting(s);
display(s);
/* s1.name = "Ahmed";
s1.regno = 35;
s1.marks = 7.5;
s2.name = "Ali";
s2.regno = 38;
s2.marks = 6;
cout<<"RegNo\tName\t\tMarks\n";
cout<<s1.regno<<"\t"<<s1.name<<"\t\t"<<s1.marks<<endl;
cout<<s2.regno<<"\t"<<s2.name<<"\t\t"<<s2.marks<<endl;*/
return 0;
}