Computer programming lecture – 41 to 42

Post on 08-Jul-2015

157 views 6 download

description

Computer programming lecture – 41 to 42

Transcript of Computer programming lecture – 41 to 42

Lecture – 41 to 42Computer Programming

14 Computer Systems Engineering – Second Semester

By: Mr. Ali Asghar Manjotho, Lecturer, CSE-MUET

Contents

• Structure (LL 02)

• Defining Structure in C++ (LL 04)

• Defining Car Structure in C++ (LL 04)

• Defining Point Structure in C++ (LL 04)

• Defining Time Structure in C++ (LL 04)

• Declaring Structure Variables in C++ (LL 04)

• Initializing Structure Variables in C++ (LL 04)

• Accessing Structure Members in C++ (LL 04)

• Structure of Structure in C++ (LL 04)

• Rectangle Structure in C++ (LL 04)

• Program Examples (LL 04)LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis

Ali Asghar Manjotho, Lecturer CSE-MUET 2

Structure

• Single variable can store only one value at a time.

• Single array can store multiple values at a time but they all must be of same data type.

• What if you want to store the data which is collection of different data types.

• In this case neither variable nor array is helpful.

• Here comes the role of a structure.

Ali Asghar Manjotho, Lecturer CSE-MUET 3

Structure

• Structure is the collection of heterogeneous data items unlike an array.

• Structure can also be defined as a collection of a fixed number of components in which the components are accessed by name. The components may be of different types.

• All the data items in a structure may or may not be of same data type.

• There are some data items which are group of multiple values instead of a single value.

Ali Asghar Manjotho, Lecturer CSE-MUET 4

Structure

Q: In what data type of a variable you will store a time?

A: As the time is not a single valued item. It has three integer values i.e. Hours, Minutes and Seconds. Hence we create a structure of three integer variables to store time.

Time{

int hours;int minutes;int seconds;

}

Ali Asghar Manjotho, Lecturer CSE-MUET 5

Structure

Q: In what data type of a variable you will store a point?

A: As the point is not a single valued item. It has two integer values i.e. X, and Y. Hence we create a structure of two integer variables to store a point.

Point

{

int x;

int y;

}

Ali Asghar Manjotho, Lecturer CSE-MUET 6

Structure

Q: We want to store the data about a car, the data contains the information about the name, color, company, max speed and transition mode of the car. In what data type of a variable you will store all the information of car?

A: As the car data is not a single valued item. It has five values i.e. Name, Company, Color, Maximum Speed and Transition mode (Automatic = 0 or Manual = 1). Hence we create a structure of five variables to store car.

Car{

string name;string company;string color;float speed;bool transition;

}

Ali Asghar Manjotho, Lecturer CSE-MUET 7

Defining Structure in C++

• In C++ the structure is defined by the keyword struct followed by the name of the structure.

• Then we specify all the variables of different data types, which constitute to form a structure.

• All the variables inside the structure are called as structure members.

• Finally the definition is terminated by the semicolon.

• Consider the Car structure:

Ali Asghar Manjotho, Lecturer CSE-MUET 8

Defining Car Structure in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 9

struct Car

{

string name;

string company;

string color;

float maxspeed;

bool transition;

};

Structure Name

Structure Members

Defining Point Structure in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 10

struct Point

{

int x;

int y;

};

Defining Time Structure in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 11

struct Time

{

int hours;

int minutes;

int seconds;

};

Declaring Structure Variables in C++

• Once you have created a structure, it can be treated as new data type.

• You can create the variables of the structure in the same way as you create the variables for built-in data types like; int, float, char etc.

• A structure variable can be declared by just writing the name of the structure followed by the variable name.

Ali Asghar Manjotho, Lecturer CSE-MUET 12

Declaring Structure Variables in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 13

Car mycar ;

Point p1 ;

Time now ;

Variable Name

Structure Name

Initializing Structure Variables in C++

• While initializing the structure variable we will also provide the values of each of the member of the structure.

• All the members values are placed in curly brackets { } , each value separated by comma ( , ).

• The order of the values does matter.

Ali Asghar Manjotho, Lecturer CSE-MUET 14

Initializing Structure Variables in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 15

Car mycar = {"Corolla", "Honda", "Maroon", 350.5, 0};

Point p1 = {5,3};

Time now = {8,35,14};

Accessing Structure Members in C++

• Once structure is defined and its variable is created, we can access the individual members of the structure with the help of dot operator ( . ).

• The dot operator is also called as member access operator.

Ali Asghar Manjotho, Lecturer CSE-MUET 16

Accessing Structure Members in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 17

Point p1;

p1.x = 10;

p1.y = 14;

cout<<"X = "<< p1.x <<endl;

cout<<"Y = "<< p1.y <<endl;

p1.xMember Access Operator

Structure Variable Member

Structure of Structure in C++

• If one of the member of a structure is itself a structure then the parent structure is called as the structure of structure.

• It is also termed as nested structure.

• You can nest as many structures as required i.e. there is no any limitation on the levels of nested structures.

Ali Asghar Manjotho, Lecturer CSE-MUET 18

Rectangle Structure in C++

• Consider the example of Rectangle structure.

• A rectangle can be defined as:

• Combination of left-top and right-bottom corner points.

• Combination of left-top corner point, width and height.

Ali Asghar Manjotho, Lecturer CSE-MUET 19

Rectangle Structure in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 20

struct Point

{

int x;

int y;

};

struct Rectangle

{

Point left_top;

Point right_bottom;

};

Rectangle rect = {{0,0},{10,12}};

Rectangle Structure in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 21

struct Point

{

int x;

int y;

};

struct Rectangle

{

Point left_top;

float width;

float height;

};

Rectangle rect = {{0,0},12.5,14.8};

Program ExamplesStructures in C++

Ali Asghar Manjotho, Lecturer CSE-MUET 22

Program Example 01

Problem Statement:

Write a program in C++ that creates structure TIME. Initialize two variables to store the starting and ending time of the race. The program should calculate the elapsed time in the third TIME variable and display it.

Ali Asghar Manjotho, Lecturer CSE-MUET 23

Program Example 01

Ali Asghar Manjotho, Lecturer CSE-MUET 24

Program Example 01

Ali Asghar Manjotho, Lecturer CSE-MUET 25

Program Example 02

Problem Statement:

Write a program in C++ that creates a Rectangle structure. Initialize two rectangles and find the third rectangle that encloses both the rectangles.

Ali Asghar Manjotho, Lecturer CSE-MUET 26

x

y

(0,0)

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 27

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 28

Program Example 02

Ali Asghar Manjotho, Lecturer CSE-MUET 29

Program Example 03

Problem Statement:

Write a program in C++ that creates structure STUDENT. The STUDENT structure contains the following members: Name, Roll number, Attendance Marks, Test1 marks, Test2 marks and Test3 marks. Initialize two variables to store the record of two students. The program should display the name, roll number and total sessional marks of both the students.

Ali Asghar Manjotho, Lecturer CSE-MUET 30

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 31

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 32

Program Example 03

Ali Asghar Manjotho, Lecturer CSE-MUET 33