Vježba za 2. međuispit

Download Report

Transcript Vježba za 2. međuispit

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:
 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
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;
}
int main(){
Solution (main program):
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 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.
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 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 pseudorandom numbers. Then, allocate the needed memory and fill in the matrix with pseudorandom 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;
}
Function Call
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);
Solution:
return address from add
first
second
local variable c
return address from sum
a
b
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
4
return address - f2
4
array2
4
array
4
n-i-1
4
i
4
array2
4
return address - f1
4
array
4
100
Complexity Analysis
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;
...
The number n is constantly divided by
2. We finish
when n is 1 (i.e. n/2 =
char *rezultat
= ...
0). How many steps do we need to come
rezultat[0]
'\0';
to =
0 (i.e.
1) when dividing by 2:
k in k-th step. Solve
while (n n,> n/2,
0) n/4,...n/2
{
n/2k = 1 for k. The number of steps is
if (n %log2 n.==In 1)
each step strcat is called.
2
rezultat
= have
strcat(rezultat,
Then, we
1+2+3+...+log2n i.e. "1");
the
solution is O((log2n)2).
else
rezultat = strcat(rezultat, "0");
n = n/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);
s=0;
n2log2n
for(i=1; i<=n; i*=2)
for(j=1; j<=n; j++)
s += work(j,n) + work(j,i);