Top Banner
A.Abhari CPS125 1 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two- dimensional array and box[2][1] is an element in row 2 , column 1 and char box[][3] can be used in the function prototype
27

A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

Jan 12, 2016

Download

Documents

Mervyn King
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 1

Multidimensional Arrays

Multidimensional array is the array with two or more dimensions.

For example:

char box [3] [3] defines a two-dimensional array

and

box[2][1] is an element in row 2 , column 1

and

char box[][3] can be used in the function prototype

note that only the first dimension can be omitted

Page 2: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 2

Multidimensional Arrays• For example :

double table [NROWS][NCOLS];

Can be used as parameter in the function prototype as:

void

process_martrix( int in[ ][4],

int out[ ][4],

int nrows)

Page 3: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 3

Two Dimensional Array• Char box [3] [3]

0

1

2

0 1 2

Row

Column

box [1] [2]

Page 4: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 4

/* * Checks whether a box is completely filled */intfilled(char box[3][3]) /* input - box to check */{ int r,c, /* row and column subscripts */ ans; /* whether or not box is filled. */ /* Assumes box is filled until blank is found */ ans = 1; /* Resets ans to zero if a blank is found */ for (r = 0; r < 3; ++r) for (c = 0; c < 3; ++c) if (box[r][c] == ' ') ans = 0; return (ans);}

Page 5: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 5

Arrays with Several Dimensions

• int soil_type[4] [7] [MAXDEPTH]

Page 6: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 6

Case Study: Cellular Telephone System

• Problem: Finding the best way to build a cellular network. There is some marketing data that predicts the demand will be at tree time of interest. There are only 10 transmitters and there is a need for a program to help analyzing call demand data.

Page 7: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 7

Case Study: Cellular Telephone System

• Analysis: There should be three matrices shows traffic density for each time of the day:

Input: int commuters[GRID_SIZE][GRID_SIZE] int salesforce[GRID_SIZE][GRID_SIZE] int weekend[GRID_SIZE][GRID_SIZE]Output:int summed_data[GRID_SIZE][GRID_SIZE] int location_i, location_j

Page 8: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 8

Case Study: Cellular Telephone System

• Design: initial algorithm:

1. Get traffic data for three time period

2. Get the weights from user

3. Multiply weight by each matrix entry and store the sum in the summed data

4. Find highest valued cells in the summed data and display them as the pair of location_i and location_j

• Implementation

Page 9: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

Filling the multidimensional array/* Fills 3 GRID_SIZE x GRID_SIZE arrays with traffic data from

TRAFFIC_FILE*/voidget_traffic_data(int commuters[GRID_SIZE][GRID_SIZE], /* output */ int salesforce[GRID_SIZE][GRID_SIZE], /* output */ int weekend[GRID_SIZE][GRID_SIZE]) /* output */{ int i, j; /* loop counters */ FILE *fp; /* file pointer */ fp = fopen(TRAFFIC_FILE, "r"); for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) fscanf(fp, "%d", &commuters[i][j]); for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) fscanf(fp, "%d", &salesforce[i][j]); for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) fscanf(fp, "%d", &weekend[i][j]); fclose(fp);}

Page 10: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 10

/* Computes and displays the weighted, summed_data */

for (i = 0; i < GRID_SIZE; ++i)

for (j = 0; j < GRID_SIZE; ++j)

summed_data[i][j] = commuter_weight*

commuters[i][j] +

salesforce_weight *

salesforce[i][j] +

weekend_weight *

weekend[i][j];

Modifying the multidimensional array

Page 11: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

/* Finds the NUM_TRANSMITTERS highest values in the summed_data matrix.Temporarily stores the coordinates in location_i and location_j, and then displays the resulting locations */

printf("\n\nLocations of the %d transmitters:\n\n", NUM_TRANSMITTERS);

