non-negative integer that corresponds to the position of an array element Indexes are numbered from 0, 1, 2, 3, 4, . . . N-1, where N is the size of the array An array element is the value that is stored in an array at a specific index
group of boxes stacked on top of each other array1[4] array2[4] array1[3] array2[3] array1[2] array2[2] array1[1] array2[1] array1[0] array2[0] 40 55.0 30 44.0 20 33.0 10 22.0 0 11.0
& <time.h> libraries Function time(NULL) gives time in seconds since January 1, 1970 Function srand( ) will seed (start off) the pseudorandom numbers srand(time(NULL));
rolling one dice 1000 times The array replaces the switch statement used in program random.c, so the code is much shorter! for(i=1;i<=MAX;i++){ side = 1 + rand() % 6; array[side]++; }
functions by reference, so the function can modify the element values in the original array The name of the array is actually the address of the first element of the array, so the function’s parameter contains a copy of the address of the array
variables, the individual elements of an array are pass by value The function’s parameter contains a copy of the value The size of the array is also pass by value
square brackets for declaring array parameters, but NOT in function call Function prototype: returnType functionName(datatype[]); Function call: functionName(array); Function definition: returnType functionName(datatype array[]); [ ]
is pass by reference, so elements are changed! Individual array elements are pass by value, so no change for single element for(i = 0; i < SIZE; i++) { printf(“%d ”,array[i]);//1 4 9 16 25 }
to be able to sort massive amounts of data Bubble sort Larger values “bubble” to the top of the array Smaller values “bubble” to the bottom of the array Think of a vertical array
the array several times With each pass, compare adjacent elements If the pair is in increasing order (or identical), don’t swap If the pair is in decreasing order, then swap
program arrayfunctions.c: has several array functions arrayfunctions.h: header file with function prototypes makefile-bubble: makefile to compile these files % make –f makefile-bubble % ./program
high-speed binary search can be used: Locate the middle element of the array and compare it to the search key If equal, then the search key is found and the array subscript is returned If not equal, then the upper half or lower half of the array is searched
example, an array of 1024 elements takes at most only 10 comparisons, with a Big-O of O(log n) Searching 1024 elements reduces to 512, 256, 64, 32, 16, 8, 4, 2, and 1 1,048,576 elements takes only 20 comparisons 4,294,967,295 takes only 32 comparisons
program arrayfunctions.c: has several array functions arrayfunctions.h: header file with function prototypes makefile-search: makefile to compile these files getdouble.c and getdouble.h: number input % make –f makefile-search
the 2nd subscript and higher void printarray(int array[][COLUMNS], const int rows, const int columns){ int i=0,j=0; for(i=0;i<rows;i++){ for(j=0;j<columns;j++){ printf(“%5d ”,array[i][j]); } printf(“\n”); } }