+ All Categories
Home > Documents > Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by...

Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by...

Date post: 23-Dec-2015
Category:
Upload: ashlie-turner
View: 220 times
Download: 3 times
Share this document with a friend
Popular Tags:
72
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Transcript
Page 1: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Chapter 8 Multidimensional Arrays

C Programming for Scientists & Engineers with Applications

by Reddy & Ziegler

Page 2: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Multidimensional Arrays

Multidimensional arrays are derived from the basic or built-in data types of the C language.

Two-dimensional arrays are understood as rows and columns with applications including two-dimensional tables, parallel vectors, and two-dimensional matrices.

The data stored in multidimensional arrays must be homogeneous. This type of data structure and its applications are very common in science and engineering.

Page 3: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Multidimensional Arrays

Topics Concept of multidimensional arrays Comparing one- and multidimensional arrays Initialization multidimensional arrays Printing multidimensional arrays

Page 4: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

8.1 Introduction to Two-Dimensional Arrays

Declaration Statement Storage Allocation Array Initialization

Page 5: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Multidimensional Arrays

What is a multidimensional array?

B = 51, 52, 53

54, 55, 56

Algebraic notation

Col 1 Col 2 Col 3

Row 1

Row 2Int b[2][3] = {51, 52, 53, 54, 55, 56};

Array type Array name

Array dimension = 2

Two rows

Three columns

First row second row

C notation

Page 6: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 7: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 8: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 9: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Multidimensional Arrays

How to declare a multidimensional array?

int b[2][3];

declares

the name of the array to be b

the type of the array elements to be int

the dimension to be 2 (two pairs of brackets [])

the number of elements or size to be 2*3 = 6

Page 10: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Declaration Statement

Page 11: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 12: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Storage Allocation

Page 13: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 14: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Multidimensional Arrays How to initialize a multidimensional array?

Initialized directly in the declaration statement int b[2][3] = {51, 52, 53, 54, 55, 56}; b[0][0] = 51 b[0][1] = 52 b[0][2] = 53

Use braces to separate rows in 2-D arrays. int c[4][3] = {{1, 2, 3},

{4, 5, 6},

{7, 8, 9},

{10, 11, 12}}; int c[ ][3] = {{1, 2, 3},

{4, 5, 6},

{7, 8, 9},

{10, 11, 12}};

Implicitly declares the number of rows to be 4.

Page 15: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Array Initialization

Page 16: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 17: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 18: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 19: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 20: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

8.2 Input of Two-Dimensional Arrays

Data may be input into two-dimensional arrays using nested for loops interactively or with data files.

Standard Input Input from a Data File

Page 21: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Standard Input

Page 22: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

How to read the data from a file? fscanf (infile, "%d %d", &year, &month) ; for (day=1; day<=31; day++) { fscanf(infile, "%d",&rainfall[year][month][day]); }

Note that the values of the first two subscripts for rainfall do not change. Only the last subscript changes.

The function fscanf automatically skips to the next line when it is looking for the next non-white-space character.

Loop over each day in a month.

Using the previously read values for year and month, read each day’s rainfall value.

Page 23: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Input from a Data File

Page 24: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 25: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

8.3 Output of Two-Dimensional Arrays

The output of two-dimensional arrays should be in the form of rows and columns for readability. Nested for loops are used to print the rows and columns in row and column order.

Standard Output Output to a Data File

Page 26: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

How to print the data to the screen?

printf ("Rainfall for Year = %d, Month = %d\n\n",year, month); for (day=1; day<=31; day++) { printf("%d ",rainfall[year][month][day]); if (day==7 || day==14 || day==21 || day==28) printf("\n"); } Illustrate some of the concepts of printing arrays.

Page 27: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Standard OutputOutput to a monitor or printer:

int a[2][3] = {5, 6, 9, 4, 2, 10};

int i, j;. . . .for (i = 0; i < 2; i++){ for(j = 0; j < 3; j++) {

printf(“%d “, a[i][j]); } printf(“\n”);}

Note that at the end of each row, a line feed character is output so that the next row is on the next line.

Page 28: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Output to a Data File

Output to a data file:int a[2][3];int i, j;. . . .for (i = 0; i < 2; i++){ for(j = 0; j < 3; j++) { fprintf(outptr, “%d “, a[i][j]); }}

Page 29: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

8.4 Manipulation of Arrays

Array Assignment Array Arithmetic Matrix Operation

Page 30: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Array Assignment

Page 31: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 32: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 33: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 34: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 35: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 36: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Array Arithmetic

Page 37: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Matrix Operation

Page 38: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 39: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 40: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 41: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 42: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 43: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 44: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

8.5 Passing Arrays to Functions Two-dimensional arrays may by passed by array

name. Because arrays are stored by rows, in order to

accurately locate an element, a function must know the length of a row: that is the number of columns.

