1 / 24

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

mutter
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

  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

  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

  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]

  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 */

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

  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.txt”, “r”); for(k=0; k<10; k++) { fscanf(sensor3, “%lf %lf”,&time[k], &motion[k]); }

  8. Practice! Show the contents of the arrays defined in each of the following sets of statements. a) int x[10] = {-5, 4, 3}; b) char letters[] = {'a', 'b', 'c'}; • double z[4]; ……… z[1]=-5.5; z[2]=z[3]=fabs(z[1]); • int k; double time[9]; ……….. for(k=0;k<=8;k++) time[k]=(k-4)*0.1;

  9. Practice • Assume that the variable k and the array s have been defined with the following statement: int k, s[]={3,8,15,21,30,41}; • Using a hand calculation, determine the output for each of the following sets of statements: 1. for(k=0;k<=5;k+=2) printf(“%d %d \n”,s[k],s[k+1]); 2. for(k=0;k<=5;k++) if(s[k]%2==0) printf(“%d “,s[k]); printf(“\n”);

  10. Function Arguments • When the information in an array is passed to a function, two parameters are usually used. • One parameter specifies a particular array. • Other parameter specifies the number of elements used in the array.

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

  12. 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.txt”, “r”); while((fscanf(exp1, “%f”, &x[count])) == 1) { count++; } printf(“Maximum value: %f \n”, max(x, count)); fclose(exp1); return 0; }//end main

  13. Selection Sort Example: Ascending algorithm • Find the minimum value • Exchanging the minimum value with the value in the first position in the array • Find the next minimum value begin with second element and exchange this minimum value with the second position in the array • Continue until the last element

  14. Two-Dimensional Arrays

  15. Definition and Initialization • int x[rows][columns]; • Example, int x[4][3]; int x[4][3]={{2,3,-1},{0,-3,5},{2,6,3},{-2,10,4}}; • int x[][3]={{2,3,-1},{0,-3,5},{2,6,3},{-2,10,4}}; • If the initializing sequence is shorter than the array, the rest of the values are initialized to zero.

  16. Nested ‘for’ loop • Arrays can also be initialized with program statements. • Two nested ‘for ‘ loops are usually required to initialize an array. /* Declare variables */ int i, j, t[5][4]; …………. ………… /* Initialize array. */ for(i=0;i<=4;i++) for(j=0;j<=3;j++) t[i][j]=i;

  17. Example #include <stdio.h> int main() { int i,j; int data[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; for (i=0;i<3;i++) { for(j=0;j<4;j++) printf("%2d ",data[i][j]); printf("\n"); } return 0; }

  18. 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 type array_name[row_size][column_size]; • int matrix[3][4]; row[0] row[1] row[2] in memory row0 row1 row2

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

  20. Practice! int i, j, g[3][3]={{0,0,0},{1,1,1},{2,2,2}}; Give value of sum after each statements is executed: 1) sum=0; for (i=0;i<=2; i++) for(j=0;j<=2;j++) sum += g[i][j]; 2) sum=1; for(i=1;i<=2;i++) for(j=0;j<=1;j++) sum *= g[i][j]; 3) sum=0; for(j=0;j<=2;j++) sum -= g[2][j]; 4) sum=0; for(i=0;i<=2;i++) sum += g[i][1];

  21. 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; }

  22. Character Functions • Character I/O Example 1, printf and scanf functions- print and read characters using %c specifier. Example 2, putchar- takes one integer argument, returns an integer value. putchar(‘a’); putchar(‘b’); putchar(‘\n’); putchar(‘c’); same as: putchar(97); putchar(98); putchar(10); putchar(99);

  23. getchar function-reads character from the keyboard and returns the integer value of the character. Example, int c; …. putchar(‘\n’); c=getchar(); putchar(c); putchar(‘\n’);

More Related