for (tr = 1; tr <= NUM_TRANSMITTERS; ++tr) { current_max = SELECTED; /* Starts off our search with a value that is known to be too low. */ for (i = 0; i < GRID_SIZE; ++i) { for (j = 0; j < GRID_SIZE; ++j) { if (current_max < summed_data[i][j]) { current_max = summed_data[i][j]; location_i = i; location_j = j; } } }

Searching in the multidimensional array

Page 12: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 12

Printing the contents of multidimensional array/* * Displays contents of a GRID_SIZE x GRID_SIZE matrix of

integers */voidprint_matrix(int matrix[GRID_SIZE][GRID_SIZE]){ int i, j; /* loop counters */ for (i = 0; i < GRID_SIZE; ++i) { for (j = 0; j < GRID_SIZE; ++j) printf("%3d ", matrix[i][j]); printf("\n"); }}

Page 13: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 13

Vectors

• Vector: a mathematical object consisting of a sequence of numbers.

/* a vector <4, 12, 19> */

int vect[3] = {4, 12, 19};

• Differences between vector and array:

1- an n_dimensional vector is represented in C as a one dimensional array of size n.

2- vect3 is vect[2] in C

Page 14: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 14

Vectors

• Calculating scalar product:

<1,2,4>. <2,3,1> = 1*2 + 2*3 +4*1=12

In C:

sum_prod = 0;

for (k=0; k<n; k++)

sum_prod += x[k] * w[k];

Page 15: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 15

Matrices

• Matrix: a mathematical object consisting of a rectangular arrangement of numbers called the element of matrix..

/* a matrix 3 6

4 5

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

3 6

4 5

x

Page 16: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 16

Matrices• Multiplying a matrix by a vector

A * X = V

1 1 1 5

2 3 1 1 10 multiplication

1 -1 -1 * 2 = -3 on

0 1 2 2 6 the right• In C for each member of V:

v[i] = 0;

for (k=0; k<n; k++)

v[k] += a[i][k] * x[k];

Page 17: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 17

/* Computes the product of M-by-N matrix a and the N-dimensional vector x. The result is stored in the output parameter v, an M-dimensional vector.*/

void mat_vec_prod(double v[], /* M-dimensional vector */ double a[M][N], /* M-by-N matrix */ double x[]) /* N-dimensional vector */ { int i, k; for (i = 0; i < M; ++i) { v[i] = 0; for (k = 0; k < N; ++k) { v[i] += a[i][k] * x[k]; } } }

Page 18: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 18

Matrix Multiplication

1 1 1 2 0 1 6 0 0 2 3 1 * 1 -1 0 = 10 -2 1 1 -1 -1 3 1 -1 -2 0 2

for ( i=0; i< m , ++i) { for (j=0; j<p; ++j) { …….. compute c[i][j]…. }}

Page 19: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 19

/* Multiplies matrices A and B yielding product matrix C */ void mat_prod(double c[M][P], /* output - M by P matrix */ double a[M][N], /* input - M by N matrix */ double b[N][P]) /* input - N by P matrix */ { int i, j, k; for (i = 0; i < M; ++i) { for (j = 0; j < P; ++j) { c[i][j] = 0; for (k = 0; k < N; ++k) c[i][j] += a[i][k] * b[k][j]; } } }

Page 20: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 20

Solving System of Linear Equations

• To solve many problems such as force equation in three-dimensional system we need to solve a three linear equations:

A X = Y

1 1 1 x1 4

2 3 1 * x2 = 9

1 -1 -1 x3 -2It is multiplication of a matrix by a vector on

the right

Page 21: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 21

Gaussian Elimination

• Gaussian elimination can be used to solve a linear equation.

• The algorithm for Gussian elimination is:

1. Transform the original system into scaled triangular form.

2. Solve for xi by back substitution

Page 22: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 22

Gaussian Elimination

triangular form

1 1 1 x1 4

0 1 -1 * x2 = 1

0 0 1 x3 1 back substitution

x1 + x2 + x3 = 4

x2 - x3 = 1

x3 = 1

Page 23: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 23

Gaussian Elimination

• For doing that we need to triangularizing the augmented matrix by following operations:

1. Multiply any row of aug by nonzero number

2. Add to any row of aug a multiple of other rows

3. Swap any two rows• If system has a unique solution, we can get

the system into desired form by this three operations.

Page 24: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 24

/* * Performs pivoting with respect to the pth row and the pth column * If no nonzero pivot can be found, FALSE is sent back through piv_foundp */void pivot(double aug[N][N+1], /* input/output - augmented matrix */ int p, /* input - current row */ int *piv_foundp) /* output - whether or not nonzero pivot found */{ double xmax, xtemp; int j, k, max_row; /* Finds maximum pivot */ xmax = fabs(aug[p][p]); max_row = p; for (j = p+1; j < N; ++j) { if (fabs(aug[j][p]) > xmax) { xmax = fabs(aug[j][p]); max_row = j; } }

Page 25: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 25

/* Swaps rows if nonzero pivot was found */ if (xmax == 0) { *piv_foundp = FALSE; } else { *piv_foundp = TRUE; if (max_row != p) { /* swap rows */ for (k = p; k < N+1; ++k) { xtemp = aug[p][k]; aug[p][k] = aug[max_row][k]; aug[max_row][k] = xtemp; } } }}

Page 26: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

/* * Performs back substitution to compute a solution vector to a system of * linear equations represented by the augmented matrix aug. Assumes

that * the coefficient portion of the augmented matrix has been triangularized, * and its diagonal values are all 1. */voidback_sub(double aug[N][N+1], /* input - scaled, triangularized augmented matrix */ double x[N]) /* output - solution vector */{ double sum; int i, j; x[N - 1] = aug[N - 1][N]; for (i = N - 2; i >= 0; --i) { sum = 0; for (j = i + 1; j < N; ++j) sum += aug[i][j] * x[j]; x[i] = aug[i][N] - sum; }}

Page 27: A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

A.Abhari CPS125 27

Common Programming Errors

• Use constants for each dimension’s size when declaring multidimensional array

• When declaring the array as a parameter of a function if you omit the first dimension all other dimensions must be supplied

• Since access to the elements of a multidimensional array requires nested counting loops it is easy to make out-of-range error.

• Since using multidimensional arrays as local variables requires large memory space, you may need to tell to operating system to increase stack size when the program is running