1 / 16

Arrays and Matrices

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

Download Presentation

Arrays and Matrices

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and Matrices Etter/Ingber

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related