1 / 44

The debugger

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!

shania
Download Presentation

The debugger

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

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

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

  4. Examples factorial_func.c getchar.c

  5. Buggy examples pi_bad.c exp_bad.c

  6. Arrays • A block of many variables of the same type • Array can be declared for any type • E.g. int A[10] is an array of 10 integers. • Examples: • list of students’ marks • series of numbers entered by user • vectors • matrices

  7. Arrays in Memory • Sequence of variables of specified type • The array variable itself holds the address in memory of beginning of sequence • Example: double S[10]; • The k-th element of array A is specified by A[k-1] (0 based) … 0 1 2 3 4 5 6 7 8 9 … S

  8. Example - reverse #include <stdio.h> int main(void) { int i, A[10]; printf("please enter 10 numbers:\n"); for(i=0; i<10; i++) scanf("%d",&A[i]); printf("numbers in reversed order:\n"); for(i=9; i>=0; i--) printf("%d\n",A[i]); }

  9. Define • Magic Numbers (like 10 in the last example) in the program convey little information to the reader • Hard to change in a systematic way • #define defines a symbolic name • During preprocessing phase, symbolic names are replaced by the replacement text

  10. Reverse with #define /* get 10 integers from the user and printing them in reversed order*/ #include <stdio.h> #define NUM 10 int main(void) { int i; int A[NUM]; printf(“Please enter %d numbers:\n",NUM); for(i=0; i<NUM; i++) scanf("%d",&A[i]); printf("numbers in reversed order:\n"); for(i=NUM-1; i>=0; i--) printf("%d\n",A[i]); }

  11. Initialization • Like in the case of regular variables, we can initialize the array during declaration. • the number of initializers cannot be more than the number of elements in the array • but it can be less • in which case, the remaining elements are initialized to 0 • if you like, the array size can be inferred from the number of initializers • by leaving the square brackets empty • so these are identical declarations : int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};

  12. Example Sort.c

  13. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index ---- i j tmp #include <stdio.h> #define SIZE 3 int main(void) { int A[SIZE] = {4,8,2}; int min_index; int i,j,tmp; ---- ---- ----

  14. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index ---- i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 ---- ----

  15. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 ---- ----

  16. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 1 ----

  17. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 1 ----

  18. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 2 ----

  19. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 2 ----

  20. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 ----

  21. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 4

  22. A[0] A[1] A[2] Sort – step by step 2 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 4

  23. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 4

  24. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

  25. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 1 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

  26. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 1 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 2 4

  27. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 2 4

  28. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

  29. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

  30. A[0] A[1] A[2] Sort – step by step 2 4 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

  31. A[0] A[1] A[2] Sort – step by step 2 4 8 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

  32. A[0] A[1] A[2] Sort – step by step 2 4 8 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=(A[min_index]>A[j] ? j : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 2 3 8

  33. Exercise Write a program that gets an input line from the user (ends with ‘\n’) and displays the number of times each letter appears in it. Example: For the input line - hello, world! The output should be: d - 1 e – 1 h – 1 l – 2 o – 2 w - 1 Assume that the input is all in lower-case.

  34. Solution letter_count.c

  35. Passing arrays to functions • Functions can accept arrays as arguments • Usually the array’s size also needs to be passed (why?) • For example - int CalcSum(int arr[], int size); • Within the function, arr is accessed in the usual way • Example – calc_sum.c

  36. Passing arrays to functions • Arrays can be passed to functions and have their values changed from within the function! • Unlike regular variables • This is possible because an array variable is actually an address. • Example – vec_mul.c

  37. Exercise • Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise • Write a program that accepts two arrays of integers from the user and checks for equality • (assume their size is 10)

  38. Solution • compare_arrays.c

  39. Two Dimensional Arrays • We sometimes want to keep an inherent Two-Dimensional structure of data • Example: We can define a two-dimensional 3x3 matrix bydouble A[3][3];

  40. Two 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-th array is A[i][j]

  41. Two Dimensional Arrays • When passing a two-dimensional array to a function, it is necessary to indicate the size of the second dimension in the function header • But this does not mean the array’s size needn’t be passed as well • Same is true when initializing the array int func(int[][4] arr);double B[][2] = {{1,2}, {2,3}, {3,4}};

  42. Example mat_add.c

  43. Exercise • 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

  44. Solution mat_mul.c

More Related