data structures lab 1 ta: nouf al-harbi [email protected]

56
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi [email protected]

Upload: jane-allen

Post on 03-Jan-2016

235 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

DATA STRUCTURESLAB 1

TA: Nouf Al-harbi [email protected]

Page 2: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

2

Introductiom to C++

What’s Data Structures ..?

a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently

Different kinds of data structures are suited to different kinds of applications

Page 3: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

3

Introductiom to C++

Understand the purposes and methods of the most commonly occurring data structures Analyze the data structure needs of particular problems Write C++ applications using data structures discussed in the course

Data Structures LAB Objectives

Page 4: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

4

Introductiom to C++

LAB Marks

Page 5: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

5

Introductiom to C++

noufcstu.weebly.com

Lab Materials..

Page 6: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

DATA STRUCTURES LAB 1

Introduction to C++

Page 7: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

7

objectives

Quick Review of some topics in C++ : Arrays Structures Classes

Page 8: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

8

Introductiom to C++

Data types in C++Data types

simple

int

char

double

composite

array

struct

class

Note : Not all the data types are included just some examples

Page 9: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

9

Arrays

Suppose we want to compute the average of 20 marks. Do we need to declare 20 variables

mark1, mark2, …, mark20? Do we need to write 20 couts and 20 cins? How about sorting a large number of ints?

This is where ARRAYS come in!

Page 10: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

10

Introduction to Arrays

An array is a fundamental data structure used to store objects of a particular type

in contiguous memory locations Arrays allow us to randomly access this

contiguous memory via indexing

Page 11: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

11

Array :Declaration

typeName arrayName[sizeOfTheArray]

typeName the type of the elements of the array Can be any type (primitive or user-

defined) arrayName the name of the array.

Any valid C++ identifier sizeOfTheArray the size or the capacity

of the array

Page 12: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

12

Array : Declaration

An array consisting of 5 variables of type int is declared:

It creates 5 int type variables which are accessed as score[0], score[1] , score[2], score[3], score[4]

These are called indexed variables or subscripted variables

The number in the brackets index or subscript Array subscripts (0 size of the array-1), which is

4.

int score[5];

Page 13: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

13

Array : Initialization

You may initialize specific index variables

this can also be read from cin You can use for statement to initialized

the values of an array

score[4] = 10;

for (int i=0;i<5;i++)

score[i]=i*i+1;

Page 14: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

14

Arrays : Initialization

The size of an array can be omitted

The following definition is also possible

Initialization may cover only the first few elements

the first two elements are initialized to 1 and 2

The others to the default value

int score[]={2, 1, 5, 4, 6};

int score[5]={2, 1, 5, 4, 6};

int marks[10]={1,2};

Page 15: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

15

Referencing and using arrays

Array references may be used anywhere an ordinary variable is used

The for statement is a good way to go through the elements of an array

cin >> score[4] >> score[2];cout << score[2] << “, “ << score[4];

score[0] = 32;score[3] = score[0]++;

1234

Page 16: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

16

Array Example

Write a program that defines an array

called StdMarksAvg have 5 elements

Allow the user to enter the 5 Marks

Calculate the Average of marks ..

Then print it out

#include<iostream.h>int main(){int StdMarksAvg[5],i=0;double sum=0,avg=0;cout<<"Enter the Students Marks"<<endl;for (i=0;i<5;i++){cout<<"the mark of sudent "<<i+1<<": ";

cin>>StdMarksAvg[i];

sum+=StdMarksAvg[i];}avg=sum/5;cout<<"The Average of Marks="<<avg<<endl;

return 0;}

123456789

101112131415161718

Page 17: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

17

Multidimensional Arrays

Multidimensional arrays can be described as “arrays of arrays”

For examplea bidimensional array can be imagined

as a bidimensional table made of elements

all of elements of a same uniform data type

Rows

Columns

Page 18: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

18

Matrix Example

int maxRows = 2;int maxCols = 3 ;int matrix [ 2] [ 3 ];int row , col ;for ( row = 0 ; row < maxRows ; row ++ ){

for ( col = 0 ; col < maxCols ; col ++ ){

cout << “Please enter value of ”<< row << “, “ << col;

cin >> matrix [ row ] [ col ] ;}

}

123456789

1011121314

Page 19: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

19

Matrix Example

5 2 9

7 0 4

5 2 9After first outer loop

After second outer loop

Input

Input

[0]

[1]

[0]

Page 20: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

20

Structure

A Structure is a container, it can hold many things. These things can be of any type.

Structures are used to organize related data (variables) in to a nice package.

Page 21: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

21

Example - Student Record

Student Record: Name a string HW Grades an array of 3 doubles Test Grades an array of 2 doubles Final Average a double

Page 22: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

22

Structure Members

Each thing in a structure is called member.

Each member has a name, a type and a value.

Names follow the rules for variable names.

Types can be any defined type.

Page 23: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

23

Example Structure Definition

struct StudentRecord {

string name; // student namedouble hw[3]; // homework gradesdouble test[2]; // test gradesdouble ave; // final average

};

1234567

Page 24: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

24

Using a struct

By defining a structure you create a new data type.

Once a struct is defined, you can create variables of the new type.

StudentRecord stu;

Page 25: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

25

Accessing Members

You can treat the members of a struct just like variables.

You need to use the member access operator '.' :

