C Puzzle Answers
C Puzzle Answers
C Puzzle Answers
L1.A1
*Solution for L1.Q1
a = xxx * 10
which is => a = ABC - XYZ * 10
=> a = 20 - 10 * 10
=> a = 20 - 100
=> a = -80
L1.A2
Solution for L1.Q2
L1.A3
Solution for L1.Q3
a /= cnt; /* ie. a /= 0 */
which leads to divide-by-zero error.
L1.A4
Solution for L1.Q4
L1.A5
Solution for L1.Q5
L1.A6
Solution for L1.Q6
Main : 5 7 Fn : 7 7
Main : 6 6
Fn : 8 7
L2.A1
Solution for L2.Q1
L2.A2
Solution for L2.Q2
L2.A3
Solution for L2.Q3
L2.A4
Solution for L2.Q4
L2.A5
Solution for L2.Q5
L2.A6
Solution for L2.Q6
Just try to execute this file as such. You can find out
that it will exit immediately. Do you know why?
With this hint, we can trace out the error. If you look
into the macro 'Error', you can easily identify that there
are two separete statements without brases '{ ..}'. That is
the problem. So, it exits after the calling open(). The
macro should be put inside the brases like this.
L2.A7
Solution for L2.Q7
L2.A8
Solution for L2.Q8
#include
main(argc, argv)
int argc;
char *argv[];
{
int count = 0, i;
int v = atoi(argv[1]);
#include
len--;
while(i < j) {
*(str+i)^=*(str+len)^=*(str+i)^=*(str+len);
i++; len--;
}
return(str);
}
L3.A2
Solution for L3.Q2
#include
void main()
{
int i;
#include
main()
{
int a, b;
L3.A4
Solution for L3.Q4
#include
/* Verification routines */
main()
{
int a=10, b =20;
float e=10.0, f = 20.0;
char *x = "string1", *y = "string2";
typedef struct { int a; char s[20]; } st;
st s1 = {50, "struct1"}, s2 = {100, "struct2"};
swap(a, b, int);
printf("%d %d\n", a, b);
swap(e, f, float );
printf("%f %f\n", e, f);
ptr_swap();
}
ptr_swap()
{
int *a, *b;
float *c, *d;
a = (int *) malloc(sizeof(int));
b = (int *) malloc(sizeof(int));
*a = 10; *b = 20;
c = (float *) malloc(sizeof(float));
d = (float *) malloc(sizeof(float));
*c = 10.01; *d = 20.02;
L3.A5
Solution for L3.Q5
#include
/* Solution */
while ((*head)) {
if ((*head)->next == NULL) tail = head;
if ((*head)->val == val) {
*head = (*head)->next;
}
else head = &(*head)->next;
}
while((*tail)) {
if ((*tail)->val == val) {
*tail = (*tail)->prev;
}
else tail= &(*tail)->prev;
}
}
Link *DL_build();
void DL_print(Link *);
main()
{
int val;
Link *head;
head = DL_build();
DL_print(head);
DL_delete(&head, val);
DL_print(head);
}
Link *DL_build()
{
int val;
Link *head, *prev, *next;
while(1) {
Link *new;
if (val == 0) break;
prev = new;
}
return (head);
}
L3.A6
Solution for L3.Q6
The first value will be 0, the rest of the three values will
not be predictable. Actually it prints the values of the
following location in each step
* savea
* (savea + sizeof(int) * sizeof(int))
etc...
Note: You can verify the above by varing the type of 'savea'
variable to char, double, struct, etc.
LX.A7
Solution for LX.Q7
LX.A8
Solution for LX.Q8
#include
typedef struct Link {
int val;
struct Link *next;
} Link;
while(head) {
Link *tmp;
tmp = head;
head = head->next;
tmp->next = revlist;
revlist = tmp;
}
return revlist;
}
Link *SL_build();
main()
{
Link *head;
head = SL_build();
head = SL_reverse(head);
printf("\nReversed List\n\n");
while(head) {
printf("%d\t", head->val);
head = head->next;
}
}
Link *SL_build()
{
Link *head, *prev;
while(1) {
Link *new;
int val;
LX.A9
Solution for LX.Q9
Extend the same logic to this problem. You will get the
output as follows