Fall 2023 Midterm

Back to the exam archive

Page 1

                                       ESE 124 Fall 2023 Midterm
ID:
First name ​ Last name
______________________________________________
Q1: 35 points (Use comments in your code to explain your solution, including algorithm - or you will lose points)
Write a C program that merges two 1D arrays into a third sorted 1 D array that contains no duplicates, then
computes and displays the sum and average of the values in the sorted array. The two 1 D arrays can have each
at most 30 decimal values and
are read from two separate input files. The third 1 D array is written in an output file.
Input example:
First 1D array 2.0 4.0 3.0 2.0
second 1D array is 8.0 5.0 7.0 2.0
Output file:
The third 1D array (result) is 2.0 3.0 4.0 5.0 7.0 8.0
Sum is: 29.0
Average is: 4.83
Steps:
    a)​
          Read Data (10 points)
         Write a C program that reads decimal numbers from two input files, "input1.txt" and "input2.txt," and stores them in
         separate arrays.
    b)​
         Store Data as 1D Array (10 points)
         Modify your program to store the decimal numbers from both files into a single 1D array with no duplicates.
         Display the contents of the 1D array.
    c)​
          Using Bubble Sort (10 points)
         Implement the bubble sort algorithm to sort the decimal numbers in ascending order within the 1D array.
         Display the sorted array.
    d)​
          Write the Solution to Output File (5  points)
         Write the sorted array, sum of the decimal numbers, and average of the decimal numbers to an output file named
         "output.txt."
         Ensure that the output file is properly formatted and includes appropriate labels for each value.
__________________________________________________________________________
Q2: 30 points (Use comments in your code to explain your solution, including algorithm - or you will lose points)
         Part 1: Design a C program to compute and display the n terms of the logarithm function ln(x) as well as their sum:
                                     ln (x) = (x-1) – (x-1)2/2 + (x-1)3/3 – (x-1)4/4 + (x-1)5/5 …
         The decimal value x and the integer value n are read from the keyboard.
              a)​
                  Define the variables and initial them (5 points).

Page 2

              b)​
                  Find the next term related to the previous term(5 points).
              c)​
                  Sum to the terms by using a loop. (5 points)
         Part 2:  Find the three consecutive terms tk, tk+1, tk+2 [ among the n terms of the ln(x) series ] that have the smallest sum
         tk + tk+1 + tk+2.
         _______________________________________________________________________
Q3: 35 points (Use comments in your code to explain your solution, including algorithm - or you will lose points)
         Design a C program that performs the following tasks:
         Read words from an input file named "input_sentence.txt".
         Read names from an input file named "names.txt".
         Create a new file named "output_sentence.txt".
         Write the sentence from “input_sentence.txt” into “output_sentence.txt”, but with the first letter of each word appearing
         in “names.txt” capitalized.
         For example, given the following input files:
         input_sentence.txt:
         Hi, james and frank are taking an exam.
         names.txt:
         james, frank, chris, anne
         The program should generate the following output file:
         output_sentence.txt:
         Hi, James and Frank are taking an exam.
         _________________________________________________
         .
Q4: Bonus 10 points
Data is provided in an 8 bits variable called par, The bits[7, 3] are data and bits[2, 0] are checksum. (eg: 11010011,
1+1+0+1+0=3).
Write a program that verifies the checksum.
    a)​
         Isolate bits [7-3] eg: 11010
         Isolate bits [2-0] eg: 011
    b)​
         Calculate checksum value of bits [7, 3] by using a loop
    c)​
         Verify only using bitwise operators.
©