This must be included in both the function prototype and the header of the function definition.

Passing Fixed Sized Arrays Passing Array Elements

Page 45: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Functions and 1-D Arrays

Topics Passing individual array elements to functions Passing entire arrays to functions Passing entire arrays to functions with a restriction Illustrate how values and address of array elements are

passed

Page 46: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Function Call

How to pass a single array element to a function? We treat a single element like a simple variable. If we want to change the value of the array element in

the function, we use the “address of” operator, &, before the array element in the function call.

If we want to pass element without having it changed in the function, we simple put the array element in the parameter list.

function1(&a[5], a[8]);

Page 47: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Function Prototype

When receiving an address, we must use a pointer variable (indicated in the declaration by *).

When receiving a value, we use a simple variable. void function1(int *d, int e);

function1(&a[5], a[8]);

void function1(int *d, int e)

Value passed to simple variable

Address passed topointer variable

*d = 100 + e;

Page 48: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Function Call and Prototype

How to pass the ability to access an entire 1-D array to a function? Pass the address of the first element of the array With the address of the first element, C can internally

calculate the address of any element in the array. The address of the first element of an array is indicated

by the array name with no brackets following it. &c[0] and c are equivalent.

Page 49: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Function Call and Prototype function2(c, 5)

Pass the address of the first element of the array c[ ] to function2, which gives function2 the ability to modify the array c[ ].

The prototype for the function must indicate that it is receiving an address. Use * in the declaration

void function2 (double *b, int num_elem); Use brackets [ ]

void function2 (double b[ ], int num_elem);

Page 50: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Function Call and Prototype

function2(c, 5)

void function2 (double b[ ], int num_elem);

Number of array elements passed as a simple variable

Address of the first element passed to pointer variable indicated with brackets

Page 51: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Function Definition

Within a function that has received an array’s address, how to work with the array? Must be cognizant of the number of elements that the

array contains. void function2 (double b[ ], int num_elem)

void function2 (double b[5])

This is correct – A separate parameter is used to transfer the number of elements.

This is incorrect – It does not indicate that b[ ] has only five elements.

Page 52: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Passing Fixed Sized Arrays

Page 53: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 54: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 55: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 56: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Passing Array Elements

Page 57: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

8.6 Higher-Dimensional Arrays

Declaration and Storage Allocation Input of Three-Dimensional Arrays Output of Three-Dimensional Arrays Manipulation of Three-Dimensional Arrays

Page 58: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Higher-Dimensional Arrays Three-dimensional arrays are very common in

scientific and engineering problems dealing with three-dimensional geometries.

Some examples of such problems include: the stress analysis in three-dimensional solids and

structures the computational analysis of velocity and pressure used to

compute the drag and lift character of airplane wings wave motion and vibration analysis in three-dimensional

solids and structures. Problems that deal with three space coordinates and

time require four dimensions.

Page 59: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

3-D Arrays

3-D array of size [2][3][4]

k = 0 k = 1 k = 2 k = 3

Rightmost subscript

J = 0

J = 1

J = 2

Middle subscript

I =1

I =0

Leftmost subscript

Page 60: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Memory Location

a[0][0][0] a[1][0][0] a[0][0][1] a[1][0][1] a[0][0][2] a[1][0][2] a[0][0][3] a[1][0][3] a[0][1][0] a[1][1][0] a[0][1][1] a[1][1][1] a[0][1][2] a[1][1][2] a[0][1][3] a[1][1][3] a[0][2][0] a[1][2][0] a[0][2][1] a[1][2][1] a[0][2][2] a[1][2][2] a[0][2][3] a[1][2][3]

Page 61: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Memory Location a[x][y][z] of an array declared with a size a[I][J][K]

Sequence location = x*(J*K) + y*(K) + z + 1 For a 3-D a[2][3][4] array, the sequence location for

element a[0][1][2] is 0*(3*4) + 1*(4) + 2 +1 = 7 How to use a loop to print a multidimensional array?

for (i=0; i<2; i++) { for (j=0; j<3; j++) { printf("b[%1d][%1d] = %5d", i, j, b[i][j]); } printf("\n"); }

Page 62: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Declaration and Storage Allocation Declaration of three-dimensional arrays requires

three indices: plane, row, and column. On each plane, the row and column indices are

the same and the plane index varies from one plane to the next.

The plane index varies from 0 to one less than the number of planes, the row index varies from 0 to one less than the number of rows, and the column index varies from 0 to one less than the number of columns.

Page 63: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 64: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 65: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 66: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Input of Three-Dimensional Arrays

Page 67: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 68: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Output of Three-Dimensional Arrays

Page 69: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Manipulation of Three-Dimensional Arrays

Page 70: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 71: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Page 72: Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Sample Programs

Drag Force Saddle Point Computation of Pressure Geometric Transformations Inventory of Cars in the XYZ Dealership


Recommended