cout << stu.name << endl;stu.hw[2] = 82.3;

stu.ave = total/100;

123

Page 26: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

26

Structure Assignment

You can use structures just like variables:

Copies the entire structure

StudentRecord s1,s2;s1.name = “Lama";

…s2 = s1;

1234

Page 27: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

27

Classes : Introduction

Structures provide a way to group data elements

Functions organize program actions into named entities

we’ll put these ideas together to create Classes

Page 28: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

28

Classes : What Are Classes?

The functions in the class manipulate the member variables.

A class enables you to encapsulate these variables and functions into one collection (object).

variable

s

• mem

ber

variable

s or

• data

mem

bers

function

s

• member functions

or• methods of

the class

class

Introductiom to C++

Page 29: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

29

Declaring a Class

class ClassName{

memberList};

1234

Page 30: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

30

Classes: Data and Method declaration

Data member declarations are normal variable declarations int x; is a data member declaration you cannot initialize data members where

you declare them They must be initialized either in a method or

outside the class. For example, int x = 50; as a data member

declaration causes an error.

Page 31: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

31

Classes: Data and Method declaration

Method declarations are function declarations placed inside a class a function declaration can also include the

implementation (definition), or you can implement it separately

Page 32: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

32

Classes: Data and Method declaration

Introductiom to C++

class Part{int modelnumber;double cost;void SetPart(int mn, double c);void ShowPart();};

12345678

Modelnumber : int

Cost : double SetPart(int

mn ,double c)

void ShowPart()

part

Data

m

em

bers

meth

od

s

Page 33: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

33

Classes : defining object

An object is an individual instance of a class

You define an object of your new type just as you define an integer variable:

This code defines wheel, which is an object whose class (or type) is Part.

Introductiom to C++

Part wheel;

Page 34: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

34

Classes : Calling Data Member and Member Functions

Once you define an actual Part object -for example, wheel You use the dot operator (.) to access the

members of that object. to assign 50 to wheel’s modelnumber

member variable

to call the ShowPart() function

wheel.modelnumber = 50;

wheel.ShowPart();

Page 35: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

35

Classes: Assign Values to Objects, Not to Classes

In C++ you don't assign values to types you assign values to variables

This is a shorthand way of saying, "Assign 50 to the variable x, which is of type int".

int x; //define x to be an int

x = 50; //set x's value to 50

int = 50; WRONG

Page 36: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

36

Classes: Assign Values to Objects, Not to Classes

In the same way, you wouldn't write

you must define a Part object and assign 50 to the modelnumber of that object.

Part.modelnumber = 50; //wrong ???

Part wheel; //just like int x;wheel.modelnumber = 50; //just

like x = 50;

Page 37: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

37

Classes: Using Access Specifiers

C++ allows you to control where the data members of your class can be accessed.

A class has 2 kinds of access specifiers: public and private. Public data members and methods can be

accessed outside the class directly. The public stuff is the interface. Private members and methods are for internal

use only. If no access specifier is provided in the class,

all members default to private.

Page 38: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

38

Classes: Using Access Specifiers example

Introductiom to C++

class ClassName{int x;public:int y;int z;private:int a;};

123456789

Page 39: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

39

Classes: Using Access Specifiers

•Modelnumber : int, private•Cost : double, private•SetPart(int mn, •double c), public•void ShowPart(), public

part

Data

m

em

bers

meth

od

s

•Modelnumber =6245•Cost=220

part1

•Modelnumber =6246•Cost=185

Part 2

•Modelnumber =6247•Cost=150

Part 3ob

jec

ts

Page 40: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

40

private & public Example

123456789

1011121314151617

Page 41: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

41

private & public Example

123456789

1011

Page 42: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

42

Special Member Functions

Constructors: called when a new object is created (instantiated) can be many constructors, each can take different

arguments It is normally used to set initial values for the data

members It has the same name as the class cannot have a return value (not even void)

Destructor: called when an object is eliminated only one, has no arguments It is always named the same name as the class, but with a

tilde (~) at the beginning

Page 43: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

43

Constructor Example 1123456789

1011121314151617

Page 44: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

44

Constructor Example 1123456789

10111213141516

Page 45: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

45

Introductiom to C++

OUTPUT..

Page 46: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

46

Constructor Example 2123456789

101112131415161718

Page 47: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

47

Constructor Example 2123456789

10111213141516

Page 48: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

48

Introductiom to C++

OUTPUT..

Page 49: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

49

Constructor Example 3123456789

10111213141516171819202122

Page 50: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

50

Constructor Example 3123456789

10111213141516

Page 51: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

51

Introductiom to C++

OUTPUT..

Page 52: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

52

Separating Classes into Files

Classes often get pretty big having all your code in one file can quickly become

unmanageable. if you want to reuse your classes in other programs, you

have to copy and paste them into the new program. there is a convention for separating classes into files.

the class declaration is placed in one file (header file) ClassName.h

the implementation of all the methods is put in another fileClassName.cpp

include the header file in your program with an #include directive

Page 53: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

53

example

123456789

101112

Page 54: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

54

example123456789

101112131415161718192021

Page 55: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

Introductiom to C++

55

123456789

101112

example

Page 56: DATA STRUCTURES LAB 1 TA: Nouf Al-harbi nouf200@hotmail.com

56

Introductiom to C++

OUTPUT..