| L'Hopital's Rule |
| Refinement |
| Invocation (or Activation) Record |
| Tail Recursion |
| Landau Numbers |
| (1) | _____ |
|
| (2) | _____ |
is of higher order than |
| (3) | _____ |
Recursion can be used to solve the Towers of Hanoi problem |
| (4) | _____ |
The linear search algorithm discussed in lecture has a worst case time efficiency bound that is |
| (1) | _____ |
For the bubble sort algorithm we discussed in lecture: |
|
||
| (2) | _____ |
For the merge sort algorithm we discussed in lecture: |
|
drawLine(int x1, int y1, int x2, int y2) function
draws a line from the point (x1, y1) to the point (x2, y2).)
void drawRuler(int left, int right, int level)
{
int mid;
if (level < 1) return;
mid = (left + right) / 2;
drawLine(mid, 100, mid, 100-level*5);
drawRuler(left, mid-1, level-1);
drawRuler(mid+1, right, level-1);
}
n (the
size of the array). Explain each step of your derivation. (Note:
Your answer must be given in terms of .)
double *moments(double x[], int n, int k) {
double xbar, sum;
double *m, *total;
int i, j;
// Initialization
//
m = new double[k+1];
total = new double[k+1];
for (i=0; i<=k; i++) {
m[i] = 0;
total[i] = 0;
}
if (k >= 2) {
// Calculate the mean
//
sum = 0;
for (i=0; i<n; i++) {
sum += x[i];
}
xbar = sum/(double)n;
// Calculate moments 2 through k
//
for (j=2; j<=k; j++) {
for (i=0; i<n; i++) {
total[j] += pow((x[i] - xbar), j);
}
m[j] = total[j] / (double)n;
}
}
delete[] total;
return m;
}
/**
* Calculates the nth Fibonacci Number
*
* @param n The desired Fibonacci Number (must be positive)
*/
int fibonacci(int n)
{
}
double power(double x, unsigned int n)
{
if (n == 0) return 1.0;
else return x*power(x, n-1);
}
is fairly inefficient. For example, observe that the computation of requires 8 multiplications. However, if we observe that then we can make this algorithm more efficient.
Use this observation to improve this algorithm. Your answer must be recursive. Hint: A special case is needed for odd exponents.
sort is implemented as
follows:
void sort(int a[], int length)
{
int i, j, k, h, tmp;
int cols[3] = {7, 3, 1};
for (k=0; k<3; k++)
{
h = cols[k];
for (i=h; i<length; i++)
{
tmp = a[i];
j = i;
while ( (j>=h) && (a[j-h]>tmp) )
{
a[j] = a[j-h];
j = j - h;
}
a[j]=tmp;
}
cout << endl << "Iteration " << k << ": ";
for (i=0; i<length; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
}
What will be printed when the following main
function is executed?
int main(void)
{
int a[6] = {8,4,7,3,9,3};
int i, n;
n = 6;
sort(a, n);
return 1;
}
[4, 4, 1, 0, 4, 4, 1, 0, 1, 1]
Copyright 2010