| Exercise 1 | Exercise 2 | Exercise 3 | Exercise 4 | Exercise 5 | |
| Solution 1 | Solution 2 | Solution 3 | Solution 4 | Solution 5 | |
| Show all | Hide all | |||||
Click on the links above
Exercise 1
Write a structure to represent an element of the form a+sqrt(-2)b for integer values a and b. Write a C function that takes two such complex numbers and returns the product of these numbers in the same form.
Solution 1
typedef struct { int a; int b; } comp2; comp2 mul ( comp2 z1 , comp2 z2 ) { comp2 z; z.a = (z1.a)*(z2.a) - 2*(z1.b)*(z2.b); z.b = (z1.a)*(z2.b) + (z1.b)*(z2.a); return z; }Exercise 2
Consider the following type definitions.
typedef struct { char name[80]; char rollno[8]; float cgpa; } stud1; typedef struct { char *name; char *rollno; float cgpa; } stud2; typedef stud1 *stud1ptr; typedef stud2 *stud2ptr; typedef stud1 stud1arr[100]; typedef stud2 stud2arr[100];Assume that a pointer is of size 32 bits. Determine the following sizes:
sizeof(stud1) sizeof(stud2) sizeof(stud1ptr) sizeof(stud2ptr) sizeof(stud1arr) sizeof(stud2arr)Solution 2
sizeof(stud1) = 80 + 8 + 4 = 92 sizeof(stud2) = 4 + 4 + 4 = 12 sizeof(stud1ptr) = 4 sizeof(stud2ptr) = 4 sizeof(stud1arr) = 92 x 100 = 9200 sizeof(stud2arr) = 12 x 100 = 1200Exercise 3
Write a C function that accepts a non-empty array of complex numbers and returns the array element having the maximum magnitude.
Solution 3
typedef struct { double re; double im; } complex; complex maxmag ( complex A[] , int n ) { int i, maxidx; double t, max; max = A[0].re * A[0].re + A[0].im * A[0].im; maxidx = 0; for (i = 1; i < n; ++i) { t = A[i].re * A[i].re + A[i].im * A[i].im; if (t > max) { max = t; maxidx = i; } } return A[maxidx]; }Exercise 4
What does the following program print? (Assume that an int is of size 32 bits.)
#include <stdio.h> #include <stdlib.h> #define MAXDIM 100 typedef int arr1[MAXDIM]; typedef struct { int data[MAXDIM]; } arr2; void f1 ( arr1 A ) { A[0] = 12345; } void f2 ( arr2 A ) { A.data[0] = 12345; } int main () { arr1 A1; arr2 A2; printf("sizeof(A1) = %d\n", sizeof(A1)); printf("sizeof(A2) = %d\n", sizeof(A2)); A1[0] = 0; f1(A1); printf("A1[0] = %d\n", A1[0]); A2.data[0] = 0; f2(A2); printf("A2.data[0] = %d\n", A2.data[0]); exit(0); }Solution 4
sizeof(A1) = 400 sizeof(A2) = 400 A1[0] = 12345 A2.data[0] = 0Exercise 5
Suppose that we want to represent lists of the following form:
(a (b (c) (d e f)) (g h))The first item of the list is the character a, whereas its second item is again a list (b (c) (d e f)), and the third item (g h) is a list too. The following figure depicts a possible representation of such list.
![]()
Define a suitable data type to represent such lists.
Write a function to print such a list with properly nested parentheses.
Solution 5
typedef struct _node { int type; /* zero for character, nonzero for (sub)list */ union { /* List item is a union of */ char data; /* either a character */ struct _node *sublist; /* or a sublist */ } item; struct _node *next; /* pointer to the next item */ } node; typedef node *list; void prnlist ( list L ) { node *p; printf("("); p = L; while (p != NULL) { if (p -> type == 0) printf("%c", (p->item).data); else prnlist((p->item).sublist); p = p -> next; if (p != NULL) printf(" "); } printf(")"); }