CSCE150A Computer Science & Engineering 150A Introduction Problem Solving Using Computers Declaring, Referencing Lecture 06 - Arrays Initialization Function Args Constants Returning Stephen Scott Arrays Adapted from Christopher M. Bourke Searching & Sorting Multi- dimensional Arrays Common Errors Fall 2009 Exercises 1 / 30
Chapter 8 CSCE150A 8.1 Declaring and Referencing Arrays Introduction 8.2 Array Subscripts Declaring, Referencing 8.3 Using For Loops for Sequential Access Initialization Function Args 8.4 Using Array Elements as Function Arguments Constants 8.5 Array Arguments Returning Arrays 8.6 Searching and Sorting an Array Searching & 8.7 Multidimensional Arrays Sorting Multi- 8.9 Common Programming Errors dimensional Arrays Common Errors Exercises 2 / 30
Introduction CSCE150A Introduction Simple data types use a single memory cell to store a variable Declaring, Referencing Collections of data should be logically grouped Initialization Example: 75 students in the class; should we declare 75 separate Function Args Constants variables to hold grades? Returning Grouping related data items together into a single composite data Arrays structure is done using an array Searching & Sorting Multi- dimensional Arrays Common Errors Exercises 3 / 30
Declaring Arrays I CSCE150A Introduction Declaring, Referencing An array is a collection of two or more adjacent memory cells, called Initialization array elements Function Args Constants All elements in an array are associated with a single variable name Returning Each element is individually accessed using indices Arrays Searching & Sorting Multi- dimensional Arrays Common Errors Exercises 4 / 30
Declaring Arrays II CSCE150A To set up an array in memory, we declare both the name of the array and the number of cells associated with it: Introduction double my_first_array[8]; Declaring, Referencing int students[10]; Initialization The first one instructs C to associate 8 memory cells of type double Function Args with the name my_first_array Constants The second one instructs C to associate 10 memory cells of type int Returning with the name students Arrays Searching & In all cases, the memory cells will be adjacent to each other in Sorting memory Multi- dimensional Arrays Common Errors Exercises 5 / 30
Referencing Array Elements I CSCE150A Introduction Declaring, To process the data stored in an array, each individual element is Referencing associated to a reference value Initialization Function Args By specifying the array name and identifying the element desired, we Constants can access a particular value Returning Arrays The subscripted variable x[0] (read as x sub zero) references the Searching & first element Sorting Multi- dimensional Arrays Common Errors Exercises 6 / 30
Referencing Array Elements II CSCE150A Other elements can be accessed similarly: x[1], x[2], ... Introduction myArray[0] = 8; Declaring, printf("value of second element=%d",myArray[1]); Referencing scanf("input a number: %d",&anotherArray[9]); Initialization Function Args For an array of size n , we index 0 , 1 , . . . , n − 1 Constants Think of index as an offset from base address Returning Arrays An array size must be an integer (no such thing as half an element) Searching & Similarly, each index is also an integer Sorting Multi- dimensional Arrays Common Errors Exercises 7 / 30
Referencing Array Elements I Pitfall CSCE150A Take care that you do not reference an index outside the array: Introduction 1 double grades [75]; Declaring, 2 ... Referencing 3 printf("75th grade is %f\n", grades [74]); Initialization 4 printf("76th grade is %f\n", grades [75]); ← Illegal Function Args 5 printf(" -1th grade is %f\n", grades [ -1]); ← Illegal Constants 6 Returning 7 int i; Arrays 8 for(i=0; i <76; i++) Searching & 9 printf("%d-th grade is %f\n", (i+1), grades[i]); Sorting 10 ↑ Illegal on last iteration Multi- dimensional Arrays Common Errors Exercises 8 / 30
Array Initialization CSCE150A You can declare multiple arrays along with regular variables: Introduction double cactus[5], needle, pins[7]; Declaring, Referencing We can initialize a simple variable when we declare it: Initialization int sum = 0; Function Args Same with arrays: Constants Returning 1 int array[SIZE ]; Arrays 2 for(i=0; i < SIZE; i++) Searching & Sorting 3 array[i] = 0; Multi- dimensional Arrays Common Errors Exercises 9 / 30
Array Declaration & Initialization CSCE150A Introduction We can declare and initialize an array Declaring, Referencing If we initialize when we declare, we can omit the size Initialization Function Args 1 int primeNumbersLessThanHundred [] = { Constants 2 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, Returning 3 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, Arrays 4 89, 97 }; Searching & Sorting Multi- dimensional Arrays Common Errors Exercises 10 / 30
Using for Loops for Sequential Access CSCE150A Elements of an array are processed in sequence, starting with element zero . Introduction Declaring, This processing can be done easily using an indexed for loop: a Referencing counting loop whose loop control variable runs from zero to one less Initialization than the array size. Function Args Using the loop counter as an array index (subscript) gives access to Constants Returning each array element in turn. Arrays Searching & 1 for(i=0; i < SIZE; i++) { Sorting Multi- 2 printf("%d ",array[i]); dimensional Arrays 3 } Common Errors Exercises 11 / 30
Using Array Elements as Function Arguments CSCE150A Introduction You can use scanf with array elements just like with regular variables Declaring, Referencing 1 int x[10]; Initialization 2 Function Args int i = 0; Constants 3 scanf("%d", &x[i]); Returning 4 printf("Hey , I read %d\n", x[i]); Arrays Searching & Sorting Multi- dimensional Arrays Common Errors Exercises 12 / 30
Arrays as Arguments CSCE150A You can also use entire arrays as function arguments Introduction Passing arrays as arguments to a function means: Declaring, The function can access any value in the array Referencing The function can change any value in the array Initialization Function Args Syntax: specify an array as a parameter by using the square brackets: Constants int sum(int array[], int size); Returning Arrays Note: what is actually being passed is a pointer to the first element Searching & of the array! Sorting We could equivalently define: Multi- dimensional int sum(int *array, int size); Arrays Common Errors Exercises 13 / 30
Full Example CSCE150A 1 #include <stdio.h> 2 3 int sum(int array [], int size ); Introduction 4 5 int main(void) Declaring, 6 { Referencing 7 int foo [] = {1,2,3,4,5,6,7,8,9,10}, i; Initialization 8 printf("sum of all array elements is %d\n",sum(foo , 10)); 9 return 0; Function Args 10 } 11 Constants 12 int sum(int a[], int size) 13 { Returning 14 int i, summation = 0; Arrays 15 for(i=0; i<size; i++) 16 { Searching & 17 summation += a[i]; Sorting 18 } Multi- 19 return summation ; dimensional 20 } Arrays Common Errors Exercises 14 / 30
Formal Array Parameter CSCE150A It was necessary to pass an additional variable size to sum Introduction An array does not have an explicit size associated with it Declaring, Referencing C does not allocate space in memory for arrays; the operating system Initialization does this at runtime Function Args Constants As programmers, we are responsible for: Returning Memory management Arrays For keeping track of the size of an array Searching & For ensuring that we do not access memory outside the array Sorting Multi- If a function accesses an array, it needs to be told how big it is dimensional Arrays Common Errors Exercises 15 / 30
Arrays as Input Arguments CSCE150A Since arrays are passed by reference (like scanf ), functions can Introduction modify their values Declaring, Sometimes, we would like to pass arrays as arguments, but do not Referencing Initialization want to change their values. Function Args We can do this by using the const quantifier in the function Constants declaration: int sum(const int foo[], int size) ... Returning Arrays Specifies to the compiler that the array is to be used only as an input Searching & The function does not intend to modify the array Sorting Multi- The compiler enforces this: any attempt to change an array element dimensional Arrays in the function as an error Common Errors Exercises 16 / 30
Returning an Array Result CSCE150A C only allows us to return a single item Introduction It is not possible to return an array (a collection of items) Declaring, Referencing We can, however, return a pointer to an array Initialization We cannot return a pointer to a local array (dangerous; undefined Function Args behavior) Constants Returning Requires knowledge of dynamic memory and malloc Arrays More later this semester; for now: simply declare an array large Searching & enough for your purposes Sorting Multi- Might need two controlling variables: one for max array size and one dimensional Arrays for number of cells used Common Errors Exercises 17 / 30
Recommend
More recommend