Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays."— Presentation transcript:

1 © Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays

2 © Janice Regan, CMPT 102, Sept. 2006 1 Simple and Composite Variables  We have studied simple variables  A simple variable describes a single value  A simple variable has an identifier  A simple variable has a type that describes the properties of the value of the variable, the permissible operations for the variable, and the representation of the variable in computer memory  We can also have composite variables  These variables describe a group of values  Arrays: all values in the group have the same type  Structures: different values in the group can have different types

3 © Janice Regan, CMPT 102, Sept. 2006 2 Composite Variables  composite variables describe a group of values  1 dimensional arrays or variables of a particular type (all entries must have the same type)  multi dimensional arrays or variables of a particular type (all entries must have the same type)  Structures containing groups of variables of different types  Composite variables can  Used as arguments of functions  Used as types of functions

4 © Janice Regan, CMPT 102, Sept. 2006 3 One-Dimensional (1-D) 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

5 © Janice Regan, CMPT 102, Sept. 2006 4 Declaration of a 1-D array  An array is defined using a declaration statement. 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

6 © Janice Regan, CMPT 102, Sept. 2006 5 Example Array Declaration in C int list[10];  allocates memory for 10 integer variables. Ten adjacent locations in memory are allocated  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[2] list[3] list[4] list[5] list[6] list[7] list[9] list[8]

7 © Janice Regan, CMPT 102, Sept. 2006 6 Example Array Declaration in C double v2[6];  allocates memory for 6 double variables. Memory is in a single block  subscript of first element is 0  subscript of last element is 9  C does not perform any bounds checking on arrays v2[0] v2[1] v2[2] v2[3] v2[4] v2[5]

8 © Janice Regan, CMPT 102, Sept. 2006 7 Avoid a common problem (1)  C does not perform any bounds checking on arrays  This means that you can accidentally change the values of other variables by changing a value you refer to as an element of the array, which is not actually part of the array int myArray[5] = {0}; int count = 6; myArray[5] = 3;

9 © Janice Regan, CMPT 102, Sept. 2006 8 Avoid a common problem (2) int myArray[5] = {0}; int count = 6; After these declarations memory looks like myArray[5] = 3; After the assignment statement above myArray[0] 0 myArray[1] 0 myArray[2] 0 myArray[3] 0 myArray[4] 0 count 6 myArray[0] 0 myArray[1] 0 myArray[2] 0 myArray[3] 0 myArray[4] 0 count 3

10 © Janice Regan, CMPT 102, Sept. 2006 9 Avoid a common problem (4)  C does not perform any bounds checking on arrays  By changing an element of the array myArray that is outside the memory area allocated to the array we have changed the value of a completely different variable  It is imperative that you be very careful to avoid using elements of the array that are not within the declared array.

11 © Janice Regan, CMPT 102, Sept. 2006 10 Initializing 1-D Arrays  Arrays can be initialized at the time they are declared.  You can specify individual values for each element of an array /* put 0.15 into element 0, 0.25 into element 1*/ /* and 0.3 into element 3 of the array taxrate */ double taxrate[3] ={0.15, 0.25, 0.3}; /* put one character in each element of the array*/ char list[5] = {‘h’,’e’,’l’,’l’,’o’};

12 © Janice Regan, CMPT 102, Sept. 2006 11 Initializing 1-D Arrays  Declare your array without specifying the size  The length of the array will be determined by the number of elements you initialize /*the size of array s is 3 */ /* because 3 elements are initialized*/ int s[ ] = {5,0,-5}; l

13 © Janice Regan, CMPT 102, Sept. 2006 12 Initializing 1-D Arrays  You can specify individual values for the first n elements of an array.  In this case the remaining unspecified elements will be 0 /*values in array taxrate will be */ /* {0.15, 0.25, 0.3, 0.0, 0.0, 0.0} */ double taxrate[6] ={0.15, 0.25, 0.3};

14 © Janice Regan, CMPT 102, Sept. 2006 13 Initializing 1-D Arrays  You can specify that all elements of an array should be 0 /*assigns zero to all 100 elements*/ /*first element is initialized to 0.0 */ /*remaining elements are initialized to 0.0*/ double vector[100] = {0.0};

