Top Banner
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.
46

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.

Mar 29, 2015

Download

Documents

Skyler Hassett
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

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.

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

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.

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

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.

Page 4: 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.

Examples

factorial_func.cgetchar.c

Page 5: 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.

Buggy examples

pi_bad.cexp_bad.c

Page 6: 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.

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

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

Sowing the seed of love The numbers returns by rand are

determined by the random-number generator’s ‘seed’

To change the seed, use -void srand(unsigned int seed);

(defined in stdlib.h) To truly randomize things, call –

srand(time(NULL));(and don’t forget to #include <time.h>)

Page 8: 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.

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

Page 9: 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.

Simulating an event with probability p

(double)rand()/RAND_MAX < p First scale rand’s return value to

between 0 and 1 If result is smaller than p (between

0 and p), the event has occurred Otherwise, it hasn’t

0 1p

Page 10: 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.

Simulating a die throw

Problem – implement the following function – int RandInRange(int min, int max);

That returns a random number between min and max

Page 11: 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.

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

Page 12: 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.

Full Solution

/* Returns a random integer between bottom and top */

int RandInRange(int bottom, int top){

return bottom + rand()%(top-bottom+1);}

Page 13: 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.

Example

die_throws.c

Page 14: 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.

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

Page 15: 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.

Solution

guess_work.c

Page 16: 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.

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

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

Solution

nuclear_decay.c Note – it might be more interesting

calculating the average time until full decay for different N’s and P’s by repeating the same experiment a large number of times.

Page 18: 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.

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

Page 19: 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.

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

……

Page 20: 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.

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]);}

Page 21: 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.

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

Page 22: 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.

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]);}

Page 23: 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.

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

Page 24: 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.

Example

Sort.c

Page 25: 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.

Sort – step by step

#include <stdio.h>

#define SIZE 3

int main(void){

int A[SIZE] = {4,8,2}; int min_index;int i,j,tmp;

4 8 2

A[0] A[1] A[2]

min_index

----

---- ---- ----

i j tmp

Page 26: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

----

0 ---- ----

i j tmp

Page 27: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

0

0 ---- ----

i j tmp

Page 28: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

0

0 1 ----

i j tmp

Page 29: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

0

0 1 ----

i j tmp

Page 30: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

0

0 2 ----

i j tmp

Page 31: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

2

0 2 ----

i j tmp

Page 32: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

2

0 3 ----

i j tmp

Page 33: 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.

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;

}

4 8 2

A[0] A[1] A[2]

min_index

2

0 3 4

i j tmp

Page 34: 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.

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;

}

2 8 2

A[0] A[1] A[2]

min_index

2

0 3 4

i j tmp

Page 35: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

2

0 3 4

i j tmp

Page 36: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

2

1 3 4

i j tmp

Page 37: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

1

1 3 4

i j tmp

Page 38: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

1

1 2 4

i j tmp

Page 39: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

2

1 2 4

i j tmp

Page 40: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

2

1 3 4

i j tmp

Page 41: 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.

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;

}

2 8 4

A[0] A[1] A[2]

min_index

2

1 3 8

i j tmp

Page 42: 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.

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;

}

2 4 4

A[0] A[1] A[2]

min_index

2

1 3 8

i j tmp

Page 43: 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.

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;

}

2 4 8

A[0] A[1] A[2]

min_index

2

1 3 8

i j tmp

Page 44: 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.

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;

}

2 4 8

A[0] A[1] A[2]

min_index

2

2 3 8

i j tmp

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

ExerciseWrite 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 - 1e – 1h – 1l – 2o – 2w - 1

Assume that the input is all in lower-case.

Page 46: 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.

Solution

letter_count.c