data structures lab 1 ta: nouf al-harbi nouf200@hotmail.com

Post on 03-Jan-2016

238 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

DATA STRUCTURESLAB 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

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

4

Introductiom to C++

LAB Marks

5

Introductiom to C++

noufcstu.weebly.com

Lab Materials..

DATA STRUCTURES LAB 1

Introduction to C++

Introductiom to C++

7

objectives

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

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

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!

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

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

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

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;

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

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

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

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

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

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]

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.

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

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.

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

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;

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

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

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

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++

Introductiom to C++

29

Declaring a Class

class ClassName{

memberList};

1234

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.

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

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

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;

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();

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

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;

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.

38

Classes: Using Access Specifiers example

Introductiom to C++

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

123456789

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

Introductiom to C++

40

private & public Example

123456789

1011121314151617

Introductiom to C++

41

private & public Example

123456789

1011

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

Introductiom to C++

43

Constructor Example 1123456789

1011121314151617

Introductiom to C++

44

Constructor Example 1123456789

10111213141516

45

Introductiom to C++

OUTPUT..

Introductiom to C++

46

Constructor Example 2123456789

101112131415161718

Introductiom to C++

47

Constructor Example 2123456789

10111213141516

48

Introductiom to C++

OUTPUT..

Introductiom to C++

49

Constructor Example 3123456789

10111213141516171819202122

Introductiom to C++

50

Constructor Example 3123456789

10111213141516

51

Introductiom to C++

OUTPUT..

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

Introductiom to C++

53

example

123456789

101112

Introductiom to C++

54

example123456789

101112131415161718192021

Introductiom to C++

55

123456789

101112

example

56

Introductiom to C++

OUTPUT..

top related