1 More on Arrays
2 Review: Passing Arrays to Functions Passing an array: what goes on in the memory? Why isn’t it the same as when passing primitives? What about passing an element from an array (f(arr[i]) is the same as f(x)!2)
3 Efficient Factorial Write a program that repeatedly receives a natural number as its input and returns its factorial The program should be efficient: Do not calculate 1*2*…*n for every n from the beginning on every input Do not calculate values that were not requested
4 Solution: Main Idea We can keep in an array the results When given a new n If calculated before – return it Otherwise, get a better start
5 Solution (efficient_factorial.c) #include #define MAX_N 10 int factorial (int array[], int pos, int n); int main() { int n, position = 0; int fact[MAX_N] = {0}; fact[0] = 1; while(1) { printf("Enter n\n"); scanf("%d",&n); if (n < 0) {break;} if (n >= MAX_N) {printf("too large!"); continue;} printf("fact(%d) = %d\n",n,factorial(fact,position,n)); if (n > position) position = n; }
6 Solution (Cont.) int factorial (int fact[], int pos, int n) { int i; if (pos < n) { for(i = pos + 1; i <= n; ++i) { fact[i] = fact[i-1] *i; } return fact[n]; }
7 Exercise Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise The arrays are of the same size Write a program that accepts two arrays of integers from the user and checks for equality
8 Solution (compare_arrays.c) int compare_arrays(int arr1[], int arr2[], int size) { int i = 0; /* compare the elements one at a time */ for (i = 0; i < size; ++i) { if (arr1[i] != arr2[i]) return 0; } /* if we got here, both arrays are identical */ return 1; }
9 Multi-dimensional arrays Array of arrays: int A[2][3] = { {1, 2, 3}, {4, 5, 6} }; Means an array of 2 integer arrays, each of length 3. Access: j-th element of the i-array is A[i][j]
10 Multi-dimensional arrays The size of the array can be determined by the compiler (not recommended): int B[][2] = {{1,2}, {2,3}, {3,4}}; Cannot skip this!!
11 Example: matrix addition #include #define SIZE 3 int main() { int A[SIZE][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}}; int B[SIZE][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}}; int C[SIZE][SIZE]; int i = 0, j = 0; for (i = 0; i < SIZE; ++i) for (j = 0; j < SIZE; ++j) C[i][j] = A[i][j] + B[i][j]; return 0; }
12 2D arrays as function arguments void print_matrix(int mat[3][3]) { int i, j; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) printf("\t%d ", mat[i][j]); printf("\n"); } The second subscript must be specified and it must be constant
13 Exercise Implement a function that accepts two integer matrices and returns 1 if they are equal, 0 otherwise The matrices are of the same size Write a program that defines and initialize 2 matrices of size 3x3 with integers You can use compare_arrays as your starting point
14 Solution (compare_matrices.c) int compare_matrices(int mat1[][SIZE], int mat2[][SIZE], int rows) { int i,j; for(i=0; i<rows; i++) for (j=0; j < SIZE; j++) if (mat1[i][j]!=mat2[i][j]) return 0; /* If we got here, both matrices are identical */ return 1; }
15 home Write a program that defines 3 matrices A,B,C of size 3x3 with float elements; initialize the first two matrices (A and B) Compute the matrix multiplication of A and B and store it in C (i.e. C = A*B) Matrix Multiplication: Print all the matrices on the screen
16 Solution mat_mul.c
17 The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs we meant to write! The debugger can be used to follow the program step by step and may help detecting bugs in an already compiled program.
18 The debugger’s common features Setting breakpoints (a point where the execution stops): bring the cursor to desired line and press the palm icon or F9. A dark red dot appears near the line. Executing a debugged run: Build->start debug->go or F5. The program will run and stop at the first breakpoint.
19 The debugger’s common features (cont.) Stopping at a specific line: Bringing the cursor to the line and press ctrl+F10, or Build->start debug->go to cursor. The program will stop at that point. Stepping to the next line – F10. Entering a function – F11. Seeing variable values – quickwatch and/or debug window at the bottom. The yellow arrow indicates our whereabouts at any given moment.
20 Examples factorial_func.c getchar.c
21 Buggy example pi_bad.c
22 More time? Talk about the loops-related questions from 2 weeks ago