






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this past exam are: Debugging, Warmup, Problem Solving, Function Activation Records, Spheres Are Colliding, Function Accepts, Number of Dimensions
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Problem 1 (5 points): Warmup
Problem 2 (20 points): Problem Solving
Problem 3 (15 points): File I/O
Problem 4 (10 points): Debugging
Problem 5 (15 points): Function Activation Records
In this assignment, you are asked to implement a function to determine if two n-dimensional spheres are colliding.
The function accepts two vectors for the centers of spheres, orig1 and orig2, radii of the spheres, radius1 and radius2, and the number of dimensions, n, and returns 1 if the spheres are collinding or 0 if they are not colliding:
i n t c o l l i d e ( f l o a t o r i g 1 [ ] , f l o a t o r i g 2 [ ] , f l o a t r a d i u s 1 , f l o a t r a d i u s 2 , i n t n ) ;
Two spheres are colliding if the distance between their centers is less than or equal to the sum of the radii of the spheres.
As a reminder, to determine the distance between two points in n-dimensional space you may use the following formula:
distance =
(orig2[0] − orig1[0])^2 + (orig2[1] − orig1[1])^2 + ... + (orig2[n − 1] − orig1[n − 1])^2
The function should accept two arguments, array to be sorted, array, and its size, n, and should return two results: sorted array in place of the original array and the number of passes it actually took to sort the array, including the last pass needed to see that the array is already sorted.
You are also required to implement a helper function that swaps two integer values:
v o i d swap ( i n t ∗a , i n t ∗b ) ;
You are required to use this function to swap values in bubble sort() function.
i n t a r r a y [ 5 ] = { 1 , 2 , 3 , 4 , 5 } ; /∗ u n s o r t e d a r r a y ∗/ i n t nP as ses = b u b b l e s o r t ( array , 5 ) ; /∗ c a l l bubble s o r t ∗/
After this code is executed, array should be sorted in the descending order (5, 4, 3, 2, 1) and nPasses should be set to 5 indicating that it took 5 passes through the array to arrange it in the required order.
In this problem, you are asked to implement a function to save the status of a game of Chess into a file.
The file produced by this function should consist of the following fields:
An example file is provided below: Joe Bob W < 8 b y t e s o f b i n a r y data f o r row 1 > < 8 b y t e s o f b i n a r y data f o r row 2 > < 8 b y t e s o f b i n a r y data f o r row 3 > < 8 b y t e s o f b i n a r y data f o r row 4 > < 8 b y t e s o f b i n a r y data f o r row 5 > < 8 b y t e s o f b i n a r y data f o r row 6 > < 8 b y t e s o f b i n a r y data f o r row 7 > < 8 b y t e s o f b i n a r y data f o r row 8 >
You are asked to implement the following function that creates a file on disk and stores data in the described format:
In this problem, you are asked to find and fix bugs in the code that implements the insertion sort algorithm. When the code is executed, the elements in the array should be arranged such that they are in the increasing order. But because of the bugs, the code actually crashes.
Recall from lectures that insertion sort algorithm treats the array to be sorted as consisting of two parts: already sorted and yet to be sorted. Every repetition of insertion sort removes the first element from the yet to be sorted part of the array and inserts it into the correct position in the already sorted part of the array. Insertion step requires sequentially comparing the removed element with the elements from the already sorder part of the array until an appropriate place for insertion is found. The algorithm stops when the yet to be sorted part of the array becomes empty.
The logic used by the insertion sort algorithm can be demonstrated using the following example:
array[4] = { 1, 4, 3, 2 } starting array
{ 1, 4, 3, 2 } array[1] is larger than array[0], leave it in place
{ 1, 4, 3, 2 } becomes { 1, 3, 4, 2 } array[2] is less than array[1], insert it between 1 & 4
{ 1, 3, 4, 2 } becomes { 1, 2, 3, 4 } array[3] is less than array[2], insert it between 1 & 3
{ 1, 2, 3, 4 } array is now sorted
To test the code at the command line, follow this procedure:
To test the code in the LC-3 simulator, follow this procedure: