| 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
A polynomial is represented by its coefficients. An array is commonly used to store these coefficients. Moreover, the degree of the polynomial should also be stored in order to identify the true size of the array. Declare a data type using a dynamic array for the coefficients. Also write a function that reads the degree and the coefficients from the user and returns the polynomial in your representation.
Solution 1
typedef struct { int degree; int *coefficient; } polynomial; polynomial readpoly ( ) { int i, d; polynomial f; printf("Enter the degree: "); scanf("%d", &d); f.degree = d; if (d >= 0) { f.coefficient = (int *)malloc((d + 1) * sizeof(int)); for (i=0; i<=d; ++i) { printf("Enter the coefficient of x^%d: ", i); scanf("%d", &(f.coefficient[i])); } } else { f.coefficient = NULL; } return f; }Exercise 2
Write a function that takes two polynomials as input, and outputs the product of the two input polynomials. Use the representation of Exercise 1.
Solution 2
polynomial polymul ( polynomial f, polynomial g ) { polynomial h; int i, j; if ((f.degree < 0) || (g.degree < 0)) { h.degree = -1; h.coefficient = NULL; return h; } h.degree = f.degree + g.degree; h.coefficient = (int *)malloc((h.degree + 1) * sizeof(int)); for (i=0; i<=h.degree; ++i) h.coefficient[i] = 0; for (i=0; i<=f.degree; ++i) for (j=0; j<=g.degree; ++j) h.coefficient[i+j] += f.coefficient[i] * g.coefficient[j]; return h; }Exercise 3
Consider the following four two-dimensional arrays.
char A[4][5]; char (*B)[5]; char *C[4]; char **D;What are sizeof(A), sizeof(B), sizeof(C) and sizeof(D)?
What are sizeof(*A), sizeof(*B), sizeof(*C) and sizeof(*D)?
What are sizeof(**A), sizeof(**B), sizeof(**C) and sizeof(**D)?
Solution 3
sizeof(A) = 20 sizeof(B) = 4 sizeof(C) = 16 sizeof(D) = 4 sizeof(*A) = 5 sizeof(*B) = 5 sizeof(*C) = 4 sizeof(*D) = 4 sizeof(**A) = 1 sizeof(**B) = 1 sizeof(**C) = 1 sizeof(**D) = 1Exercise 4
Suppose that we want to store the following two-dimensional data.
a b c d e f g h i j k Use each of the four arrays of Exercise 3 to store these data. Be as stingy as possible while allocating memory.
Solution 4
A[0][0] = 'a'; A[1][0] = 'b'; A[1][1] = 'c'; A[1][2] = 'd'; A[2][0] = 'e'; A[2][1] = 'f'; A[3][0] = 'g'; A[3][1] = 'h'; A[3][2] = 'i'; A[3][3] = 'j'; A[3][4] = 'k'; B = (char (*)[5])malloc(4 * sizeof(char[5])); B[0][0] = 'a'; B[1][0] = 'b'; B[1][1] = 'c'; B[1][2] = 'd'; B[2][0] = 'e'; B[2][1] = 'f'; B[3][0] = 'g'; B[3][1] = 'h'; B[3][2] = 'i'; B[3][3] = 'j'; B[3][4] = 'k'; C[0] = (char *)malloc(sizeof(char)); C[1] = (char *)malloc(3 * sizeof(char)); C[2] = (char *)malloc(2 * sizeof(char)); C[3] = (char *)malloc(5 * sizeof(char)); C[0][0] = 'a'; C[1][0] = 'b'; C[1][1] = 'c'; C[1][2] = 'd'; C[2][0] = 'e'; C[2][1] = 'f'; C[3][0] = 'g'; C[3][1] = 'h'; C[3][2] = 'i'; C[3][3] = 'j'; C[3][4] = 'k'; D = (char **)malloc(4 * sizeof(char *)); D[0] = (char *)malloc(sizeof(char)); D[1] = (char *)malloc(3 * sizeof(char)); D[2] = (char *)malloc(2 * sizeof(char)); D[3] = (char *)malloc(5 * sizeof(char)); D[0][0] = 'a'; D[1][0] = 'b'; D[1][1] = 'c'; D[1][2] = 'd'; D[2][0] = 'e'; D[2][1] = 'f'; D[3][0] = 'g'; D[3][1] = 'h'; D[3][2] = 'i'; D[3][3] = 'j'; D[3][4] = 'k';Exercise 5
Free all the memory allocated in Exercise 4.
Solution 5
free(B); for (i=0; i<4; ++i) free(C[i]); for (i=0; i<4; ++i) free(D[i]); free(D);