Purdue CS24000 Fall 2018 Midterm II Solutions
Purdue University CS24000 is an undergraduate-level course that teaches students programming principles and techniques for problem-solving in the C programming language. Here are the solution and study notes for the Fall 2018 Midterm 2 exam.
CS24000 Syllabus
Below are extracted from the Spring 2024 CS24000 course syllabus:
Disclosure: This blog site is reader-supported. When you buy through the affiliate links below, as an Amazon Associate, I earn a tiny commission from qualifying purchases. Thank you.
- Course Title: Programming in C
- Textbook: The C Programming Language (2nd Edition); Kernighan and Ritchie; March 1988
- Reference: Beej’s Guide to C Programming; Brian “Beej” Hall; 2007
- Course Outcomes: A student who successfully fulfills the course requirements will have the ability to:
- write quality code that is readable, maintainable, and well commented
- create, compile, and execute C programs using industry standard tools including the GNU Compiler Collection
- apply debugging techniques to analyze, identify, and fix errors
- assess and address security-related issues in code bases written in C
- produce code that appropriately and properly utilizes pointers
- solve problems through the application of explicit memory management
- design and implement programs in C that utilize dynamic data structures such as linked lists and trees
- Lectures:
Fall 2018 Midterm 2 Exam
Exam Solutions and Notes
Problem 1 (30 pts)
(a) Code without using array brackets:
1 | int reverse(int *source, int *dest, int n) { |
In summary, the reverse function reverses the order of elements in the source array, stores them in the dest array, and calculates the sum of the reversed elements.
(b) The atomic weight of Aluminum is 26.981.
(c) Structure for a singly-linked list node containing an integer:
1 | typedef struct single_node { |
(d) Function to prepend a node to a singly-linked list:
1 | void push(single_node_t **head, single_node_t *node) { |
(e) Function to remove the first node from a singly-linked list:
1 | single_node_t *pop(single_node_t **head) { |
Problem 2 (40 pts)
(a) Structure for a doubly-linked list node containing a string and an integer:
1 | typedef struct double_node { |
(b) Function to create a new doubly-linked list node:
1 | double_node_t *create(char *name, int age) { |
(c) Function to delete a node from a doubly-linked list:
1 | void delete(double_node_t *node) { |
(d) Function to insert a new node after a given node in a doubly-linked list:
1 | void insert(double_node_t *node, double_node_t *new_node) { |
Problem 3 (30 pts)
(a) Structure for a binary tree node:
1 | typedef struct tree_node { |
(b) The size of the tree_node_t structure on a 64-bit architecture system is 24 bytes (4 bytes for int, 1 byte for bool, and 8 bytes for each pointer).
(c) Function to mark a node as invalid:
1 | void delete_node(tree_node_t *node) { |
(d) Function to remove a node from a binary tree (assuming it's not the root):
1 | void free_node(tree_node_t *node) { |
(e) Recursive function to delete invalid nodes from a binary tree:
1 | int flush_tree(tree_node_t *root, void (*my_del)(tree_node_t *)) { |