You are on page 1of 6

LAB 14 – STRUCTURES

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:

Example: Array of Structures


Example: Nested Structures:
struct point{
double x, y;
};

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.

Enter values for book 1 :


Book’s Name : A Guide to C Language Programming
Author’s Name : XYZ
Edition Number : 1
Year of Publishing : 2013
Price of Book : 250

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

Class Activity: Example


#include <iostream>

using namespace std;


struct DOB
{
int day;
int year;
string month;
};
struct student
{
string name;
int regno;
float marks;
DOB db;
};
void display(student s1[])
{
cout<<"RegNo\tName\t\tMarks\tDateOfBirth\n";
for(int i=0;i<3;i++)
{
cout<<s1[i].regno<<"\t"<<s1[i].name<<"\t\t"<<s1[i].marks<<"\
t"<<s1[i].db.day<<s1[i].db.month<<s1[i].db.year<<endl;
}
}
void sorting(student s1[])
{
student stemp;
for(int i=0;i<3;i++)
{
for(int j=i+1;j<3;j++)
{
if(s1[i].regno<s1[j].regno)//12,50,33
{
stemp =s1[i];//50
s1[i]=s1[j];//12
s1[j]=stemp;//50
}
}
}
}

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;
}

You might also like