Question: Please use CodeBlocks IDE to solve. in4.txt: 1007 ME 3.8 1013 CS 3.2 1018 BME 3.75 1043 EE 3.1 1048 BME 3.7 1054 CS 3.95
Please use CodeBlocks IDE to solve.
in4.txt:
1007 ME 3.8 1013 CS 3.2 1018 BME 3.75 1043 EE 3.1 1048 BME 3.7 1054 CS 3.95 1061 CS 3.5 1025 CS 3.3 1031 ME 2.9 1038 ME 3 1100 CS 3.25
in5.txt:
1000 CS 3.7 1100 CE 3.2 1200 ME 3.0 1300 BME 3.4 1400 CS 3.5
in6.txt:
1000 CS 3.7 2000 CE 3.2 3000 ME 3.0 4000 BME 3.4 5000 CS 3.5 6000 ME 3.1 7000 CS 3.6
1.7 HW4 This homework assignment gives you the opportunity to practice file I/O, sorting/searching, and arrays of structures. HW4 (Graded out of 100) Write a program that manages students' records. Each student's record consists of a netid, a major and a GPA, maintained in a structure. The structures are elements of an array. The program will read students' data from a file and loop on displaying the following menu of choices: 1. List the top n students 2. Search for a student 3. Exit the program if the user chooses 1, the user will be prompted for the value of n, and the netid, major and GPA of the top n students will be listed, starting with the student with highest GPA and going down. If the user chooses 2. user will be prompted to enter the student's netid. If the netiD is found in a structure of the array, the array index, major and GPA of the student are displayed. If the student is not found, a "Student not found" message is displayed. if the user chooses 3, the program prints "Exiting in and terminates. 1. Additional Requirements - Make sure you comply with all the requirements to avoid losing points a) Definitions You need to define the below structure at the global level, before the main function and any function prototype. struct Student { int netID; string major; double GPA; Define these arrays in the main function (for consistency, please use the same names to make the grading easier): const int NUM_ELMTS = 18; // Original array of Student structures Student studArray [NUM_ELMTS) ; // Array sorted by GPA Student studArraysortedbyGPA [NUM_ELMTS]; // Array sorted by netID Student studArraysortedbyID [NUM_ELMTS) ; b) Initialization The structures in the array will be initialized by reading from a file, and the program prompts the user for the file name. c) File The file is a text file containing student records. A student record consists of a student's netid, followed by that student's major followed by that student's GPA. An example of student record is as follows for simplicity, assume the student's netiD is an int): 1001 CS 3.8 In the above record, the netlDis 1001, major is CS, and GPA is 3.8. You may assume the data is properly formatted and therefore there is no need for input validation when reading from the file. You may assume the data is properly formatted and therefore there is no need for input validation when reading from the file. The number of records in the file is unknown but you may assume it is at most NUM_ELMTS. d) Search for a student When the user selects choice 2 your program will perform both a linear search and a binary search, and display the result from both. Use the C++ code provided in the lectures, with the necessary adaptations. The linear search is performed on the original array, studArray[NUM_ELMTS]. The binary search can only be performed on a sorted array, but the original array is not sorted. Normally when an array is sorted, the values in the array are modified. Since we want to preserve the values of the original array (in order to do linear search on the original array), we make a copy called studArraysortedbyID, and sort the copy by netid. The binary search will be performed on studArraysortedby.D. You can use one of the sorting algorithms (bubble sort or selection sort) taught in class. Your code should also keep track of how many iterations the search went through and the main function should display the number of iterations for both linear search and binary search. e) List the top n students To list the top students, your program will sort by GPA. To preserve the original array, make a copy called studArraysortedby GPA and sort the copy. Like before, you can use one of the sorting algorithms taught in class. f) Outline of main Prompt the user for a file name. Open the file and read the data to initialize the array of structures. If the file cannot be opened, print "Could not open file" and "Exiting " and terminate. Make the copies and sort the copies Display the contents of the arrays Display the menu of choices, and perform the action selected by the user on the menu (search, list the top n students, or quit). If the user chooses "search", the main function should call both the linear search and binary search functions, and display the result of both searches, along with the number of iterations. The main function should loop on displaying the menu as long as the user does not choose to quit. If the user chooses to quit, the program should print "Exiting " and terminate. g) Display arrays GPAs should be displayed with a decimal point followed by 2 digits. h) Search functions You need to follow the conventions (function name, parameter list, return type) to pass the unit test. /* This function implements linear search. It takes the following arguments: arr: the array of Student structures, numElems: number of elements of array, value: netID search value, and nIter. nIter is a reference variable used to pass the number of iterations back to the calling function. The function returns the index of the structure where the match is found, -1 if no match is found int linear SearchID (Student arril, int numElems, int value, int EnIter) { // Function body ) /* This function implements binary search. It takes the following arguments: arr: the array of Student structures, numElems: number of elements of array, value: netID search value, and nIter. nIter is a reference variable used to pass the number of iterations back to the calling function. The function returns the index of the structure where the match is found, -1 if no match is found int binarySearchID (Student arr[], int numElems, int value, int EnIter) { // Function body } i) Sort functions You need to follow the conventions (function name, parameter list, return type) to pass the unit test. /* This function sorts the array of Student structures according to GPA void modifiedSortGPA (Student arrayi[], int size) { // Function body } /* This function sorts the array of Student structures according to netID void modifiedSortID (Student array2[], int size) { // Function body } Implementation note: To swap structures, it is not necessary to swap the structure members one by one. Instead, you may swap whole structures. For instance, to swap two structure variables struct1 and struct2: Student temp = structi; structi = struct2; struct2 = temp; j) Style Make sure you follow the style requirements in the "Homework Notes to avoid losing points. 2. Grading criteria Source code inspection (grader) *Style: 10 points (refer to the "Homework Notes for the style requirements) *Requirement: Use array of structures. Deduct 30 points if requirement not met Code compilation and execution (zylabs) *Test-1 - Display the original arrays - Output starts like output-1:10 points *Test-2-Display the arrays sorted by netID and menu - Output ends like output-2: 12 points *Test-3-List the top n students - Output ends like output-3: 12 points *Test-4 - Search for a student-Output ends like output-4: 12 points *Test-5 - Search for a non existent student - Output ends like output-5: 12 points *Test-6 - modifiedSortID unit test: 10 points *Test-7 - modifiedSortGPA unit test: 10 points *Test-8- linear search unit test 6 points *Test-9 - binary search unit test: 6 points 3. Output screens Enter file name:in4.txt Original array: index netID major 1007 ME 2 1018 BME 1048 BME 6 1061 CS 1031 ME 10 1100 CS Array sorted by GPA: GPA 3.80 3.75 3.70 3.50 2.90 3.25 index 1 3 5 7 9 netID 1013 1043 1054 1025 1038 major CS EE CS CS ME GPA 3.20 3.10 3.95 3.30 3.00 index netID major 1031 ME 2 1043 EE 4 1100 CS 1061 CS GPA 2.90 3.10 3.25 3.50 index 1 3 5 7 netID 1038 1013 1025 1048 major ME CS CS BME GPA 3.00 3.20 3.30 3.70 Array sorted by GPA: EE index netID major GPA 1031 ME 2.90 2 1043 3.10 4 1100 CS 3.25 1061 3.50 8 1018 BME 3.75 10 1054 CS 3.95 Array sorted by netID: index 1 3 5 7 9 netID 1038 1013 1025 1048 1007 major ME CS CS BME ME GPA 3.00 3.20 3.30 3.70 3.80 CS index netID major GPA 1007 ME 3.80 2 1018 BME 3.75 1031 ME 2.90 6 1043 EE 3.10 8 1054 CS 3.95 10 1100 CS 3.25 index 1 3 5 7 9 netID 1013 1025 1038 1048 1061 major CS CS ME BME CS GPA 3.20 3.30 3.00 3.70 3.50 Output-1 Array sorted by netID: major CS index 0 2 4 netID 1000 3000 5000 7000 GPA 3.70 3.00 3.50 3.60 index 1 3 5 netID 2000 4000 6000 major CE BME ME GPA 3.20 3.40 3.10 ME CS CS Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 3 Exiting Output-2 Menu of choices *********** 1 - List top n students 2 - Search on a netID 3 - Quit 1 Enter n:14 Top 11 students are: netID: 1054, major: CS, GPA: 3.95 netID: 1007, major: ME, GPA: 3.80 netID: 1018, major: BME, GPA: 3.75 netID: 1048, major: BME, GPA: 3.70 netID: 1061, major: CS, GPA: 3.50 netID: 1025, major: CS, GPA: 3.30 netID: 1100, major: CS, GPA: 3.25 netID: 1013, major: CS, GPA: 3.20 netID: 1043, major: EE, GPA: 3.10 netID: 1038, major: ME, GPA: 3.00 netID: 1031, major: ME, GPA: 2.90 Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit Exiting Output-3 Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 2 Enter netID:1013 Result of linear search: Student found at index 1, major is cs, GPA is 3.20 It took 2 iterations Result of binary search: Student found at index 1, major is cs, GPA is 3.20 It took 4 iterations Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 3 Exiting Output-4 Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 2 Enter netID:2222 Result of linear search: Student not found It took 11 iterations Result of binary search: Student not found It took 4 iterations Menu of choices ###### 1 - List top n students Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 3 Exiting Output-5 2997881460034 1.7 HW4 This homework assignment gives you the opportunity to practice file I/O, sorting/searching, and arrays of structures. HW4 (Graded out of 100) Write a program that manages students' records. Each student's record consists of a netid, a major and a GPA, maintained in a structure. The structures are elements of an array. The program will read students' data from a file and loop on displaying the following menu of choices: 1. List the top n students 2. Search for a student 3. Exit the program if the user chooses 1, the user will be prompted for the value of n, and the netid, major and GPA of the top n students will be listed, starting with the student with highest GPA and going down. If the user chooses 2. user will be prompted to enter the student's netid. If the netiD is found in a structure of the array, the array index, major and GPA of the student are displayed. If the student is not found, a "Student not found" message is displayed. if the user chooses 3, the program prints "Exiting in and terminates. 1. Additional Requirements - Make sure you comply with all the requirements to avoid losing points a) Definitions You need to define the below structure at the global level, before the main function and any function prototype. struct Student { int netID; string major; double GPA; Define these arrays in the main function (for consistency, please use the same names to make the grading easier): const int NUM_ELMTS = 18; // Original array of Student structures Student studArray [NUM_ELMTS) ; // Array sorted by GPA Student studArraysortedbyGPA [NUM_ELMTS]; // Array sorted by netID Student studArraysortedbyID [NUM_ELMTS) ; b) Initialization The structures in the array will be initialized by reading from a file, and the program prompts the user for the file name. c) File The file is a text file containing student records. A student record consists of a student's netid, followed by that student's major followed by that student's GPA. An example of student record is as follows for simplicity, assume the student's netiD is an int): 1001 CS 3.8 In the above record, the netlDis 1001, major is CS, and GPA is 3.8. You may assume the data is properly formatted and therefore there is no need for input validation when reading from the file. You may assume the data is properly formatted and therefore there is no need for input validation when reading from the file. The number of records in the file is unknown but you may assume it is at most NUM_ELMTS. d) Search for a student When the user selects choice 2 your program will perform both a linear search and a binary search, and display the result from both. Use the C++ code provided in the lectures, with the necessary adaptations. The linear search is performed on the original array, studArray[NUM_ELMTS]. The binary search can only be performed on a sorted array, but the original array is not sorted. Normally when an array is sorted, the values in the array are modified. Since we want to preserve the values of the original array (in order to do linear search on the original array), we make a copy called studArraysortedbyID, and sort the copy by netid. The binary search will be performed on studArraysortedby.D. You can use one of the sorting algorithms (bubble sort or selection sort) taught in class. Your code should also keep track of how many iterations the search went through and the main function should display the number of iterations for both linear search and binary search. e) List the top n students To list the top students, your program will sort by GPA. To preserve the original array, make a copy called studArraysortedby GPA and sort the copy. Like before, you can use one of the sorting algorithms taught in class. f) Outline of main Prompt the user for a file name. Open the file and read the data to initialize the array of structures. If the file cannot be opened, print "Could not open file" and "Exiting " and terminate. Make the copies and sort the copies Display the contents of the arrays Display the menu of choices, and perform the action selected by the user on the menu (search, list the top n students, or quit). If the user chooses "search", the main function should call both the linear search and binary search functions, and display the result of both searches, along with the number of iterations. The main function should loop on displaying the menu as long as the user does not choose to quit. If the user chooses to quit, the program should print "Exiting " and terminate. g) Display arrays GPAs should be displayed with a decimal point followed by 2 digits. h) Search functions You need to follow the conventions (function name, parameter list, return type) to pass the unit test. /* This function implements linear search. It takes the following arguments: arr: the array of Student structures, numElems: number of elements of array, value: netID search value, and nIter. nIter is a reference variable used to pass the number of iterations back to the calling function. The function returns the index of the structure where the match is found, -1 if no match is found int linear SearchID (Student arril, int numElems, int value, int EnIter) { // Function body ) /* This function implements binary search. It takes the following arguments: arr: the array of Student structures, numElems: number of elements of array, value: netID search value, and nIter. nIter is a reference variable used to pass the number of iterations back to the calling function. The function returns the index of the structure where the match is found, -1 if no match is found int binarySearchID (Student arr[], int numElems, int value, int EnIter) { // Function body } i) Sort functions You need to follow the conventions (function name, parameter list, return type) to pass the unit test. /* This function sorts the array of Student structures according to GPA void modifiedSortGPA (Student arrayi[], int size) { // Function body } /* This function sorts the array of Student structures according to netID void modifiedSortID (Student array2[], int size) { // Function body } Implementation note: To swap structures, it is not necessary to swap the structure members one by one. Instead, you may swap whole structures. For instance, to swap two structure variables struct1 and struct2: Student temp = structi; structi = struct2; struct2 = temp; j) Style Make sure you follow the style requirements in the "Homework Notes to avoid losing points. 2. Grading criteria Source code inspection (grader) *Style: 10 points (refer to the "Homework Notes for the style requirements) *Requirement: Use array of structures. Deduct 30 points if requirement not met Code compilation and execution (zylabs) *Test-1 - Display the original arrays - Output starts like output-1:10 points *Test-2-Display the arrays sorted by netID and menu - Output ends like output-2: 12 points *Test-3-List the top n students - Output ends like output-3: 12 points *Test-4 - Search for a student-Output ends like output-4: 12 points *Test-5 - Search for a non existent student - Output ends like output-5: 12 points *Test-6 - modifiedSortID unit test: 10 points *Test-7 - modifiedSortGPA unit test: 10 points *Test-8- linear search unit test 6 points *Test-9 - binary search unit test: 6 points 3. Output screens Enter file name:in4.txt Original array: index netID major 1007 ME 2 1018 BME 1048 BME 6 1061 CS 1031 ME 10 1100 CS Array sorted by GPA: GPA 3.80 3.75 3.70 3.50 2.90 3.25 index 1 3 5 7 9 netID 1013 1043 1054 1025 1038 major CS EE CS CS ME GPA 3.20 3.10 3.95 3.30 3.00 index netID major 1031 ME 2 1043 EE 4 1100 CS 1061 CS GPA 2.90 3.10 3.25 3.50 index 1 3 5 7 netID 1038 1013 1025 1048 major ME CS CS BME GPA 3.00 3.20 3.30 3.70 Array sorted by GPA: EE index netID major GPA 1031 ME 2.90 2 1043 3.10 4 1100 CS 3.25 1061 3.50 8 1018 BME 3.75 10 1054 CS 3.95 Array sorted by netID: index 1 3 5 7 9 netID 1038 1013 1025 1048 1007 major ME CS CS BME ME GPA 3.00 3.20 3.30 3.70 3.80 CS index netID major GPA 1007 ME 3.80 2 1018 BME 3.75 1031 ME 2.90 6 1043 EE 3.10 8 1054 CS 3.95 10 1100 CS 3.25 index 1 3 5 7 9 netID 1013 1025 1038 1048 1061 major CS CS ME BME CS GPA 3.20 3.30 3.00 3.70 3.50 Output-1 Array sorted by netID: major CS index 0 2 4 netID 1000 3000 5000 7000 GPA 3.70 3.00 3.50 3.60 index 1 3 5 netID 2000 4000 6000 major CE BME ME GPA 3.20 3.40 3.10 ME CS CS Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 3 Exiting Output-2 Menu of choices *********** 1 - List top n students 2 - Search on a netID 3 - Quit 1 Enter n:14 Top 11 students are: netID: 1054, major: CS, GPA: 3.95 netID: 1007, major: ME, GPA: 3.80 netID: 1018, major: BME, GPA: 3.75 netID: 1048, major: BME, GPA: 3.70 netID: 1061, major: CS, GPA: 3.50 netID: 1025, major: CS, GPA: 3.30 netID: 1100, major: CS, GPA: 3.25 netID: 1013, major: CS, GPA: 3.20 netID: 1043, major: EE, GPA: 3.10 netID: 1038, major: ME, GPA: 3.00 netID: 1031, major: ME, GPA: 2.90 Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit Exiting Output-3 Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 2 Enter netID:1013 Result of linear search: Student found at index 1, major is cs, GPA is 3.20 It took 2 iterations Result of binary search: Student found at index 1, major is cs, GPA is 3.20 It took 4 iterations Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 3 Exiting Output-4 Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 2 Enter netID:2222 Result of linear search: Student not found It took 11 iterations Result of binary search: Student not found It took 4 iterations Menu of choices ###### 1 - List top n students Menu of choices 1 - List top n students 2 - Search on a netID 3 - Quit 3 Exiting Output-5 2997881460034
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
