Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two-Dimensional Arrays Lesson xx

Similar presentations


Presentation on theme: "Two-Dimensional Arrays Lesson xx"— Presentation transcript:

1 Two-Dimensional Arrays Lesson xx
 In this presentation, we’ll talk about two-dimensional arrays.

2 Objective We will cover the following topics that deal with two-dimensional array: Declaration Subscripts Initialization Operations Application in a program In this module, we learned the following about two-dimensional arrays: 1. Declaration 2. Subscripts 3. Initialization 4. Operations 5. Application in a program.

3 Two Dimensional Array Declaration
int m [3] [4]; col 0 col 1 col 2 col 3 row 0 row 1 A 2-dimensional array allows you to store items in table form in the computer. Here is how you declare a 2-dimensional array: int m[3] [4]; This means that array m has 3 rows and 4 columns. The rows are labeled row 0-2 and the columns are from 0-3. row 2

4 More Examples of 2-D Arrays
float taxTable [10] [15]; double d [50][50]; char nameList [100] [20]; int t [5] [6]; Here are more examples of two-dimensional array declarations: float taxTable [10][5]; this declares an array call taxTable that has 10 rows and 15 columns. The array contains floating point numbers. double d [50] [50]; declares an array of doubles. Basically, you may declare an array with any data type.

5 Subscripts int m [3] [4]; col 0 col 1 col 2 col 3 row 0 row 1
In order to access one element of a 2-dimensional array, we use subscripts. Shown in red is how you refer to the item in the 3rd row, 2nd column, m [2][1]. The row subscript always goes first and then the column subscript. Row and column numbers start with 0. Shown in green is another example of using subscripts. row 2 m [2] [1]

6 Initialization with { }
int m[2][3] = {{3, 6, 7}, {4, 7, 5}};   3 7 6 4 5 Here is an example of declaration and initialization of a two-dimensional array. We have array m that has 2 rows and 3 columns. The 1st row is initialized with 3, 6 and 7. The 2nd row is initialized with 4, 7 and 5. The inner {} are optional in this example. However, they are useful when you want to initialize partial rows which we’ll demonstrate in the next slide.

7 Initializing Partial Rows
  int m[2][3] = {{3, 6 }, {4} };   6 4 3 6 4 3 In the top example declaration, we have array m that has 2 rows and 3 columns. The 1st row is initialized with 3, 6 and 0. The 2nd row is initialized with 4, 0 and 0. The inner {}s allow us to initialize partial rows. In the bottom example, we have the same statement except that we have removed the { }s. Notice that all the #s go into the 1st row.   int m[2][3] = { 3, 6 , 4 };

8 Two-Dimensional Array Operations
int m[4][5] = {0};   int x = 0;   int a = 1;   m[2][3] = 17;   m[3][3] = a + m[2][3];   if (m[1][2] >= x)      cin >> m[1][3];   cout << m[1][0] << endl;   Basically, any operation that can be performed on scalar variables or one-dimensional arrays can be done with two-dimensional arrays. Here are some examples, you can declare and initialize an array as in: int m[4][5] = {0}; This sets up an array called m with 4 rows and 5 columns. The entire array contains zeros.

9 Two-Dimensional Array Operations
int m[4][5] = {0};   int x = 0;   int a = 1;   m[2][3] = 17;   m[3][3] = a + m[2][3];   if (m[1][2] >= x)      cin >> m[1][3];   cout << m[1][0] << endl;   You can do assignment with the statement: m[2][3] = 17; Arithmetic operation can be performed on the different elements of the array. Shown here is the example: m[3][3] = a + m[2][3]; At the bottom of the slide, you can see that we can use input, output and conditional statements in conjunction with two-dimensional arrays .

10 Program Description Write a program for a shipping company that:
1. Reads in an origination and destination zone. 2. Reads in the weight of the package. 3. Looks up the price/lb for shipping the package. 4. Computes the cost for shipping the package. A common use of two-dimensional arrays is table look up. Let’s write a program that does this. We’ll write a program for a shipping company that does the following: 1. reads in an origination and destination zone. 2. Reads in the weight of the package. 3. Looks up the price/lb for shipping the package 4. Computes the cost for shipping the package

11 Table .50 1.00 1.2 5 2.00 .89 .47 1.25 .82 .69 .44 Zone 1 2 3 4 1 Here is the chart with the price/lb for shipping a package from one zone to another. To send a package from zone 1 to zone 2, it costs 1.00 / lb. In order to ship a package from zone 4 to zone 3, the price is 82 cents/lb.

12 Program Listing #include <iostream> using std::cin; using std::cout; using std::endl; int main() {   double chart[4][4] = {{0.5, 1.0, 1.25, 2.0},    {1.0, 0.5, 0.89, 0.47},     {1.25, 0.82, 0.5, 0.69},     {2.0, 0.44, 0.82, 0.5}};   int o, d; float w;   cout << "Enter origination zone, destination zone & weight: ";   cin >> o >> d>> w;   cout << "The cost for sending your package is "     <<( chart[o-1][d-1 ] ) * w << endl;   return 0; } Here is the entire listing of the program: the table set-up, the input and the computation.

13 Table int main() {   double chart[4][4] = {{0.5, 1.0, 1.25, 2.0},    {1.0, 0.5, 0.89, 0.47},     {1.25, 0.82, 0.5, 0.69},     {2.0, 0.44, 0.82, 0.5}}; chart .50 1.00 1.2 5 2.00 .89 .47 1.25 .82 .69 .44 1 2 3 double chart [4] [4]; sets ups a two-dimensional array of doubles. We have also initialized the array with the values from our chart. Notices that the row and column # are off by 1 digit from the zone #s.

14 Declaration and Input float w;
  int o, d; float w;   cout << "Enter origination zone, destination zone & weight: ";   cin >> o >> d>> w; In this code, we have declared 3 variables, o, d & w. o is the origination zone, d is the destination zone and w is the weight of the package. We prompt the user for the 3 items of input with the cout statement and we read in the values via keyboard using the cin statement.

15 Compute Cost cout << "The cost for sending your package is "     <<( chart[o-1][d-1 ] ) * w << endl;   return 0; } This is the important part of the program. We calculate and print the cost of shipping a package in the cout statement. chart [o-1] [d-1] contains the cost/lb for sending an item from zone o to zone d. We need to subtract 1 because the array is offset by 1 from the actual table.

16 Summary In this module, we learned the following about two-dimensional arrays: Declaration Subscripts Initialization Operations Application in a program In this module, we learned the following about two-dimensional arrays: 1. Declaration 2. Subscripts 3. Initialization 4. Operations 5. Application in a program.


Download ppt "Two-Dimensional Arrays Lesson xx"

Similar presentations


Ads by Google