Chapter 10 TwoDimensional Arrays 10 1 Two Dimensional

  • Slides: 9
Download presentation
Chapter 10 Two-Dimensional Arrays 10. 1 Two Dimensional Arrays A two-dimensional array may be

Chapter 10 Two-Dimensional Arrays 10. 1 Two Dimensional Arrays A two-dimensional array may be thought of as being composed of horizontal rows and vertical columns. In mathematics, the elements of a two-dimensional array are identified by means of two subscripts placed after the array name. The first subscript refers to the row number, and the second subscript refers to the column number. In mathematical notation a two-dimensional array can be written as follows:

10. 1. 1 Declaring Two-Dimensional Arrays As with one-dimensional arrays, we have to first

10. 1. 1 Declaring Two-Dimensional Arrays As with one-dimensional arrays, we have to first declare a two-dimensional array so that the compiler can set aside memory storage for the elements of the array. The general form of the declaration for a two-dimensional array is as follows. type_specifier array_name[num_rows][num_cols; [ where type_specifier is the data type for all elements of the array, array_name is the name of the array, num_rows is the number of rows, and num_cols the number of columns in the arrays.

10. 1. 2 Initializing Two-dimensional Arrays Two-dimensional arrays may be initialized in a manner

10. 1. 2 Initializing Two-dimensional Arrays Two-dimensional arrays may be initialized in a manner similar to the initialization of one-dimensional arrays. Two dimensional arrays are initialized be rows. To separate the rows, each row of initiaizers can be enclosed in braces. For example, the statement int x [3] [4} = [ , {1, 2, 3, 4} , {5, 6, 7, 8} {9, 10, 11, 12} ; {

Initializes the array x [] to the following values:

Initializes the array x [] to the following values:

Example: Sum of Rows and Columns of Two-Dimensional Array problem statement: write a program

Example: Sum of Rows and Columns of Two-Dimensional Array problem statement: write a program to: . 1 Compute and print the sum of each row of the array. . 2 Compute and print the sum of each column of the array.

#include <stdio. h< #define NUM_ROWS 3 #define NUM_COLS 5 main() } int x [NUM_ROWS]

#include <stdio. h< #define NUM_ROWS 3 #define NUM_COLS 5 main() } int x [NUM_ROWS] [NUM_COLS} = [ , {75 , 30 , 20 , 10 , 5} , {70 , 40 , 35 , 65 , 25} {85 , 55 , 45 , 35 , 15} ; { int row, col, sum; for( row = 0; row < NUM_ROWS; row(++ } sum = 0; for (col = 0; col < NUM_COLS; col(++ sum += x[row] [col; [ printf(“n The sum of elements in row %d is %d”, row+1, sum; ( { printf(“n; (”

for(col = 0; col < NUM_COLS; col(++ } sum = 0; for(row = 0;

for(col = 0; col < NUM_COLS; col(++ } sum = 0; for(row = 0; row < NUM_ROWS; row(++ sum += x[row] [col; [ printf(“n The sum of elements in col %d is %d”, col+1, sum; ( { { Program output: The sum of elements in row 1 is 140 The sum of elements is row 2 is 235 The sum of elements is row 3 is 235 The sum of elements in col 1 is 45 The sum of elements in col 2 is 110 The sum of elements in col 3 is 100 The sum of elements in col 4 is 125 The sum of elements in col 5 is 230

Any Questions

Any Questions