Dynamic Memory Allocation, Function Calls, Complexity Analysis

19
Dynamic Memory Allocation, Function Calls, Complexity Analysis Algorithms and Data Structures Exercise for the 1st Written Exam (23 April 2014, 9:00 am)

description

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:. - PowerPoint PPT Presentation

Transcript of Dynamic Memory Allocation, Function Calls, Complexity Analysis

Page 1: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Dynamic Memory Allocation, Function Calls,

Complexity Analysis

Algorithms and Data StructuresExercise for the 1st Written Exam (23 April 2014, 9:00 am)

Page 2: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Dynamic Memory Allocation

Page 3: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Assignment 1:Write the function that 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

Page 4: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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;

}

Page 5: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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;

}

Page 6: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Assignment 2:Write the function that 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.

Page 7: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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;

}

Page 8: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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 the main 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.

Page 9: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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;

}

Page 10: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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;

}

Page 11: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Function Call

Page 12: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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);

Page 13: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Solution:

return address from add

first

second

local variable c

return address from sum

a

b

s

Page 14: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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:

 

int main() {int array[100];...f1(array, 100);

}

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);}

Page 15: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

100

array

return address - f1

array2

i

n-i-1

array

array2

return address - f2

counter

4

4

4

4

4

4

4

4

4

40

Solution:

Page 16: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

Complexity Analysis

Page 17: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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.

Assignment 6:

(1) – the best case is when p[0][0]==0. Then for loop terminates with break; after execution of two

statements

Page 18: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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).

Page 19: Dynamic Memory Allocation, Function Calls,  Complexity  Analysis

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!

s=0;for(i=1; i<=n; i*=2)

for(j=1; j<=n; j++)s += work(n,j) + work(n,i);

s=0;for(i=1; i<=n; i*=2)

for(j=1; j<=n; j++)s += work(j,n) + work(j,i);

2n2log2n

n2log2n