1 / 27

CMPT 128: Introduction to Computing Science for Engineering Students

CMPT 128: Introduction to Computing Science for Engineering Students. Introduction to Arrays. Simple and Composite Variables. We have studied simple variables A simple variable describes a single value A simple variable has an identifier

finn
Download Presentation

CMPT 128: Introduction to Computing Science for Engineering Students

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. CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays

  2. 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. 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 that may be of different types • Composite variables can • Used as arguments of functions • Used as return values of functions

  4. 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. 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. Example Array Declaration in C++ list[0] 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[1] list[2] list[3] list[4] list[5] list[6] list[7] list[8] list[9]

  7. 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 5 • C++ does not perform any bounds checking on arrays v2[0] v2[1] v2[2] v2[3] v2[4] v2[5]

  8. 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. 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[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 myArray[0] 0

  10. Avoid a common problem (3) • 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. 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 2 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. 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. 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 zero (or equivalent) // 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. 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 zero (or equivalent) // values in array name will be // {‘J’, ‘a’, ‘n’, ‘i’, ‘c’, ‘e’, ‘\0’, ‘\0’, ‘\0’, ‘\0’} // Two ways to initialize char name[10] ={‘J’, ‘a’, ‘n’, ‘i’, ‘c’, ‘e’}; OR char name[10] = “Janice”;

  15. 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. double vector[100] = {0.0};

  16. 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, by declaring only the first value // first element is initialized to 1.0 // remaining elements are initialized to 0.0 double vector[100] = {1.0};

  17. 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 using separate assignment statements following the declaration of the array • 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

  18. Initializing or using 1-D arrays int myinteger[100]; int index; // Initialize each element of array myinteger to 1 for(index=0; index<100; index++) { myinteger[index] = 1; }

  19. Using a 1-D array for loops are often used to assign values to the elements of an array const int TEN =10.0; double mydouble[TEN] = {0.0}; int index; for(index=0; index<TEN; index++) { list[index] += sqrt( index) * TEN; }

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

  21. Array Input from a data file • int k; • double time[10]; • double motion[10]; • ifstreamsensor3Stream; • sensor3Stream.open(“sensor3.txt”); • for(k=0; k<10; k++) • { • sensor3Stream >> time[k] >> motion[k]; • }

  22. Array Input from a data file • int k; • double time[10]; • double motion[10]; • FILE *sensor3FilePointer; • sensor3FilePointer = fopen(“sensor3.txt”); • for(k=0; k<10; k++) • { • fscanf ( sensor3FilePointer, %lf %lf“, • time[k], motion[k] ); • }

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

  24. 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 • Formal parameter par1 of function donothing has the value of element 2 of the array (array[2]) • Formal 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]); return 0; }

  25. 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 actualnumber of array elements that are used will vary, so the actualsize of the array is usually passed as another argument

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

  27. Using the function double max( double array[ ], int actual_size ); int main(void) { ifstream exp1; double x[100]; int count=0; exp1.open("inputfile.dat"); while( exp1 >> x[count] && count < 100) { count++; } cout << "Maximum value: “ << max(x, count)<< endl; exp1.close(); return 0; }

More Related