| 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
What values does the following program print?
#include <stdio.h> void doit ( int A[] ) { int *p; p = A; ++p; printf("%d\n", *p); } int main () { int A[] = {1,2,3,4,5}; int *p; p = A; doit(p); printf("%d"\n", *p); }Solution 1
2 1Exercise 2
Write a function that takes three two-dimensional arrays A,B,C and three positive integers r,s,t as input. A is treated as an r x s matrix, and B as an s x t matrix. The function computes and stores in C the product matrix AB.
Solution 2
void matmul ( int A[][100], int B[][100], int C[][100], int r, int s, int t ) { int i, j, k; for (i=0; i<r; ++i) { for (j=0; j<t; ++j) { C[i][j] = 0; for (k=0; k<s; ++k) C[i][j] += A[i][k] * B[k][j]; } } }Exercise 3
Write a recursive function that computes the maximum of an array A of size n as follows. It first computes the maximum of the first n/2 elements and stores this maximum in a temporary variable a. It then recursively computes the maximum of the remaining elements of the array and stores this second maximum in a temporary variable b. The function then compares a and b and returns the larger of these values. Recursion stops when the size of the array is reduced to one.
Solution 3
#define ERROR -2147483648 int findmax ( int A[], int n ) { int a,b; if (n == 0) return ERROR; if (n == 1) return A[0]; a = findmax(A,n/2); b = findmax(A+n/2,n-(n/2)); return ((a >= b) ? a : b); }Exercise 4
Write a function that accepts two strings s and t as parameters. The function checks whether t is a substring of s. If so, a pointer to the leftmost match is returned, otherwise the NULL pointer is returned.
Solution 4
char *match ( char *s, char *t ) { int lens, lent, i; lens = stdlen(s); lent = stdlen(t); for (i=0; i<=lens-lent; ++i) { if (!strncmp(s+i,t,lent)) return s+i; } return NULL; }Exercise 5
Given a 2-dimensional matrix as input, the following function returns an array storing a specified column of the matrix.
#define MAXDIM 100 int *getcol ( int (*A)[MAXDIM] , int m , int n , int j ) { int v[MAXDIM]; int i; for (i=0; i<m; ++i) v[i] = A[i][j]; return v; }If you compile this function, the compiler issues a warning message. What is wrong with this function? How can you remove this problem?
Solution 5
A statically allocated array inside a function is local to that function. After the function returns, the memory goes back to the run-time system pool. Returning a pointer to a temporary memory is the source of the problem.
This error can be removed by dynamically allocating memory to the vector v. A dynamically allocated memory does not go back to the system pool, until you expicitly free it (or the program terminates). The following implementation removes this error.
#define MAXDIM 100 int *getcol ( int (*A)[MAXDIM] , int m , int n , int j ) { int *v; int i; v = (int *)malloc(m * sizeof(int)); for (i=0; i<m; ++i) v[i] = A[i][j]; return v; }