Download presentation
Presentation is loading. Please wait.
Published bySusan Cunningham Modified over 9 years ago
1
Two dimensional arrays Please use speaker notes for additional information!
2
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
3
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 123123 1 2 3 4 5
4
From City To City 123123 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. From City To City 123123 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.
5
From City To City 123123 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
6
From City To City 123123 1 2 3 4 5 Flying from Providence to Istanbul: fare(row,column) = fare(2,1) = 929.00 From City To City 123123 1 2 3 4 5 Flying from Boston to Prague: fare(row, column) = fare(1,5) = 498.99
7
From City To City 123123 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.
8
From City To City 123123 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.
9
From City To City 123123 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.
10
Get Fare Take in fromCity and toCity fromCity >0 and fromCity < 4 toCity >0 and toCity < 6 YN Invalid fromCity N Invalid toCity Y fare(fromCity,toCity) End Get Fare
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.