Lecture 7

advertisement

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.

Multidimensional Arrays

 Topics

 Concept of multidimensional arrays

 Comparing one- and multidimensional arrays

 Initialization multidimensional arrays

 Printing multidimensional arrays

8.1 Introduction to Two-

Dimensional Arrays

 Declaration Statement

 Storage Allocation

 Array Initialization

Multidimensional Arrays

 What is a multidimensional array?

B =

51, 52, 53

54, 55, 56

Col 1 Col 2 Col 3

Row 1

Row 2

Array type

Array name

Array dimension = 2

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

Two rows

First row second row

Three columns

Algebraic notation C notation

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

Declaration Statement

Storage Allocation

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.

Array Initialization

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

Standard Input

How to read the data from a file?

 fscanf (infile, "%d %d", &year, &month) ; for (day=1; day<=31; day++) Loop over each day in a month.

{ fscanf(infile, "%d",&rainfall[year][month][day]);

}

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

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.

Input from a Data File

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

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.

Standard Output

Output 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.

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]);

}

}

8.4 Manipulation of Arrays

 Array Assignment

 Array Arithmetic

 Matrix Operation

Array Assignment

Array Arithmetic

Matrix Operation

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

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

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]);

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]);

Address passed to pointer variable

Value passed to simple variable void function1(int *d, int e)

*d = 100 + e;

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.

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);

Function Call and Prototype function2(c, 5)

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

Number of array elements passed as a simple variable void function2 (double b[ ] , int num_elem);

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)

This is correct –

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

 void function2 (double b[5] )

This is incorrect

It does not indicate that b[ ] has only five elements.

Passing Fixed Sized Arrays

Passing Array Elements

8.6 Higher-Dimensional Arrays

 Declaration and Storage Allocation

 Input of Three-Dimensional Arrays

 Output of Three-Dimensional Arrays

 Manipulation of Three-Dimensional Arrays

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.

3-D Arrays

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

Leftmost subscript

I =1

Rightmost subscript k = 0 k = 1 k = 2 k = 3

I =0

J = 0

J = 1

J = 2

Middle subscript

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]

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");

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.

Input of Three-Dimensional

Arrays

Output of Three-Dimensional

Arrays

Manipulation of Three-

Dimensional Arrays

Sample Programs

 Drag Force

 Saddle Point

 Computation of Pressure

 Geometric Transformations

 Inventory of Cars in the XYZ Dealership

Download