






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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 normalize an n-dimensional vector.
The function accepts one input vector, vector, one output vector, result, and the number of dimensions, n:
v o i d n o r m a l i z e ( f l o a t v e c t o r [ ] , f l o a t r e s u l t [ ] , i n t n ) ;
Recall from MP4 that a normalized vector has a length of one. Thus, to normalize a vector, one must divide each component of the vector by the length of the original vector.
To determine the length of the vector you may use the following formula:
length =
vector[0]^2 + vector[1]^2 + ... + vector[n − 1]^2
f l o a t s t a r t s [ 4 ] = { 3. 0 , 5. 3 , 2. 7 , 5. 4 } ; f l o a t ends [ 4 ] = { 4. 0 , 6. 1 , 5. 4 , 5. 5 } ; i n t n I n s = i n t e r s e c t s e g m e n t s ( 4 , s t a r t s , ends ) ;
After this code is executed, nIns should be set to 3 indicating that 3 pairs of line segments intersect. (For this example, intersecting pairs of segments are 1 and 3, 2 and 3, and 2 and 4).
In this problem, you are asked to implement a function to save data about an array of polygons into a file. Each polygon is comprised of 4 vertices whose coordinates are stored in two 2D arrays.
The file produced by this function should consist of the following fields:
An example file is provided below:
3 1 2. 3 0 3 2. 1 0 4. 0 0 5. 0 0 1. 0 0 7. 4 0 6. 0 0 4. 5 6 1 0. 0 0 1. 0 0 1 1. 0 0 0. 5 0 1 2. 0 0 0. 0 0 2. 1 2 3. 1 4 3 1 2. 5 0 1 7. 0 0 5. 5 5 5. 0 1 4. 4 4 4. 0 0 1 2. 1 2 0. 1 1
You are asked to implement the following function that creates a file on disk and stores data in the described format:
i n t s a v e p o l y g o n s ( c o n s t char ∗ f i l e n a m e , i n t num polygons , f l o a t x [ ] [ 4 ] , f l o a t y [ ] [ 4 ] ) ;
The function should accept 4 arguments: the name of the file to be saved to, file name, number of the polygons to be saved, num polygons, a 2D array of all the x coordinates of
In this problem, you are asked to find and fix bugs in the code that implements the bubble 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 does not behave as expected.
Bubble sort works by repeatedly marching through the array to be sorted, comparing each pair of adjacent elements of the array, and swapping them if they are in the wrong order. This process is repeated until no swaps are needed, which indicates that the array is already sorted in the desirable order.
The logic used by the insertion sort algorithm can be demonstrated using the following example:
array[4] = { 1, 4, 3, 2 } starting array
Iteration 1
{ 1, 4, 3, 2 } array[1] is greater than array[0], leave them in place
{ 1, 4, 3, 2 } becomes { 1, 3, 4, 2 } array[2] is less than array[1], swap them
{ 1, 3, 4, 2 } becomes { 1, 3, 2, 4 } array[3] is less than array[2], swap them
Iteration 2
{ 1, 3, 2, 4 } array[1] is greater than array[0], leave them in place
{ 1, 3, 2, 4 } becomes { 1, 2, 3, 4 } array[2] is less than array[1], swap them
{ 1, 2, 3, 4 } array[3] is greater than array[2], leave them in place
At this point, the array is already sorted, but the algorithm does not know this. One more pass through the array is needed to see that no swaps are occurring, which indicates that the array is already sorted in the desired order.
To test the code at the command line, follow this procedure:
To test the code in the LC-3 simulator, follow this procedure: