Questions:
Objective:
Students will display their knowledge of sorting algorithms and memory management functions (and previous course topics) in programming using a real-world example.
Problem:
You are the financial manager of a robot development design team on campus. Recently, the team sent you requests for certain parts to complete their design prototype. These part orders have been pooled into a single file, which contains the total number of different parts to be ordered on the first line. On subsequent lines, the following information is given for each different part: the part serial number, the price of ordering a single unit of that part, and the number of units to be ordered. This information is provided in that order, separated by spaces. A visualization of the file layout is shown below.
2
4913 26.41 9
8057 13.70 14
As the financial manager, you want to analyze the total price of ordering all the different parts to make sure your team stays on budget. To do this, you have decided to calculate the total price of ordering all units for each part, reorganize them in increasing total price, and store the sorted list in its own file for convenient viewing. (Hint: the total price can be determined by multiplying the price of a single part unit by the number of units to order.) This new file will have a title on the first line displaying what each column represents. The subsequent lines will provide the same information as in the original file, but containing the total price value for that part. A visualization of this layout is shown below.
Serial / Price / Units / Total
8057 13.70 14 191.80
4913 26.41 9 238.41
For this assignment, you will be both outputting the final parts list to the console to review immediately, as well as saving it to a file for later reference!
Instructions:
Write a program in C to performing the following tasks:
Open and read the first line of the file “parts.txt” to get the number of different parts.
Create an array to hold all of the parts information (including an additional column for the total prices) using the malloc function.
Read all the parts information from the file into the first three columns of your array.
For each different part, calculate the total price of ordering all units of that part and store that value in the fourth column of the array.
Output the contents of your array to the console. You can use the Example Output Code section to help you do this.
Sort the different parts in increasing order of total order price. Make sure you move all row elements when performing swaps!
Output the contents of your sorted array to the console using the format shown in the sample output below (including the heading line).
Store the finished array in a new file named “orders.txt”, with the format shown above.
Use the free function to free the memory associated with your parts array.
Comments are mandatory for this assignment. Add comments as necessary for important parts of your code, such as memory allocation, repetition or selection structures, or function calls to explain what the program is doing.
Both your output and your “orders.txt” file must match the sample below as closely as possible; otherwise, the autograding software will not be able to grade your assignment, which may affect your mark.
Sample Input & Output:
Displayed below is a sample “parts.txt” file that you can create and use for testing your program. Note that this .txt sample is also available on OnQ attached to the assignment.
8
5847 95.58 9
6162 44.94 9
3068 48.77 19
5614 34.00 15
9060 80.13 14
2292 79.38 3
8197 67.19 19
6689 66.62 3
Displayed below is a sample output based on the above file contents. Note that the “orders.txt” file created by your program will be identical to the second half of this output – containing the heading line and the sorted parts data.
5847 95.58 9 860.22
6162 44.94 9 404.46
3068 48.77 19 926.63
5614 34.00 15 510.00
9060 80.13 14 1121.82
2292 79.38 3 238.14
8197 67.19 19 1276.61
6689 66.62 3 199.86
Serial / Price / Units / Total
6689 66.62 3 199.86
2292 79.38 3 238.14
6162 44.94 9 404.46
5614 34.00 15 510.00
5847 95.58 9 860.22
3068 48.77 19 926.63
9060 80.13 14 1121.82
8197 67.19 19 1276.61
Submission Instructions:
You can either utilize the Mimir IDE found here to create and submit your program, or create your program using CLion and upload it here for grading. Your program file must be named “apsc143assign7.c” in order for your assignment to be graded. Do not include any personal information (student number, name, etc.) in your submission.
Refer to the assignment rubric on OnQ for a detailed breakdown of the grading criteria. Your submission must adhere to the assignment rules as outlined in the submission policy document for this course, which can also be found on OnQ. There is zero tolerance for plagiarism in this course. This autograding software will automatically flag potential cases of plagiarism, which will be reviewed by the instructors.
More information on both assignment submissions and the specific definition and repercussions of plagiarism can be found in the “Begin Here (About This Course)” module on OnQ in the “Assignments” and “Plagiarism” videos, respectively.
Example Output Code:
The below code can be used to output the contents of an array to the console with the required format for this assignment. Note that the parts array used here will have a number of rows equal to the integer numParts.