Lecture 21 Arrays and Stringscont Introduction to Computer




![Two-Dimensional Arrays(cont. ) n Example: double sales[10][5]; 5 Two-Dimensional Arrays(cont. ) n Example: double sales[10][5]; 5](https://slidetodoc.com/presentation_image_h2/bca754d91f5455888bcfa484bb642802/image-5.jpg)











- Slides: 16

Lecture 21: Arrays and Strings(cont. ) Introduction to Computer Science Spring 2006 1

Arrays n Array - a collection of a fixed number of elements n n n All the elements of an array must be the same data type; The elements of an array are stored sequentially in memory. One-dimensional array - an array in which the components are arranged in a list form n The syntax for declaring a one-dimensional array is: data. Type array. Name[int. Exp]; where int. Exp is any expression that evaluates to a positive integer n Example: int num[5]; 2

#include <iostream> #include <iomanip> using namespace std; const int array. Size = 10; void fill_Array(double list[], int list. Size) { for(int i=0; i<list. Size; ++i) cin>>list[i]; } void print_Array(double list[], int list. Size) { for(int i=0; i<list. Size; ++i) cout<<setw(5)<<list[i]; } void copy_Array(const double list. One[], double list. Two[], int list. One. Size) { for(int i=0; i<list. One. Size; ++i) list. Two[i] = list. One[i]; } void square_Array(double list[], int list. Size) { int k; for (int i = 0; i < list. Size; i++) { k = i + 1; /* i runs from 0 to 9 */ /* k runs from 1 to 10 */ list[i] = k*k; cout << "The square of " << k << " is " << list[i] << endl; } } int main() { double list. A[array. Size]={0}; double list. B[array. Size]; fill_Array(list. A, array. Size); print_Array(list. A, array. Size); copy_Array(list. A, list. B, array. Size); square_Array(list. A, array. Size); } 3

Two-Dimensional Arrays n Two-dimensional Array: a collection of a fixed number of components arranged in two dimensions n The syntax for declaring a two-dimensional array is: data. Type array. Name[intexp 1][intexp 2]; where intexp 1 and intexp 2 are expressions yielding positive integer values n n n The two expressions intexp 1 and intexp 2 specify the number of rows and the number of columns, respectively, in the array Two-dimensional arrays are sometimes called matrixes or tables Two-dimensional arrays are stored in row order n The first row is stored first, followed by the second row, followed by the third row and so on 4
![TwoDimensional Arrayscont n Example double sales105 5 Two-Dimensional Arrays(cont. ) n Example: double sales[10][5]; 5](https://slidetodoc.com/presentation_image_h2/bca754d91f5455888bcfa484bb642802/image-5.jpg)
Two-Dimensional Arrays(cont. ) n Example: double sales[10][5]; 5

Accessing Array Components n The syntax to access a component of a two-dimensional array is: array. Name[indexexp 1][indexexp 2] where indexexp 1 and indexexp 2 are expressions yielding nonnegative integer values n n indexexp 1 specifies the row position and indexexp 2 specifies the column position Example: sales[5][3]=25. 75; 6

Accessing Array Components n A common way to access the elements of a two-dimensional arrays is with nested for loops. const int MAXI = 50; const int MAXJ = 75; int main() { int i; int j; float values[MAXI][MAXJ]; } for (i = 0; i < MAXI; i++) { for (j = 0; j < MAXJ; j++) { values[i][j] = whatever; } } 7

Initialization n Like one-dimensional arrays n n Two-dimensional arrays can be initialized when they are declared Example: n int board[4][3] = { {2, 3, 1}, {15, 25, 13}, {20, 4, 7}, {11, 18, 14} } To initialize a two-dimensional array when it is declared: • Elements of each row are enclosed within braces and separated by commas • All rows are enclosed within braces • For number arrays, if all components of a row are not specified, the unspecified components are initialized to zero 8

Processing Two-Dimensional Arrays n A two-dimensional array can be processed in three different ways: 1. Process the entire array 2. Process a particular row of the array, called row processing 3. Process a particular column of the array, called column processing 9

Processing Two-Dimensional Arrays : Row processing and Column processing n We can process a particular row or column of a twodimensional array as a one-dimensional array n n use algorithms similar to processing one-dimensional arrays For example: n the following for loop initializes row four to zero: row = 4; for(col = 0; col < columns; col++) matrix[row][col] = 0; n The following for loop inputs data in row 4 of matrix: row = 4; for(col = 0; col < columns; col++) cin>>matrix[row][col]; 10

Processing Two-Dimensional Arrays : Entire Array n n We can process the entire array by using nested for loop Example: n The following nested for loop initialize the entire matrix: for(row = 0; row < rows; row++) { for(col = 0; col < columns; col++) { matrix[row][col] = 0; } } 11

Print n The following nested for loops print the components of matrix, one row per line: for(row = 0; row < rows; row++) { for(col = 0; col < columns; col++) { cout<<setw(5)<<matrix[row][col]<<" "; } cout<<endl; } 12

Input n The following nested loop input data in each component of the matrix for(row = 0; row < rows; row++) for(col = 0; col < columns; col++) cin>>matrix[row][col]; 13

Passing Two-Dimensional Arrays as Parameters to Functions n n Two-dimensional arrays can be passed as parameters to a function By default, arrays are passed by reference The base address, that is, the address of the first component of the actual parameter is passed to the formal parameter When declaring a two-dimensional array as a formal parameter n n Can omit size of first dimension, but not the second Number of columns must be specified 14

#include <iostream> #include <iomanip> using namespace std; const int MAXI = 10; const int MAXJ = 15; void init_matrix(double matrix[MAXI][MAXJ]) { for(int i=0; i<MAXI; ++i) for(int j=0; j<MAXJ; ++j) matrix[i][j]=i+j; } void print_matrix(double matrix[MAXI][MAXJ]) { for(int i=0; i<MAXI; ++i) { for(int j=0; j<MAXJ; ++j) cout<<setw(5)<<matrix[i][j]; cout<<endl; } } double sum_matrix(double matrix[MAXI][MAXJ]) { double sum=0. 0; for(int i=0; i<MAXI; ++i) for(int j=0; j<MAXJ; ++j) sum = sum + matrix[i][j]; return sum; } int main() { double matrix[MAXI][MAXJ]; double sum; init_matrix(matrix); print_matrix(matrix); sum = sum_matrix(matrix); } cout<<"The sum of all the elements of matrix is "<<sum<<endl; 15

End of lecture 21 Thank you! 16