Joseph Haugh
University of New Mexico
Which of the following hexadecimal number is the largest?
Which of the following hexadecimal number is the largest?
How many ones appear in the binary representation of the hexadecimal value 0x01239ACF?
Hex | 0 | 1 | 2 | 3 | 9 | A | C | F |
Binary | 0000 | 0001 | 0010 | 0011 | 1001 | 1010 | 1100 | 1111 |
Ones | 0 | 1 | 1 | 2 | 2 | 2 | 2 | 4 |
Total | 14 |
void quicksort(int v[],
int left, int right) {
int i, last;
printArray(v, left, right);
if(left >= right) return;
swap(v, left, (left+right)/2);
last = left;
for (i = left+1; i <= right; i++) {
if(v[i] < v[left]) {
last++;
swap(v, last, i);
printArray(v, left, right);
}
}
swap(v, left, last);
quicksort(v, left, last-1);
quicksort(v, last+1, right);
}
If output from line 4 is [75 62 33 41 24], what is the output the next time line 12 is reached?
void quicksort(int v[],
int left, int right) {
int i, last;
printArray(v, left, right);
if(left >= right) return;
swap(v, left, (left+right)/2);
last = left;
for (i = left+1; i <= right; i++) {
if(v[i] < v[left]) {
last++;
swap(v, last, i);
printArray(v, left, right);
}
}
swap(v, left, last);
quicksort(v, left, last-1);
quicksort(v, last+1, right);
}
If output from line 4 is [75 62 33 41 24], what is the output the next time line 12 is reached?
When you are going to use the debugger, compile your code with the -g option to include debugging information in your executable.
gcc -g -o myprog myprog.c
or with all the flags the standard demands:
gcc -Wall -ansi -pedantic -g -o myprog myprog.c
> gdb myprog
(gdb)
> gdb
(gdb) file myprog
int main() {
char *str = "value";
int i;
str[3] = 'x';
for(i = 0; i < 5; i++) {
printf("%c\n", str[i]);
}
return 0;
}
Program received signal SIGSEGV, Segmentation fault.
main () at str-broken.c:7
7 str[3] = 'x';
int factorial(int n) {
int result = 1;
while(n--) {
result *= n;
}
return result;
}
int main() {
int n = 5;
int fact = factorial(5);
printf("%d! = %d\n", n, fact);
return 0;
}
int fib(int n) {
if(n < 2) return 1;
else return fib(n-1) + fib(n-2);
}
int main() {
int n = 5;
printf("fib(%d) = %d\n", n, fib(n));
return 0;
}
Sometimes, you want a friendlier display when debugging.