You are on page 1of 23

DATA STRUCTURES

STRUCTURES
Instructor : Rafia Hassan
Date: 14-03-2017
OBJECTIVES

Defining structure

Accessing of data members of structure

Initialization of structure variable

Structure variable in assignment statement


INTRODUCTION
There are many instances in programming where we
need more than one variable in order to represent
something.
For example, to represent yourself, you might want to
store your name, your birthday, your height, your
weight, or any other number of characteristics about
yourself. You could do so like this:
char strName[20];
int nBirthYear;
int nBirthMonth;
int nBirthDay;
int nHeight; // in inches
int nWeight; // in pounds
INTRODUCTION
However, you now have 6 independent variables that are not
grouped in any way. If you wanted to pass information about
yourself to a function, youd have to pass each variable
individually.

Furthermore, if you wanted to store information about more


people, youd have to declare 6 more variables for each additional
person! As you can see, this can quickly get out of control.

Fortunately, C++ allows us to create our own user-defined


aggregate data types. An aggregate data type is a data type
that groups multiple individual variables together. One of the
simplest aggregate data type is the struct.
Structure
A structure is a collection of related
elements or data items.

A structure is a collection of simple


variables.

The data items in a structure are called


the members of the structure.
Defining the Structure

Specifying the names, types and number of data items


within a structure called defining of the structure. The
syntax for defining astruct:
structure is:
Keyword

st_name: Structure name. Also called structure tag.


struct st_name type:
{ Data type of structure

type l; l,m: Members of the structure

type m; struct: Keyword

}; st_name: Structure name. Also called structure tag.


type: Data type of structure
of the structure
EXAMPLE #1

(A Simple Structure)

Structure Variables
A structure is a collection of related elements or data items. It is
defined to declare its variables. A variable of structure type represents
the member of its structure. To declare a structure variable, the
structure is first defined, e.g:

struct part
{
int modelnum;
int partnum;
float price;
};
part part1 //define a structure variable

In the above example, modelnum, partnum and price are the members
of structure part. The variable part1 is declared as structure variable.
Accessing Structure Members

The dot operator is used to access the members of a structure. To


access a member of a specific structure, the structure variable name,
the dot operator and then the member of the structure is written.

The syntax to access a member of a structure is:


struct_variable.struct_element

Example: part1.modelnum

In the given example part1 is structure variable and model number is


structure element and dot operator is used between part1 and
modelnum.
Initialization of Structure Variables

The value into a structure variable can be assigned when it is declared. It is called
initialization of the structure variable. To initialize a structure variable, data is
assigned to the members of the structure; the data items are written in the same order
in which these have been defined in the structure.
struct part
{
int modelnum;
int partnum;
float price;
};
part part1 = {624, 300. 100000};

The values to be assigned to the structure members are surrounded by braces and
separated by commas. The first value in the list is assigned to the first member, the
second to the second member and so on.
EXAMPLE # 2
Structure variable in assignment statement

s3=s2;

The statement assigns the value of each member


of s2 to the corresponding member of s3.

Note that one structure variable can be assigned to


another only when they are of the same structure
type, otherwise complier will give an error.
TASKS
TASK # 1
Write a program to find the factorial of a number
using structure. The output of the program
should be like:
TASK # 2
Write a program that defines the structure "part", having three members:

model number (int)


partnumber (int)
cost (float)

Assign the value to the members of the structure. Define two structure
variables part1 and part2. Initialize part1, print out the values of its members,
assign part1 to part2, and print out is members. The output of the program
should be like:

Model 6244, partnumber 373, cost $217


Model 6244, partnumber 373, cost $217
TASK # 3
Write a program that that uses structure variables to input the marks of the
student in three subjects. Then calculate the total marks of the three subjects
and the average. The output of the program should be like:

Enter the marks in subject 1 = 50


Enter the marks in subject 2 = 50
Enter the marks in subject 3 = 50
Total marks = 150
Average= 50
TASK # 4
A point on the two dimensional plane can be represented by two numbers: an x
coordinate and a y coordinate. For example, (4,5) represents a point 4 units to
the right of the vertical axis, and 5 units up from the horizontal axis. The sum
of two points can be defined as a new point whose x coordinate is the sum of
the x coordinates of the two points, and whose y coordinate is the sum of y
coordinates.

Write a program that uses a structure called point to model a point. Define three
points, and have the user input values to two of them. then set the third point
equal to the sum of the other two, and display the value of the new point. The
output of the program should be like:
#include<iostream>
using namespace std;

struct point
{
int xco,yco;
};

void main()
{
point p1, p2,p3;
cout<<"\n Enter the co ordinates for p1 = ";
cin>>p1.xco>>p1.yco;
cout<<"\n Enter the co ordinates for p2 = ";
cin>>p2.xco>>p2.yco;

p3.xco = p1.xco+p2.xco;
p3.yco= p1.yco+p2.yco;

cout<<"\n Co-ordinates of p1 + p2 are = "<< p3.xco<< ","<<p3.yco<<endl;


system(pause);
}
TASK # 5
A phone number, such as (212) 767-8900, can be thought of as having three
parts: the area code(212), the exchange (767), and the number (8900). Write a
program that uses a structure to store these three parts of a phone number
separately.
Call the structure phone. Create two structure variables of type phone. Initialize
one, and have the user input a number for the other one. then display both
numbers. The output of the program should be like:
struct phone
{
int area_code, exchange, number;
};
void main(void)
{
cout<<"-------------------------------------------------------------------------------\n"<
<endl;
phone input, mine={212, 767, 8900};

do
{
cout<<"\n Enter your area code, exchange, and number: ";
cin >>input.area_code>>input.exchange>>input.number;
cout<<"\n My number is ("<<mine.area_code<<")
"<<mine.exchange<<"-"<<mine.number<<endl;
cout<<"\n Your number is ("<<input.area_code<<")
"<<input.exchange<<"-"<<input.number;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}
while(getch()=='c');

You might also like