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 .
Assignment 1 CMPT 125 Intro. To Computing Science & Programming II Fall 2023 Page 3 of 4 © Victor Cheung, 2023 Coding Style [4 marks] Your program should be properly indented, have clear and meaningful variable names (e.g., no single- letter variable names except loop iterators) and enough white space and comments to make it easy to read. Named constants should be used where appropriate. Each line of code should not exceed 80 characters. White space should be used in a consistent manner. Remember to include your information. To help you to get into the habit of good coding style, we will read your code and marks will be deducted if your code is not styled properly. Using the Makefile and Other Supplied Files The Makefile provided in this assignment is used by a command in the CSIL machines called “make” to quickly compile your code. It is especially useful if you have multiple source files. To use it, type the following command in the prompt (make sure you are in the directory with all the files of Assignment 1): $ make test1 The example above illustrates how Question 1 is compiled into an executable called “test1” when using the Makefile. Replace the “test1” with “test2”, “test3”, …etc. for other questions. You can then run the executable by typing “./test1” to test your code for Question 1. If you make changes to your code, use the make command again. You can also use “make all” if you want to compile all your code at once. The test files (test1.c, test2.c, …etc.) are provided in this assignment for you to test your code. Each typically contains a main function along with other tester functions and/or calls. You can modify them to further test your code, but do not submit these test files because we will be using our test files that are similar but not identical to grade your assignment. This makes sure that your code is not written to produce hard-coded output. The header files (question1.h, question2.h, …etc.) are there to make the compilation work. You can look at them but do not modify them. You also do not have to submit them. Submission Submit only the 3 source files indicated above (a1_question1.c, a1_question2.c, a1_question3.c) to CourSys. Refer to the corresponding Canvas assignment entry for details. Assignment late penalty: 10% per calendar day (each 0 to 24 hour period past due), max 2 days late. Academic Honesty It is expected that within this course, the highest standards of academic integrity will be maintained, in keeping with SFU’s Policy S10.01, “Code of Academic Integrity and Good Conduct.” In this class, collaboration is encouraged for in-class exercises and the team components of the assignments, as well as task preparation for group discussions. However, individual work should be completed by the person who submits it. Any work that is independent work of the submitter should be clearly cited to make its source clear. All referenced work in reports and presentations must be appropriately cited, to include websites, as well as figures and graphs in presentations. If there are any questions whatsoever, feel free to contact the course instructor about any possible grey areas. Some examples of unacceptable behavior:
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
Assignment 1 CMPT 125 Intro. To Computing Science & Programming II Fall 2023 Page 4 of 4 © Victor Cheung, 2023 Handing in assignments/exercises that are not 100% your own work (in design, implementation, wording, etc.), without a clear/visible citation of the source. Using another student's work as a template or reference for completing your own work. Using any unpermitted resources during an exam. Looking at, or attempting to look at, another student's answer during an exam. Submitting work that has been submitted before, for any course at any institution. All instances of academic dishonesty will be dealt with severely and according to SFU policy. This means that Student Services will be notified, and they will record the dishonesty in the student's file. Students are strongly encouraged to review SFU’s Cod e of Academic Integrity and Good Conduct (S10.01) available online at: http://www.sfu.ca/policies/gazette/student/s10-01.html . Use of ChatGPT or Other AI Tools As mentioned in the class, we are aware of them. I see them as helpers/tutors from which you can look for inspiration. However, these tools are not reliable and they tend to be overly confident about their answers, sometimes even incorrect. It is also very easy to grow a reliance to them and put yourself at risk of not actually learning anything and even committing academic dishonesty. Other issues include: When it comes to uncertainty, you won t know how to determine what is correct. If you need to modify or fine tune your answer, you won t know what to do. You will not be able to learn the materials and thus will not ne able to apply what you learn in situations where no external help is available, for example, during exams and interviews. For introductory level courses and less sophisticated questions, it is likely that you ll get an almost perfect answer from these tools. But keep in mind if you can get the answer, everyone can also get the answer. Bottomline is, if you ask these tools to give you an answer and you use the answer as yours, you are committing academic dishonesty by claiming work that is not done by you as yours. Note that different instructors might have different policies regarding the use of these tool. Check with them before you proceed with assignments from other courses.