220 likes | 391 Views
Final Exam. Friday, August 15 th from 1:30pm – 4:00pm You can bring one 3x5 cheat card filled out on both sides which you will turn in with your test There are 7 questions. You can try all of them but circle the numbers of the 5 you want graded for a total of 100 points.
E N D
Final Exam • Friday, August 15th from 1:30pm – 4:00pm • You can bring one 3x5 cheat card filled out on both sides which you will turn in with your test • There are 7 questions. You can try all of them but circle the numbers of the 5 you want graded for a total of 100 points. • Covers chapters 1-6 of the textbook; however, emphasis is on arrays and pointers
Definition and Initialization • An array is defined using a declaration statement. datatypearray_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
Example int list[5]; • allocates memory for 5 integer variables • subscript of first element is 0 • subscript of last element is 4 • C does not perform any bounds checking on arrays list[0] list[1] list[2] list[3] list[4]
Initializing Arrays • 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 */
Assigning values to an array for loops are often used to assign values to an array Example: int list[5], i; for(i=0; i<5; i++) { list[i] = i; } 0 list[0] 1 list[1] 2 list[2] 3 list[3] 4 list[4]
Exercise • int data[100] • Store random numbers [0,99) in data for (i=0; i<100; i++) data[i] = rand() % 100;
Function Arguments • Individual elements of an array can be passed as regular arguments. void donothing(int a, int b) { … } int main(void) { /* Declare variables and functions */ int array[5] = {1,2,3,4,5}; donothing(array[2], array[4]); . . }
Passing Arrays to Functions • Arrays are always pass by reference • Modifications to the array are reflected to main program • The array name is the address of the first element • 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 to the function
Exercise • int data[100] • Write a function to find maximum value in the array data int maximum(int fdata[], int n) { int i, fmax; fmax = fdata[0]; for (i=0; i<n; i++) if (fdata[i] > fmax) fmax = fdata[i]; return(fmax); } int main() { int data[100],i, max; for (i=0; i<100; i++) data[i] = rand() % 100; max = maximum(data,100); printf("Max = %d\n",max); return(0); }
Exercise • What is the output of the following program? void print(int pdata[], int n) { int i; for (i=0; i<n; i++) printf("data[%d]=%d\n",i,pdata[i]); return; } void modify(int fdata[], int n) { int i; for (i=0; i<n; i++) fdata[i] = 1; return; } int main() { int data[10]; for (i=0; i<10; i++) data[i] = rand() % 100; print(data,10); modify(data,10); print(data,10); return(0); }
Addresses and Pointers name address Memory - content 0 1 2 3 4 5 6 … x1 int x1=1, x2=7; double distance; x2 distance
6.1 Addresses and Pointers name address memory int a, b; int *c, *d; a = 5; c = &a; d = &b; *d = 9; print c, *c, &c print a, b a 5 9 b c 1 d 2 c=1 *c=5 &c=4 a=5 b=9
Exercise void function3(int a, int b) { int temp; temp = a; a = b; b = temp; printf("A = %d, B = %d\n",a,b); return; } 1 2 a b temp Function 3 variables
Exercise int x = 1, y = 2; printf("X = %d, Y = %d\n",x,y); function3(x,y); printf("X = %d, Y = %d\n",x,y); 1 2 x y
Exercise void function4(int *pa, int *pb) { int temp; temp = *pa; *pa = *pb; *pb = temp; printf("PA = %d, PB = %d\n",*pa,*pb); return; } 1 2 pa pb temp Function 4 variables
Exercise int x = 1, y = 2; int *px = &x, *py = &y; x = 1; y = 2; printf("PX = %d, PY = %d\n",*px,*py); function4(px,py); printf("PX = %d, PY = %d\n",*px,*py); 1 2 x y px py
Exercise void function1(int a, int *pb) { int temp; temp = a; a = *pb; *pb = temp; printf("A = %d, PB = %d\n",a,*pb); return; } 2 1 a pb temp Function 1 variables
Exercise int x = 1, y = 2; int *px = &x, *py = &y; printf("X = %d, PY = %d\n",x,*py); function1(x,py); printf("X = %d, PY = %d\n",x,*py); 1 2 x y px py
Exercise void function2(int *pa, int b) { int temp; temp = *pa; *pa = b; b = temp; printf("PA = %d, B = %d\n",*pa,b); return; } 1 2 pa b temp Function 2 variables
Exercise int x = 1, y = 2; int *px = &x, *py = &y; x = 1; y = 2; printf("PX = %d, Y = %d\n",*px,y); function2(px,y); printf("PX = %d, PY = %d\n",*px,y); 1 2 x y px py