190 likes | 334 Views
Algorithms and Data Structures Exercise for the 1st Written Exam (23 April 2014, 9:00 am). Dynamic Memory Allocation, Function Calls, Complexity Analysis. Dynamic Memory Allocation. Assignment 1:.
E N D
Algorithms and Data Structures Exercise for the 1st Written Exam (23 April 2014, 9:00 am) • Dynamic Memory Allocation, Function Calls, • Complexity Analysis
Assignment 1: • Write thefunctionthat takes an array of integers and removes its members containing even values. Based on the new number of array elements, the function dynamically decreases the size of the array and returns the pointer to this array. The prototype of the function is: int *remove_even(int *array, int *elem_num); • Wrtite the main program that dynamically allocates the memory for the array, fills the array with random values, calls the function remove_even, prints out the new array, and, finally, releases dynamically allocated memory. • Note: after removal, the mutual order of members with odd values is not important
Solution (function): int *remove_even(int *array, int *elem_num){ int i, j; for(i=0; i<*elem_num;){ if (array[i] % 2 == 0){ for(j=i+1; j<*elem_num; j++) array[j-1] = array[j]; (*elem_num)--; } else i++; } array = (int*) realloc(array, *elem_num * sizeof(int)); return array; }
Solution (main program): int main(){ int *array, i, elem_num = 20; array = (int*) malloc(elem_num * sizeof(int)); srand((unsigned) time(NULL)); for(i=0; i< elem_num; i++){ array[i] = rand(); } printf("\nBefore removal\n"); for(i=0; i< elem_num; i++){ printf("%d\n", array[i]); } array = remove_even(array, &elem_num); printf("\nAfter removal\n"); for(i=0; i<elem_num; i++){ printf("%d\n", array[i]); } free(array); return 0; }
Assignment 2: • Write thefunctionthat takes an array of integers and duplicates it (creates new duplicated array). The function returns the pointer to the duplicated array. The prototype of the function is: int *duplicate(int *array, int elem_num); • Wrtite the main program that defines an array of 50 elements, fills the array with random integers from the range [1-20], calls the function duplicate, prints out the new array, and, finally, releases dynamically allocated memory.
Solution (function & main program): int *duplicate(int *array, int elem_num){ int i, *new_array; new_array = (int*) malloc(elem_num * sizeof(int)); for(i=0; i<elem_num; i++) new_array[i] = array[i]; return new_array; } int main(){ int array1[50], *array2, i, elem_num = 50; srand((unsigned) time(NULL)); for(i=0; i<elem_num; i++){ array1[i] = 1+rand()%20; } array2 = duplicate(array1, elem_num); for(i=0; i<elem_num; i++){ printf("%d\n", array2[i]); } free(array2); return 0; }
Assignment 3 (Written Exam 21 March 2011): • Write the function remove_row that takes the index of the matrix row and removes that row from the matrix (some rows must be shifted). This function releases the unnecessary memory allocated for that matrix. The rest of the matrix must be preserved. The prototype of the function is: int *remove_row(int *mat, int rown, int coln, int irow); • Write themain program that dynamically allocates the matrix mat and fills it with pseudo-random integers. In the main program input the number of matrix rows and columns: rown and coln, lower and the upper bound of the range for the generation of pseudo-random numbers. Then, allocate the needed memory and fill in the matrix with pseudo-random numbers from the given range. • In the main program, ask the user to input the index of matrix row irow and call the function remove_row.
Solution (function): int *remove_row(int * mat, int rown, int coln, int irow){ int i, j; for (i = irow + 1; i<rown; i++) for (j = 0; j<coln; j++) *(mat + (i - 1)*coln + j) = *(mat + (i)*coln + j); mat = (int *)realloc(mat, (rown - 1)* coln *sizeof(int)); return mat; }
Solution (main): int main(){ int *mat, i, j, irow; int rownum, colnum, LB, UB; printf("Input the number of rows, columns, lower bound, and the upper bound: \n"); scanf("%d %d %d %d", &rownum, &colnum, &LB, &UB); mat = (int *)malloc(rownum*colnum*sizeof(int)); srand((unsigned)time(NULL)); for (i = 0; i<rownum; i++) for (j = 0; j<colnum; j++) mat[i*colnum + j] = rand() % (UB - LB + 1) + LB; printf("Input the index of the row to remove: \n"); do{ scanf("%d", &irow); } while (irow<0 || irow>rownum); mat = remove_row(mat, rownum, colnum, irow); free(mat); return 0; }
Assignment 4: • Illustrate the system stack during the call and execution of the function sum, and at the call of the function add: int add(int first, int second){ return a+b; } void sum(int a, char b, int * s){ int c; c=add(a,b); if (c<0) c=-c; *s=c; } • The function call is: int s; sum(5, 7, &s);
Assignment 5 (Written Exam 21 March 2011): • Illustrate the system stack just before the exit from the function f2. Indicate what is the size of each variable on the stack: void f2(double *a, int *b, int n){ int counter[10]={0}; a[n]=b[n]; counter[n%10]++; } void f1(int *array, int n) { double *array2; int i; array2=(double*)malloc(n*sizeof( double)); for(i=0;i<n;i++) f2(array2,array,n-i-1); } int main() { int array[100]; ... f1(array, 100); }
Solution: 40 counter return address - f2 4 array2 4 4 array 4 n-i-1 4 i 4 array2 return address - f1 4 array 4 100 4
Assignment 6: • What is the time complexity of the following program section, in O and notation? • The answer MUST be argumented! for (i = 0; i < n; i++) { if (p[i][i] == i){ printf("%d", i); break; }} O(n) – the worst case is when it is for each i, p[i][i]!=i. In that case the loop (statement if inside the loop) executes n times. (1) – the best case is when p[0][0]==0. Then for loop terminates with break; after execution of two statements
Assignment 7: • What is the time complexity of the following program section ( in bold), in O notation? • The answer MUST be argumented! int b; ... char *rezultat = ... rezultat[0] = '\0';while (n > 0) { if (n % 2 == 1) rezultat = strcat(rezultat, "1"); else rezultat = strcat(rezultat, "0"); n = n/2; } The number n is constantly divided by 2. We finish when n is 1 (i.e. n/2 = 0). How many steps do we need to come to 0 (i.e. 1) when dividing by 2: n, n/2, n/4,...n/2k in k-th step. Solve n/2k = 1 for k. The number of steps is log2n. In each step strcat is called. Then, we have 1+2+3+...+log2n i.e. the solution is O((log2n)2).
Assignment 8 (Written Exam 3 September 2013): • What is the asymptotic complexity of the following algorithms: • Take that the asymptotic complexity of the function work(x,y) is x • The answer MUST be argumented! 2n2log2n s=0; for(i=1; i<=n; i*=2) for(j=1; j<=n; j++) s += work(n,j) + work(n,i); n2log2n s=0; for(i=1; i<=n; i*=2) for(j=1; j<=n; j++) s += work(j,n) + work(j,i);