multiple subscripted array l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Multiple-Subscripted Array PowerPoint Presentation
Download Presentation
Multiple-Subscripted Array

Loading in 2 Seconds...

play fullscreen
1 / 16

Multiple-Subscripted Array - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

Multiple-Subscripted Array. What it is How to use it How to declare it How to initialize it. Multiple-Subscripted Arrays. Use more than one subscript (can have up to 12) Double-subscripted array (2 subscripts). Double-subscripted array. good for representing tables in memory

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Multiple-Subscripted Array' - dara


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
multiple subscripted array
Multiple-Subscripted Array
  • What it is
  • How to use it
  • How to declare it
  • How to initialize it
multiple subscripted arrays
Multiple-Subscripted Arrays
  • Use more than one subscript (can have up to 12)
  • Double-subscripted array (2 subscripts)
double subscripted array
Double-subscripted array
  • good for representing tables in memory
  • one subscript is the row
  • second subscript is the column
double subscripted array4
Double-subscripted array

column 0

column 1

column 2

column 3

row 0

a[0][0]

a[0][1]

a[0][2]

a[0][3]

row 1

a[1][0]

a[1][1]

a[1][2]

a[1][3]

a[2][0]

a[2][1]

a[2][2]

a[2][3]

row 2

column subscript

row subscript

array name = a

a double-subscripted array with 3 rows and 4 columns

3 by 4 array

referencing elements in a double subscripted array
Referencing elements in a double-subscripted array

To set the element in row 2 and column 3 to the value 4 use:

a [2] [3] = 4;

column subscript

row subscript

name of the array

declaring a 3 by 4 array
Declaring a 3 by 4 array

int a [3] [4];

must tell compiler name, size (2 sizes) and type

all elements of the array must be the same type

any data type is fine (int, float, char, etc.)

declaring a double subscripted array
Declaring a double-subscripted array

Must use constants to declare row and column sizes.

int a [3] [4];

char letters [2] [26];

#define ROW 3

#define COLUMN 4

int a [ROW] [COLUMN];

const int row = 3;

const int column = 4;

int a [row] column];

initializing a double subscripted array
Initializing a double-subscripted array

const int row = 3;

const int column = 4;

int a [row] [column];

for (int i = 0; i < row; i++) {

for (int j = 0; j < column; j++) {

a [i][j] = 0;

}

}

initializing a double subscripted array with declaration
Initializing a double-subscripted array with declaration

const int row = 3;

const int column = 4;

int a[row] [column] = {{1,2,3,4},

{1,2,3,4},

{1,2,3,4}};

1

2

3

4

3

1

2

4

3

4

1

2

initializing a double subscripted array with declaration10
Initializing a double-subscripted array with declaration

const int row = 3;

const int column = 4;

int a[row] [column] = {{1,2,3},

{1,2},

{1,2,3,4}};

1

2

3

0

0

1

2

0

3

4

1

2

setting entire array to 0
Setting entire array to 0

const int row = 3;

const int column = 4;

int a [row] [column] = {0};

0

0

0

0

0

0

0

0

0

0

0

0

exercises
Exercises

Answer the following questions regarding an array called table that is a double-subscripted array with 2 rows and 3 columns.

1) Define constant variables numRows and numCols to be used to declare the array.

2) Declare the array to be an integer array.

3) Use a for loop to initialize each element of the array to be the sum of it’s subscripts

4) Write a code segment to print the array as a table.

how double subscripted arrays are stored in memory
How double-subscripted arrays are stored in memory

table

1

2

3

4

5

6

7

8

9

10

11

12

table

1

2

3

4

5

6

7

8

9

10

11

12

passing double subscripted array to a function
passing double-subscripted array to a function
  • Must tell function size of second subscript so it can find the beginning of each row in memory.
  • void PrintTable (int table [ ] [4], int numRows, int numCols) {
    • for (int i = 0; i < numRows; i++) {
    • for (int j = 0; j < numCols; j++) {
    • cout << setw(4) << table [i] [j];
    • }
    • cout << endl;
    • }
  • }
passing double subscripted array to a function15
passing double-subscripted array to a function
  • passed as reference parameter
  • Use const to prevent function changing the array.

void PrintTable (const int table [ ] [4], int numRows, int numCols);

exercises16
Exercises

Write a main program as follows:

Use the results of the last set of exercises to declare an array of type integers with 2 rows and 3 columns and initialize each element to the sum of it’s subscripts. Call the PrintTable function to print the array.

Create a function called PrintTable to print each element of the array in tabular form.