160 likes | 290 Views
Arrays and Matrices. One-Dimensional Arrays. An array is an indexed data structure All variables stored in an array are of the same data type An element of an array is accessed using the array name and an index or subscript
E N D
Arrays and Matrices Etter/Ingber
One-Dimensional Arrays • An array is an indexed data structure • All variables stored in an array are of the same data type • An element of an array is accessed using the array name and an index or subscript • The name of the array is the address of the first element and the subscript is the offset • In C, the subscripts always start with 0 and increment by 1 Etter/Ingber
Definition and Initialization • An array is defined using a declaration statement. datatypearray_name[size]; • allocates memory for size elements • subscript of first element is 0 • subscript of last element is size-1 • size must be a constant Etter/Ingber
Example int list[10]; • allocates memory for 10 integer variables • subscript of first element is 0 • subscript of last element is 9 • C does not perform any bounds checking on arrays list[0] list[1] list[9] Etter/Ingber
Initializing Arrays • Arrays can be initialized at the time they are declared. Examples: double taxrate[3] ={0.15, 0.25, 0.3}; char list[5] = {‘h’,’e’,’l’,’l’,’o’}; double vector[100] = {0.0}; /*assigns zero to all 100 elements*/ int s[] = {5,0,-5}; /*the size of a s is 3 */ Etter/Ingber
Assigning values to an array • for loops are often used to assign values to an array Example: int list[10], i; for(i=0; i<10; i++) { list[i] = i; } Etter/Ingber
Input from a data file • Arrays are often used to store information from a data file • Example • int k; • double time[10], motion[10]; • FILE *sensor3; • sensor3 = fopen(“sensor3.dat”, “r”); • for(k=0; k<10; k++) • { • fscanf(sensor3, “%lf %lf”,&time[k], &motion[k]); • } Etter/Ingber
Practice! • Show the contents of the arrays defined in each of the following sets of statements. • int x[10] = {-5, 4, 3}; • char letters[] = {'a', 'b', 'c'}; • double z[4]; Etter/Ingber
Function Arguments • Individual elements of an array can be passed as regular arguments. Example int main(void) { /* Declare variables and functions */ void fun donothing(int, int); int array[5] = {1,2,3,4,5}; donothing(array[2], array[4]); . . Etter/Ingber
Passing Entire Arrays as Arguments to Functions • Arrays are always pass by reference • The array name is the address of the first element • The maximum size of the array must be specified at the time the array is declared. The actualnumber of array elements that are used will vary, so the actualsize of the array is usually passed as another argument to the function Etter/Ingber
Example int main(void) { /* Declare variables and functions */ FILE *exp1; double max (double array[], int actual_size); double x[100]; int count=0; exp1 = fopen(“exp1.dat”, “r”); while((fscanf(exp1, “%f”, &x[count])) == 1) { count++; } printf(“Maximum value: %f \n”, max(x, count)); fclose(exp1); return 0; }//end main Etter/Ingber
Selection Sort void selection_sort(double x[], int n) { /* Declare variables and functions */ int max_pos, i; int find_max_pos(double x[], int n, int i); void swap(double x[], int p1, int p2); for(i=0; i<n-1; i++) { max_pos = find_max_pos(x, n, i); swap(x, i, max_pos); } }//end selection_sort Etter/Ingber
Modify! Write the function definitions for the functions • int find_max_pos(double x[], int n, int i); • void swap(double x[], int p1, int p2); Etter/Ingber
Matrices • A matrix is a set of number arranged in a grid with rows and columns. A matrix is defined using a type declaration statement. • data typearray_name[row_size][column_size]; • int matrix[3][4]; row[0] row[1] row[2] in memory row0 row1 row2 Etter/Ingber
Accessing Array Elements • int matrix[3][4]; • matrix has 12 integer elements • matrix[0][0] element in first row, first column • matrix[2][3] element in last row, last column • matrix is the address of the first element • matrix[1] is the address of the second row Etter/Ingber
2-Dimensional Arrays as Arguments to Functions Example: void transpose(int b[NROWS][NCOLS], int bt[NCOLS][NROWS]) { /* Declare Variables. */ int i, j; /* Transfer values to the transpose matrix. */ for(i=0; i<NROWS; i++) { for(j=0; j<NCOLS; j++) { bt[j][i] = b[i][j]; } } return; } Etter/Ingber