15 © Janice Regan, CMPT 102, Sept. 2006 14 Initializing 1-D Arrays  Avoid a common error  You cannot specify that all elements of an array should be 1.0 (or any other non-zero value) in a declaration statement /*first element is initialized to 1.0 */ /*remaining elements are initialized to 0.0*/ double vector[100] = {1.0};

16 © Janice Regan, CMPT 102, Sept. 2006 15 Initializing a 1-D array  You can initialize a simple variable either in the declaration statement, or using separate assignment statements following the declaration statements.  You can also initialize the values of the elements of an array following using assignments statements following the declaration statements  A for loop (a counting loop since we know how many elements need initializing) is the most natural way to initialize or otherwise manipulate an array

17 © Janice Regan, CMPT 102, Sept. 2006 16 Initializing or using 1-D arrays int list[10]; int i; /*Initialize each element of array list to 1 */ for(i=0; i<10; i++) { list[i] = 1; }

18 © Janice Regan, CMPT 102, Sept. 2006 17 Using a 1-D array for loops are often used to assign values to the elements of an array int list[10], i; for(i=0; i<10; i++) { list[i] = i * i; }

19 © Janice Regan, CMPT 102, Sept. 2006 18 Putting data into a 1-D Array  Another common way of assigning values to the elements of an array is to read data values from a file directly into the array  Each value read from the file is assigned to a single array element (for example motion[6])  A single value stored in location i in the array time is referred to as time[i]  Note that checks to determine the file was opened correctly and that data was read correctly have been omitted from the example, they should not be omitted from your code

20 © Janice Regan, CMPT 102, Sept. 2006 19 Array Input from a data file int k; double time[10]; double motion[10]; FILE *sensor3; sensor3 = fopen(“sensor3.dat”, “r”); for(k=0; k<10; k++) { fscanf(sensor3, “%lf %lf”,&time[k], &motion[k]); }

21 © Janice Regan, CMPT 102, Sept. 2006 20 Arrays as function parameters  Arrays, or parts of arrays, can be passed as arguments to functions.  An element of an array can be used as a simple variable parameter It can be passed by value or by reference  An entire array can be used as a parameter of a function It can only be passed by reference

22 © Janice Regan, CMPT 102, Sept. 2006 21 1-D Array elements as parameters  Individual elements of an array can be passed by value as arguments that are of simple variable types.In the illustrated function call  parameter par1 of function donothing has the value of element 2 of the array (array[2])  parameter par2 of function donothing has the value of element 4 of the array (array[4]) void donothing( int par1, int par2 ); int main(void) { int array[5] = {1,2,3,4,5}; donothing(array[2], array[4]); }

23 © Janice Regan, CMPT 102, Sept. 2006 22 1-D Array elements as parameters  Individual elements of an array can be passed by reference as arguments that are of simple pointer variable types. In the illustrated function call  parameter par1p of function donothing has the value of the address of element 2 of the array (&array[2])  parameter par2p of function donothing has the value of the address of element 4 of the array (&array[4]) void donothing( double *par1p, double *par2p ); int main(void) { double array[5] = {1,2,3,4,5}; donothing( &array[2], &array[4] ); }

24 © Janice Regan, CMPT 102, Sept. 2006 23 1-D arrays as parameters  Arrays are always passed to a function by reference  The array name is the address of the first element of the array  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

25 © Janice Regan, CMPT 102, Sept. 2006 24 Function with array argument double max( double array[ ], int actual_size ) { double max; int index; max = 0; for(index = 0; index < actual_size; index++) { if(max < array[index]) { max = array[index]; } return max; }

26 © Janice Regan, CMPT 102, Sept. 2006 25 Using the function double max( double array[ ], int actual_size ); int main(void) { FILE *exp1p; double x[100]; int count=0; exp1p = fopen("inputfile.dat", "r"); while( (fscanf(exp1p, "%lf", &x[count]) == 1 ) && count < 100) { count++; } printf("Maximum value: %f \n", max(x, count)); fclose(exp1p); return 0; }


Download ppt "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays."

Similar presentations


Ads by Google