More Problems: Write a function that returns the mean of the

advertisement
More Problems:
1. Write a function that returns the mean of the elements in an array of size n.
#include <stdio.h>
double average(double a[], int n);
int main(void){
double test[] = {-15.3, 111.4, 21.7, -19.0, 3.4);
printf(“%g \n”, average(test, sizeof(test)/sizeof(test[0]))));
return 0;
}
2. Approximately how many times will binary search probe a sorted array of size 1,000,000
when searching for a value that does not appear in the array?
3. Quick sort algorithm has three steps: 1) pick a pivot, 2) partition the array and 3) sort
each partition recursively. Consider sorting the following example array with quick sort:
int a[] = { 5, 6, 4, 7, 3, 8, 2}
The algorithm selects a[0] = 5 as the pivot element. Show the contents of the array after it
has been partitioned using the partitioning algorithm given in lecture. (Hint: Show values
before the recursive call)
4. Identify the sub-array whose contents sum to the smallest value, and return the sum of
that sub-array. Construct a O(n) solution.
5. What is wrong with this code? If it is supposed to print out the integers from 1 to 100.
int main(void){
int i;
for(i = 0; i < 99; i++);
printf(“$d\n”, i);
}
6. Explain the issues that can occur with double floating point partition?
7. What is the worst-case time complexity of Merge sort, Insertion sort, Selection sort, and
Quick sort?
8. Write a program that reads in a series of integers until the value 0 is seen. After it reads
an unknown amount of numbers it prints the numbers in order 3 times. (Hint: use
dynamic memory)
9. If the Unicode codepoint of a character is 950 in decimal, which is 1110110110 in binary.
What is the binary UTF-8 encoding for this character?
10. What does this program print?
#include <stdio.h>
int main(){
int i;
for(i = 0; i < 10; i += 7) I -= 2;
printf(“%d\n”, i);
}
11. Write a non-recursive function that computes F_n in O(n) time. Assume n > 0.
#include <stdio.h>
int fibonacci(int n);
int main(void){
printf(“%d\n”, fibonacci(5));
printf(“%d\n”, fibonacci(20));
return 0;
}
12. Implement a function that returns a count of the number of elements in an array of size n
that is greater than a specified value x.
#include <stdio.h>
int greater(int a[], int n, int x);
int main(void){
int a[] = {1,2,3,4,5,6,7,);
printf(“%d\n”, greater(a, 7, 0)); //output 7
printf(“%d\n”, greater(a, 7, 3)); //output 7
printf(“%d\n”, greater(a, 7, 7)); //output 7
return 0;
}
13. Re-write the following polynomial to illustrate Honer’s rule: 8x^3 + x^2 + 4x – 10
14. Write a function that determines if a string is equal to “yes”, returning 0 or 1 as
appropriate. Treat upper and lower case letters as equivalent.
15. Implement a function that will calculate 2^n when n can be greater 10000. (Note: 2^1000
has over 300 digits and won’t fit into a int)
16. Write merge sort.
17. Implement a function that returns any integer value that is not inside of the given array.
18. Given a person’s birthday, day, month, year. Calculate how old they will be today:
December 3rd 2015.
struct date{
int day, month, year;
};
int old(struct date *dob, struct date *today);
int main(void){
struct date today = {3,12,2015};
struct date age = {3,12,1993}
printf(“%d\n”, old(&test0, &today));
return 0;
}
19. Write a function that reverses a string. Do not change the original string and store the
results of the new string in a newly allocated string. (Don’t forget to free)
20. Shift the contents of an array by 1. Another words a[1] becomes a[2], a[2] becomes a[3]
and a[n] becomes a[0]. The implementation must be O(n).
21. Implement a function that computes the dot product of two vectors.
struct vector{
double x, y, z;
};
22. Write a program to print out the real root of a function accurate to at least 10^-5.
(Midterm question 7)
23. Implement function zeroes that returns the number of trailing zeros at the end of n!.
24. Write a function that appends one string to the end of another string.
25. Sum the contents of an array without using the [] operator.
26. Calculate the result of x that is taken to the power of x, n times. Example: x^x^x^x where
n is 3.
27. Points on the plane can be defined using a structure as follows:
struct point{
double x,y;
};
Implement the following function, which sorts an array of n points according to their distance
from the origin (0,0);
#include <stdio.h>
struct point{
double x,y;
};
void pointSort (struct point p[], int n);
int main(void){
int i;
struct point p[] = {{-2.0, 3.0}, {1.0,-1.0}, {2.0, 1.0}, {0.0,0.0}};
pointSort (p, 4);
for(i = 0; i < 4; i++)
printf(“(%g, %g)\n”, p[i].x, p[i].y);
return 0;
}
Download