Fall 2024 Final

Back to the exam archive

Page 1

                                      ESE124 Programming Fundamentals
                                             Final Exam - Fall 2024
Final Exam Rules: choose 3 out of 4 questions to answer, and 1 out of 2 bonus questions as a option
- You can tear pages; write your name on each page.
- Points breakdown: Coding Logic (45%), Problem-solving (40%), Code Documentation (15%).
Advice:
- Read and understand each question before you start.
- Note down thoughts and steps for partial credit.
- Questions vary in difficulty; skip around if needed.
Q1: Write a function to print a matrix in a spiral order. The matrix is
represented as a 2D array.
Spiral order refers to traversing a matrix in a spiral pattern, starting from the top-left corner and moving right, then
down, then left, then up, and so on, until all elements are visited. This pattern is often used in matrix problems to
visit all elements in a specific order.
void printSpiral(int* matrix, int rows, int cols)
Test Case for Spiral Order
Let's consider a 4x4 matrix as an example:
1  2  3  4
5  6  7  8
9 10 11 12
13 14 15 16
The spiral order traversal of this matrix would be:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Question 2: Implement a Queue using Two Stacks
Implement a queue using two stacks in C. Your implementation should support the following operations:
    ●​
         void enqueue(int x): Inserts the element x to the queue.
         int dequeue(): Removes the element from the front of the queue and returns it.
    ●​
         int peek(): Gets the front element.
    ●​
         int empty(): Returns whether the queue is empty.
    ●​
                                                                        Electrical and Computer Engineering | 1

Page 2

                                      ESE124 Programming Fundamentals
Question 3: Maximum Area of Water Container
Given a list of numbers. Each number tells you how tall a line is. These lines are drawn on a graph. Your job is to
write a function that takes two things:
A pointer to the list of numbers (the lines' heights).
The size of the list (how many lines there are).
The function should figure out which two lines can hold the most water between them and return the biggest amount
of water they can hold.
int maxArea(int* height, int heightSize)
Question 4: Finite State Machine (FSM) with Moore Machine
Implement a Finite State Machine (FSM) using a Moore machine in C. The FSM should have the following states
and transitions:
Additionally, implement a function to encode the state sequence into a bitwise representation and a function to
decode the bitwise representation back into the state sequence.
         States: STATE_A, STATE_B, STATE_C, STATE_D
    ●​
    ●​
         Inputs: 0, 1
    ●​
         Transitions:
                  STATE_A -> 0 -> STATE_B
              ○​
                                                                        Electrical and Computer Engineering | 2
Maximum-area water-container example. Detailed description follows.
Maximum-area water-container example

At indices 0 through 5, the line heights are 1, 5, 6, 3, 4, 2. The example chooses the lines at indices 1 and 4, with minimum height 4 and width 3. Maximum area = (4 - 1) × min(height[4], height[1]) = 3 × min(4, 5) = 3 × 4 = 12.

Page 3

                                      ESE124 Programming Fundamentals
                  STATE_A -> 1 -> STATE_C
              ○​
                  STATE_B -> 0 -> STATE_A
              ○​
              ○​
                  STATE_B -> 1 -> STATE_D
                  STATE_C -> 0 -> STATE_D
              ○​
                  STATE_C -> 1 -> STATE_A
              ○​
                  STATE_D -> 0 -> STATE_C
              ○​
                  STATE_D -> 1 -> STATE_B
              ○​
    ●​
         Outputs:
                  STATE_A: 0x01
              ○​
                  STATE_B: 0x02
              ○​
              ○​
                  STATE_C: 0x04
                  STATE_D: 0x08
              ○​
4
Bonus Question 2: Power of Two
Topic: Bitwise Operations
Write a function that determines whether a given integer is a power of two only using bitwise operator.
Test Case:
printf("%d\\n", isPowerOfTwo(1)); // Should print 1 (true)
printf("%d\\n", isPowerOfTwo(16)); // Should print 1 (true)
printf("%d\\n", isPowerOfTwo(218)); // Should print 0 (false)
ESE 124 - Survey Fall 2024
This brief survey aims to gather your feedback about the class and course content. We want to improve future
iterations of the course by making it more relevant, timely, and efficient. We greatly appreciate your input.
    1.​
         Please rate how helpful each course component was for your learning on a scale of 1 (not helpful) to 5
         (very helpful). If you did not use a component, circle N/A.​
          •​
                  Lectures: 1 2 3 4 5​
          •​
                  In-class discussions/quiz: 1 2 3 4 5​
                                                                        Electrical and Computer Engineering | 3

Page 4

                                      ESE124 Programming Fundamentals
          •​
                  Lab assignments: 1 2 3 4 5​
          •​
                  Lab experience on quiz: 1 2 3 4 5​
          •​
                  PDF on BS: 1 2 3 4 5​
          •​
                  Videos on BS: 1 2 3 4 5​
          •​
                  Online tools and reference on BS: 1 2 3 4 5​
    2.​
         What aspects of the course were most valuable to your learning experience?
    3.​
         Which aspects of the course were least effective or most challenging?
    4.​
         Which topics helped you most in understanding C programming fundamentals?
    5.​
         Did the course effectively teach debugging and troubleshooting skills?
    6.​
         Did the course help you understand how to write and use functions, arrays, pointers, data structure, file I/O,
         FSM and bitwise manipulation in C?​
    7.​
         What additional topics or resources would you have liked to see included?​
Please let me know if you need any clarification!
                                                                        Electrical and Computer Engineering | 4
©