Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 debuggers 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 debuggers 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 Random numbers Some applications require using random numbers Computer games - Card games, raffles, adventure and strategy games etc. Some cryptography stuff… int rand(void); Declared in stdlib.h Returns a pseudo-random number from 0 to RAND_MAX, which is at least 32,767

7 Sowing the seed of love The numbers returns by rand are determined by the random-number generators seed To change the seed, use - void srand(unsigned int seed); (defined in stdlib.h) To truly randomize things, call – srand(time(NULL)); (and dont forget to #include )

8 Telling time time_t time(NULL); time_t is an arithmetic type for storing time periods Essentially an int The function returns the number of seconds elapsed since 00:00:00 GMT, Jan 1, 1970 The parameter should always be NULL, until we learn about pointers

9 Simulating an event with probability p (double)rand()/RAND_MAX < p First scale rands return value to between 0 and 1 If result is smaller than p (between 0 and p), the event has occurred Otherwise, it hasnt 01 p

10 Simulating a die throw Problem – implement the following function – int RandInRange(int min, int max); That returns a random number between min and max

11 First step Implement a function that returns a random value between 0 and N-1 for some N int BoundedRand(int N) { return rand()%N; }

12 Full Solution /* Returns a random integer between bottom and top */ int RandInRange(int bottom, int top) { return bottom + rand()%(top-bottom+1); }

13 Example die_throws.c

14 Exercise Write a program that generates a number at random and waits for the user to guess the right number At each guess, the program informs the user if the real number is greater or smaller than the number she has chosen The program should terminate when the user guesses correctly or enters -1

15 Solution guess_work.c

16 Exercise Write a program that simulates a nuclear decay experiment Input – N – the number of nuclei P – the probability of decay per time step Output – The number of time steps until all nuclei have decayed Note – simulating the number of steps should take place in a separate function

17 Solution nuclear_decay.c Note – it might be more interesting calculating the average time until full decay for different Ns and Ps by repeating the same experiment a large number of times.

18 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

19 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) 0123456789 S ……

20 Example - reverse #include 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]); }

21 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

22 Reverse with #define /* get 10 integers from the user and printing them in reversed order*/ #include #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]); }

23 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};

24 Example Sort.c

25 Sort – step by step #include #define SIZE 3 int main(void) { int A[SIZE] = {4,8,2}; int min_index; int i,j,tmp; 482 A[0]A[1]A[2] min_index ---- ijtmp

26 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index ---- 0 ijtmp

27 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 0 0---- ijtmp

28 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 0 01---- ijtmp

29 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 0 01---- ijtmp

30 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 0 02---- ijtmp

31 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 2 02---- ijtmp

32 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 2 03---- ijtmp

33 Sort – step by step 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; } 482 A[0]A[1]A[2] min_index 2 034 ijtmp

34 Sort – step by step 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; } 282 A[0]A[1]A[2] min_index 2 034 ijtmp

35 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 2 034 ijtmp

36 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 2 134 ijtmp

37 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 1 134 ijtmp

38 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 1 124 ijtmp

39 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 2 124 ijtmp

40 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 2 134 ijtmp

41 Sort – step by step 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; } 284 A[0]A[1]A[2] min_index 2 138 ijtmp

42 Sort – step by step 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; } 244 A[0]A[1]A[2] min_index 2 138 ijtmp

43 Sort – step by step 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; } 248 A[0]A[1]A[2] min_index 2 138 ijtmp

44 Sort – step by step 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; } 248 A[0]A[1]A[2] min_index 2 238 ijtmp

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

46 Solution letter_count.c


Download ppt "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."

Similar presentations


Ads by Google