two dimensional arrays please use speaker notes for additional information!

10
Two dimensional arrays Please use speaker notes for additional information!

Upload: susan-cunningham

Post on 24-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Two dimensional arrays Please use speaker notes for additional information!

Two dimensional arrays

Please use speaker notes for additional information!

Page 2: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

Looking at the data, I can see that if I fly:

From New York to Budapest it will cost me $526.89

From Providence to Istanbul it will cost $929.00

From Boston to Prague it will cost $498.99

From New York to Frankfort it will cost me $498.99

From Providence to London it will cost me $525.00

Page 3: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

To make this easier, I am going to assign numbers to the cities:

From City To City

1 is Boston 1 is Istanbul 2 is Providence 2 is Budapest 3 is New York 3 is Frankfort 4 is London 5 is Prague

Now, flying from New York to Budapest can be thought of as flying from the FROMCITY with the number 3 to the TOCITY with the number 2. This will be 526.89

123

1 2 3 4 5

Page 4: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Flying from Providence to Istanbul can be thought of as flying from the FROMCITY with the number 2 to the TOCITY with the number 1. This will be 929.00.

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Flying from Boston to Prague can be thought of as flying from the FROMCITY with the number 1 to the TOCITY with the number 5. This will be 498.99.

Page 5: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

When I set up a two dimensional array in programming languages I have to deal with which I want to do first. Do I want to get to the right row and then to the right column or do I want to get to the column first and then the row. The most common is to pick the row first and then the column.

If I call each element in the two dimensional array fare, then I want fare(row, column):

fare(3,2) means going to the third row which is New York and then to the second column which is Budapest to get the fare 0f $526.89

Page 6: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Flying from Providence to Istanbul:

fare(row,column) = fare(2,1) = 929.00

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Flying from Boston to Prague:

fare(row, column) = fare(1,5) = 498.99

Page 7: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Input format:

fromCity toCity

I am now going to assume I either read a record or took in user input and that fromCity has a value of 3 and toCity has a value of 2.

Now I am going to use fare which is defined as a cell in the table, subscripted or indexed by the two input fields fromCity and toCity to get the fare:

fare(fromCity, toCity)

Since fromCity = 3 and toCity = 2, I am getting fare(3,2) which has we know gets 526.89.

Page 8: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Input format:

fromCity toCity

I am now going to assume I either read a record or took in user input and that fromCity has a value of 2 and toCity has a value of 1.

Now I am going to use fare which is defined as a cell in the table, subscripted or indexed by the two input fields fromCity and toCity to get the fare:

fare(fromCity, toCity)

Since fromCity = 2 and toCity = 1, I am getting fare(2,1) which has we know gets 929.00.

Page 9: Two dimensional arrays Please use speaker notes for additional information!

Istanbul Budapest Frankfort London PragueBoston 851.00 549.50 659.96 499.00 498.99Providence 929.00 612.75 628.99 525.00 550.00New York 799.99 526.89 498.99 425.00 512.00

From City

To City

123

1 2 3 4 5

Input format:

fromCity toCity

I am now going to assume I either read a record or took in user input and that fromCity has a value of 1 and toCity has a value of 5.

Now I am going to use fare which is defined as a cell in the table, subscripted or indexed by the two input fields fromCity and toCity to get the fare:

fare(fromCity, toCity)

Since fromCity = 1 and toCity = 5, I am getting fare(1,5) which has we know gets 498.99.

Page 10: Two dimensional arrays Please use speaker notes for additional information!

Get Fare

Take in fromCity and toCity

fromCity >0 andfromCity < 4

toCity >0 andtoCity < 6

YN

Invalid fromCity

N

Invalid toCity

Y

fare(fromCity,toCity)

End Get Fare