defining a 2d array a 2d array implements a matrix. example: #define numrows 5 #define numcols 10...

17

Upload: kathlyn-parker

Post on 14-Jan-2016

245 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4
Page 2: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Defining a 2d Array

A 2d array implements a MATRIX.

Example:

#define NUMROWS 5

#define NUMCOLS 10

int arr[NUMROWS][NUMCOLS];0 1 2 3 4 5 6 7 8 9

0

1

2

3

4

Page 3: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

When would you use a 2d arraySuppose you have multiple elements of

data about several people (or records).

Example:

You have 8 grades each for 30 students.

Define a 2d array to store the grades.

double grades[30][8];

Page 4: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

To access an element in a 2d array, use double square brackets.

Example:

grades[i][j]=score;

cout << grades[0][0];

Page 5: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Iterating through a 2d array

Use a nested loop.

Example: create an NxN identity matrix.

const in N=10;

int identity[N][N];

for (int i=0; i<N; i++)

for (int j=0; j<N; j++)

if (i==j) identity[i][j]=1;

else identity[i][j]=0;

Page 6: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Initializing a 2d array

Use double curly braces { { } }

int identity[N][N]={ {1,0,0},

{0,1,0}

{0,0,1} } ;

Page 7: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Passing a 2d array to a function

RULE: You can leave out the number of rows, but you MUST specify the number of columns.

example:

void print_array(int arr[][COLS], int numrows);

Page 8: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

In Memory

Although we think of a 2d array as a matrix, memory is 1d, and therefore a multidimensional array is actually stored as a 1d array in memory.

It is stored in row-major order.

Row 0 Row 1 Row 2 Row 3

Page 9: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Relating 2d arrays to pointers (e.c.)

int arr[numrows][numcols];

arr[0] refers to the address of row 0. That is, it is of type 1d array (alternatively int*).

So, *arr[0] is the contents of the first element of the first row (i.e. arr[0][0]).

arr[i][j] is translated by the compiler into:

*(arr[i]+j) *(*(arr+i)+j)

Page 10: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

// USING 2D ARRAYS

const int NUM_STUDENTS = 30; const int NUM_SCORES = 8; double scores[NUM_STUDENTS][NUM_SCORES]; double total, average;  // for each student (row) for (int i=0; i<NUM_STUDENTS; i++) get_scores(scores, NUM_STUDENTS); // read in the students’ scores for (int i=0; i<NUM_STUDENTS;i++) // calculate each student’s average { total=0; for (int j=0; j < NUM_SCORES; j++) {

total+=scores[i][j]; } // end for j average = total/NUM_SCORES; cout << “average for student” << i+1 << “is” << average << endl;  } // end for I

Page 11: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Think about:

What if you would want the class average for exam 2?

Page 12: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4
Page 13: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

1d array of pointers

In picture form:

each location

can hold an

address.

0

1

2

3

4

5

6

Page 14: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

When to use

You have 30 students, but each takes a different number of exams. You can dynamically allocate an array of scores for each student.

You have 1000 accounts, and each account has its own transaction list. Define an array of pointers to char, and let each account have a dynamically allocated array of transactions.

Page 15: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

How can we do that??

char *transactions[NUM_ACCOUNTS];

// transactions is an array of type pointer to char

transactions[0] = new char [transnumber];

// now, the first location of the transactions array points to the beginning of a dynamically allocated array.

Page 16: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

Using the array of pointers

Since a pointer is allowed to be used with subscripting [ ], we can use the array of pointers as if it were a 2d array.

So,

transactions[0][j] refers to the jth element in the 0th array of the transactions array.

translated by compiler into:

*(transactions[0]+j)

Page 17: Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS]; 0123456789 0 1 2 3 4

see handout.