CS 310 Programming Practices Midterm Examination
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CS 310 Programming Practices
Midterm Examination
Please complete the following exercises and submit them before the due time. The deliverables must include the exercise questions or labels and your answers.
Each exercise is grouped in a folder containing: source files, object files, an executable file, and an PDF containing screen shots showing the results of running your programs.
1.[20 points] Write a recursive version of the function binarySearch (int list [], int x), which accepts a sorted array of ints and a key x. The function returns the position of x in the array if x in the array, otherwise return - 1. You can use any sort function to sort the test data.
Test data: 12, 5, 8, 4, 45, 32, 25, 16, 3, 35,38, 22
Three keys: 2, 16, 3
2. [30 points] (Geometry: two rectangles) Write a program that prompts the user to enter the center x-, y-coordinates, width, and height of two rectangles and determines whether the second rectangle is inside the first or overlaps with the first, as shown in Figure: (a) A rectangle is inside another one. (b) A rectangle overlaps another one.
Here are the sample runs:
Enter r1’s center x-, y coordinates, width, and height: 2.5 4 2.5 43
Enter r2’s center x-, y coordinates, width, and height: 1.5 5 0.5 3
r2 is inside r1
Enter r1’s center x-, y coordinates, width, and height: 1 2 3 5.5
Enter r2’s center x-, y coordinates, width, and height: 3 4 4.5 5
r2 overlaps r1
Enter r1’s center x-, y coordinates, width, and height: 1 2 3 3
Enter r2’s center x-, y coordinates, width, and height: 40 45 3 2
r2 does not overlaps r1
Write a program and run it with the above test data, showing the right results.
3. [20 points]
( 1) Write a program to compute the following summation:
(2) Rewrite the above code as a function sumOfseries (int n), and write the main() as test framework that accepts user entering a number n and call the function to show the sum of series. Two test data: 100, 200.
4.[30 points] Write a program to fulfill the tasks.
( 1) It reads a text file testData.txt,
( 1) Counts the occurrences of each letter (insensitive of small case and upper case ) , and
(2) Display each letter in ascending order of its occurrence number, and print the occurrence number of each letter.
Example of print: |
|
|
|
|
|
|
|
|
|
|
|
|
a a |
|
|
|
|
o |
c |
a |
|
|
|
b |
o |
c |
a |
|
f |
i |
b |
o |
c |
a |
d g |
f |
i |
b |
o |
c |
a |
x y d g |
f |
i |
b |
o |
c |
a |
z x y d g |
f |
i |
... b |
o |
c |
a |
2022-07-30