Description
Problem 1
Write the function int removekdigits(long long int n, int k); that returns an integer like n after
removing the first k largest digits from n, and returns the smallest integer possible from the resulting digits. Return 0 if
no digits remain after removing k digits. Assume k<= length of n.
• You must NOT use a variable length array (VLA). Using VLA will result in receiving a zero on this question.
• Assume n>=0
Submit kdigits.c file containing only your implemented functions (that is, you must delete the test cases
portion/the main function). However, you must keep the required included libraries.
Sample Test program (make sure you test your solution with more test cases)
1. int main(void) {
2. assert(removekdigits(345267,3)==234);
3. assert(removekdigits(0,0)==0);
4. assert(removekdigits(9,1)==0);
5. assert(removekdigits(871,0)==178);
6. assert(removekdigits(9898989,2)==88899);
7.
8. return 0;
9. }
10.
Page 3 of 8
Problem 2
Let a be an mxn matrix with 0 and 1 entries. Assume the top left entry a[0][0] = 1 and the bottom right entry a[m1][n-1] = 1.
A path between the top left and bottom right entries is a sequence of matrix entries such that adjacent entries in the
path are adjacent in the matrix.
The figure below gives two such paths (assume the blank entries are zeros in the matrix).
A monotone path is a path that does not travel left or up. In the figure, the red path is monotone. The blue path is not,
because it goes left.
Write a function int monotonePath(int m, int n, int a[m][n]) that returns 1 if the matrix a has a
monotone path from a[0][0] to a[m-1][n-1], otherwise return 0.
Submit path.c file containing only your implemented function (that is, you must delete the test cases portion/the
main function). However, you must keep the required included libraries.
Page 4 of 8
Sample Test program (You need to add more test cases)
1. int main(void)
2. {
3. int m = 10;
4. int n = 12;
5. int a[][12] = {{1,1,0,0,0,0,0,0,0,0,0,0},
6. {0,1,1,0,0,0,0,0,0,0,0,0},
7. {0,0,1,1,1,1,0,0,0,0,0,0},
8. {0,0,1,0,0,1,0,0,0,0,0,0},
9. {0,1,1,0,0,1,1,0,0,0,0,0},
10. {0,1,0,0,0,0,1,0,0,0,0,0},
11. {0,1,1,0,0,0,1,1,1,0,0,0},
12. {0,0,1,0,0,0,0,0,1,0,0,0},
13. {0,0,1,1,1,1,1,1,1,1,1,0},
14. {0,0,0,0,0,0,0,0,0,0,1,1}
15. };
16.
17. // a, but with one entry switched eliminating the monotone path
18. int b[10][12] = {{1,1,0,0,0,0,0,0,0,0,0,0},
19. {0,1,1,0,0,0,0,0,0,0,0,0},
20. {0,0,1,1,0,1,0,0,0,0,0,0},
21. {0,0,1,0,0,1,0,0,0,0,0,0},
22. {0,1,1,0,0,1,1,0,0,0,0,0},
23. {0,1,0,0,0,0,1,0,0,0,0,0},
24. {0,1,1,0,0,0,1,1,1,0,0,0},
25. {0,0,1,0,0,0,0,0,1,0,0,0},
26. {0,0,1,1,1,1,1,1,1,1,1,0},
27. {0,0,0,0,0,0,0,0,0,0,1,1}
28. };
29.
30. assert (monotonePath(m, n, a));
31. assert (!monotonePath(m, n, b));
32. return 0;
33. }
34.
Page 5 of 8
Problem 3
Complete the provided C program named trapezoidal.c that consists of the function
// b>a, n>0
double trapezoidal(double (*f)(double), double a, double b, double epsilon, int
n);
that computes an approximation to find the definite integral of a function.
The function f(x) is divided into many sub-intervals, and each interval is approximated by a Trapezium. Then the area of
trapeziums is calculated to find the integral, which is the area under the curve. The more the number of trapeziums is
used, the better the approximation.
Trapezodial begins by taking the dividing the area into n sub-intervals, calculate the area of trapeziums, then continue
doing the same by doubling the number of sub-intervals until the difference between the last two results (area of n
trapeziums vs area of 2n trapeziums) calculated is less or equal epsilon.
Note: You are to submit this file containing only your implemented function (that is, you must delete the test cases
portion and the main function). However, you must keep the required included libraries.
Page 6 of 8
The sample code for testing is below.
1. #include <stdio.h>
2. #include <math.h>
3. #include <assert.h>
4. #define PI acos(-1)
5.
6. double f1(double x)
7. {
8. return x * x;
9. }
10.
11. double f2(double x)
12. {
13. return cos(x);
14. }
15.
16. double f3(double x)
17. {
18. return sqrt(x);
19. }
20.
21. double trapezoidal(double (*f) (double), double a, double b, double epsilon, int
n)
22. {
23.
24. }
25.
26. int main(void)
27. {
28. assert(fabs(trapezoidal(f1, 5, 10, 0.00001, 2) – 291.667) <= 0.001);
29. assert(fabs(trapezoidal(f2, PI/2, 3, 0.00001, 5) – -0.858879) <=
0.000001);
30. assert(fabs(trapezoidal(f3, 1, 4, 0.00001, 2) – 4.666) <= 0.001);
31. return 0;
32. }
33.
Page 7 of 8
Problem 4
Write a C program grades.c that reads the grades of a certain course and provides some statistics as illustrated in the
examples below. Please note the following:
• Your program must read grades using scanf until a character is entered.
• Assume that all entered grades are between 0 – 100, inclusive. (This is a critical point to be able to solve the
problem without using VLA)
• We also provided string.txt to avoid any typos in formatting the outputs. Make sure to use it for printing.
• Assume that the user will enter at least two valid grades.
• Assume that at least one grade is entered.
• Failing grade is a grade <50.
• You must NOT use a variable length array (VLA). Using VLA will result in receiving a zero on this question.
• You may not limit the maximum number of grades that can be entered. Failing to do so will result in receiving
a zero on this question.
Page 8 of 8
Sample input 1:
80
78
80
96
85
78
85
85
9
44
45
66
45
13
?
Sample output 1:
The maximal grade is: 96
The number of students who received the maximal grade is 1
The minimal grade is: 9
The number of students who received the minimal grade is 1
The course mean is: 63.500
The standard deviation is: 27.734
The number of students who failed the course is: 5
Sample input 2:
100
100
100
50
30
30
30
30
0
0
0
*
Sample output 2:
The maximal grade is: 100
The number of students who received the maximal grade is 3
The minimal grade is: 0
The number of students who received the minimal grade is 3
The course mean is: 42.727
The standard deviation is: 40.023
The number of students who failed the course is: 7

