cmpt

.pdf

School

Simon Fraser University *

*We aren’t endorsed by this school

Course

125

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

4

Uploaded by SargentEchidnaPerson2082

Report
Assignment 1 CMPT 125 Intro. To Computing Science & Programming II Fall 2023 Page 1 of 4 © Victor Cheung, 2023 Assignment 1 (5% of Course Total) Due date: 11:59pm, Sep 29, 2023 Part of the assignment will be graded automatically. Make sure that your code compiles without warnings/errors and produces the required output. Also use the file names and structures indicated as requested. Deviation from that might result in 0 mark. Your code MUST compile and run in the CSIL machines with the Makefile provided. It is possible that even with warnings your code would compile. But this still indicates there is something wrong with you code and you have to fix them, or marks will be deducted. Your code MUST be readable and have reasonable documentation (comments) explaining what it does. Use your own judgement, for example, no need to explain i += 2 is increasing i by 2, but explain how variables are used to achieve something, what a certain loop is doing, and the purpose of each #include. Description There is a total of 3 questions in this assignment. For each question, write your answer in a single file that contains your student information as comments at the top. Unless otherwise specified, do not include any pre-existing libraries in your answers . You can however write your own helper functions. Also, do not print anything unless the question asks you to. None of these files should contain the main function . Question 1 [5 marks] Write a function that “shuffles” the digits in an unsigned int number and returns the result as an unsigned int with this rule: swap the first digit with the last digit, swap the second digit with the second last digit, …etc. Use this function header: unsigned int shuffleDigits(unsigned int number) For example: shuffleDigits(1) should return 1 shuffleDigits(22) should return 22 shuffleDigits(123) should return 321 shuffleDigits(1223) should return 3221 shuffleDigits(5670) should return 765 (leading zero will not be part of the resulting number) shuffleDigits(30400) should return 403 (leading zeros will not be part of the resulting number) You can assume the number does not have leading zeros, and we will not test shuffleDigits(0). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question1.c . Do not use recursion in your answer. Question 2 [5 marks] Write a function that takes in 4 parameters: an int array, its size, the left index, and the right index; and sorts the elements between the left and right index (inclusive) in the int array in ascending order. If the left index is larger than the right index, swap them first. Then, if the left index is invalid (e.g., negative, larger that the size), use the leftmost valid index of the array; if the right index is invalid, use the rightmost valid index of the array. Use this function header:
Assignment 1 CMPT 125 Intro. To Computing Science & Programming II Fall 2023 Page 2 of 4 © Victor Cheung, 2023 void rangedSort(int array[], unsigned int size, int leftIndex, int rightIndex) For example* (suppose there is a myIntArray created as [-4, 3, -12, 0, 5, 72, 88, 128, 1, 64]): rangedSort(myIntArray, 10, -2, 3) will change myIntArray into [-12, -4, 0, 3, 5, 72, 88, 128, 1, 64] rangedSort(myIntArray, 10, 10, 7) will change myIntArray into [-4, 3, -12, 0, 5, 72, 88, 1, 64, 128] rangedSort(myIntArray, 10, 5, 5) will change myIntArray into [-4, 3, -12, 0, 5, 72, 88, 128, 1, 64] rangedSort(myIntArray, 10, 0, 9) will change myIntArray into [-12, -4, 0, 1, 3, 5, 64, 72, 88, 128] *each example works on the original myIntArray. You can assume the array has one or more elements and the size is always correct. For the sorting, you can choose between Insertion Sort and Selection Sort (state your choice in the comments). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question2.c . Do not use recursion in your answer. Question 3 [6 marks] Suppose we use a 1D array of 3 ints to represent a point (e.g., [1, 2, 1] represents (1, 2, 1)), then we can use a N-by-3 2D array to represent N points, where each row represents 1 point. In mathematics, the Euclidean distance between 2 points (x1, y1, z1) and (x2, y2, z2) is calculated as: √(? 1 − ? 2 ) 2 + (? 1 − ? 2 ) 2 + (? 1 − ? 2 ) 2 Write a function that takes in the number of rows (number of columns is fixed at 3) of a 2D array of ints, the 2D array itself, a 1D array of 3 ints; and prints the Euclidean distances from each row of the 2D array (representing 1 point) to the point represented by the 1D array. This function should also return the total distance as a float . Use this function header: float printPointsDistances(unsigned int row, int points[][3], int point[]) For example, suppose we have 4 points: (0, 0, 0), (0, 2, 0), (2, 0, 0), and (2, 2, 2), and 1 point at (2, 2, 0). Then the function will print the following to the console (distances are in 4 decimal places): Euclidean distance from (0, 0, 0) to (2, 2, 0) is 2.8284 Euclidean distance from (0, 2, 0) to (2, 2, 0) is 2.0000 Euclidean distance from (2, 0, 0) to (2, 2, 0) is 2.0000 Euclidean distance from (2, 2, 2) to (2, 2, 0) is 2.0000 It will also return the total distance (8.828427) to the caller of this function. You can assume that the 2D array will have the correct number of columns and the number of rows is correct. You can also assume that the 1D array will have the correct number of int elements (3). Do not use the pow function from the math.h library in your answer (if you do you get 0 for this question). Only include the function definition (and your helper functions, if any) in the source file and name it as a1_question3.c .
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help