Download presentation

Presentation is loading. Please wait.

Published byAustin Leblanc Modified over 3 years ago

1
Etter/Ingber Arrays and Matrices

2
Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of the same data type 4 An element of an array is accessed using the array name and an index or subscript 4 The name of the array is the address of the first element and the subscript is the offset 4 In C, the subscripts always start with 0 and increment by 1

3
Etter/Ingber Definition and Initialization 4 An array is defined using a declaration statement. data type array_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
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]

5
Etter/Ingber Initializing Arrays 4 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
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; }

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

8
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];

9
Etter/Ingber Function Arguments 4 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]);.

10
Etter/Ingber Passing Entire Arrays as Arguments to Functions 4 Arrays are always pass by reference 4 The array name is the address of the first element 4 The maximum size of the array must be specified at the time the array is declared. The actual number of array elements that are used will vary, so the actual size of the array is usually passed as another argument to the function

11
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

12
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

13
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);

14
Etter/Ingber Matrices 4 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 row0row1row2

15
Etter/Ingber Accessing Array Elements 4 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

16
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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google