16
Two-Dimensional Arrays Lesson xx

Two-Dimensional Arrays Lesson xx

  • Upload
    iram

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

Two-Dimensional Arrays Lesson xx. Objective. We will cover the following topics that deal with two-dimensional array: Declaration Subscripts Initialization Operations Application in a program. Two Dimensional Array Declaration. int m [3] [4];. col 1. col 2. col 3. col 0. - PowerPoint PPT Presentation

Citation preview

Slide 1

Two-Dimensional Arrays Lesson xx

In this presentation, well talk about two-dimensional arrays.1ObjectiveWe will cover the following topics that deal with two-dimensional array:

Declaration SubscriptsInitializationOperationsApplication 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.

2Two Dimensional Array Declarationint m [3] [4];row 0row 1row 2col 0col 1col 2col 3

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.3More Examples of 2-D Arraysfloat 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.4Subscriptsint m [3] [4];row 0row 1row 2col 0col 1col 2col 3m [2] [1]m [1] [3]

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.5Initialization with { } int m[2][3] = {{3, 6, 7}, {4, 7, 5}};

677453

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 well demonstrate in the next slide.6Initializing Partial Rows

int m[2][3] = {{3, 6 }, {4} };

600403604003 int m[2][3] = { 3, 6 , 4 };

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.7Two-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][3]; cout > d>> w; cout > 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.14Compute Cost cout