Fall 2024 Midterm
Page 1
ESE124 Programming Fundamentals
Exercise Activity – Fall 2024 Midterm (10/2/2024)
First Last Name:__________
ID:____________________
Lab Section: ____________
Add comments to explain your code. Choose 3 out of 4 questions. If you do all, we’ll grade 3 plus
the bonus. You can tear the front page, but be sure to write your name/ID on the second page.
Q1 : Write a C program that reads words from a file and counts how many words there are for each length. Display
counts for word lengths up to 5 characters, and group all words of 6 characters or more into one category.
(33.33points)
Input File (words.txt):
C programming is fun
Data structures are important
Output File (word_lengths.txt)
Word length counts:
1 letter: 1
2 letter: 1
3 letters: 2
4 letter: 1
5 letters: 0
6+ letters: 4
Q2: Read data from input.txt, which includes students' names, lab sections, and final grades. Extract the
data for Lab 1 and Lab 2, sort the grades in ascending order, and save the results to two output files
named sortedlab1.txt and sortedlab2.txt, including header text line. (33.33 points)
Input file:
stu1 lab1 88.5
stu2 lab2 66.5
stu3 lab1 76.2
stu4 lab1 90.3
stu5 lab2 76.2
stu6 lab1 70.3
Output file:
Sorted the ESE 124 grade on Lab1:
stu6 70.3
stu3 76.2
stu1 88.5
stu4 90.3
Sorted the ESE 124 grade on Lab2:
stu2 66.5
stu5 76.2
Electrical and Computer Engineering | 1
Page 2
ESE124 Programming Fundamentals
Q3 Write a C program to merge two 1D arrays into a third array that includes duplicates, ordered by the
number of duplicates. If two numbers have the same count, sort them in ascending order.(33.33 points)
Input example:
First array: 7 7 7 6 6 5 4
Second array: 2 2 2 3 3 4 5 5 5 5 6 7
Expected output:
Merged array: [5, 7, 2, 6, 3, 4]
Explanation:
5 appears 5 times
7 appears 4 times
2 appears 3 times
6 appears 3 times
3 appears 2 times
4 appears 1 time
Q4: Design a C program to compute and display compute sin (x). The computation ends when the
precision of the terms drops below a (epsilon: 𝜺 ), where the value a (epsilon: 𝜺) is input from the
keyboard. Calculate the sum of the values of the three smallest consecutive terms in a sequence and
approximate the sin(x) value (33.33 points).
Bonus Question (No partial points for this):Data is stored in an 8-bit variable called par. Bits [7-3]
represent the data, and bits [2-0] represent the checksum. For example, in 11010011, the sum of bits from 3 to 7 is 3
(1+1+0+1+0). Write a program to verify the checksum. (10 points)
Steps:
Isolate bits [7-3] (e.g., 11010).
Isolate bits [2-0] (e.g., 011) and calculate the sum.
Use a loop to calculate the checksum of bits [7-3].
Verify using bitwise operators.
Data bits [7:3]: 11010
Provided checksum [2:0]: 011
Calculated checksum: 3
Checksum valid: Yes
Electrical and Computer Engineering | 2
sin(x) = x - x³/3! + x⁵/5! - x⁷/7! + x⁹/9! - … = the sum, for n from 0 to infinity, of (-1)ⁿ x^(2n+1)/(2n+1)!. The formula is valid for all real x.