the debugger some programs may compile correctly, yet not produce the desirable results. these...
TRANSCRIPT
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.
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.
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.
Examples
factorial_func.cgetchar.c
Buggy examples
pi_bad.cexp_bad.c
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
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>)
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
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
Simulating a die throw
Problem – implement the following function – int RandInRange(int min, int max);
That returns a random number between min and max
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;}
Full Solution
/* Returns a random integer between bottom and top */
int RandInRange(int bottom, int top){
return bottom + rand()%(top-bottom+1);}
Example
die_throws.c
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
Solution
guess_work.c
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
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.
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
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
……
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]);}
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
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]);}
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};
Example
Sort.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
Solution
letter_count.c