Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a (write in c++) program that manages studentsrecords. Each students record consists of a netID, a major and a GPA, maintained in parallel arrays.

Write a (write in c++) program that manages studentsrecords. Each students record consists of a netID, a major and a GPA, maintained in parallel arrays. 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 students netID. If the netID is found in the netID 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.

1. Additional Requirements Make sure you comply with all the requirements to avoid losing points

a) Arrays definition

Define these arrays in the main function (for consistency, please use the same names to make the grading easier):

image text in transcribed

b) Arrays initialization

The arrays 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 students netID, followed by that students major, followed by that students GPA. An example of student record is as follows (for simplicity, assume the students netID is an int):image text in transcribed

In the above record, the netID is 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.

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. You may use the C++ code provided in the lectures, with the necessary adaptations.

The linear search is performed on the original array, netID[NUM_ELMTS].

The binary search can only be performed on a sorted array, but netID 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 netID array (in order to do linear search on the original array), we make a copy of netID called sortedNetIDbyID, and sort the copy by netID. The binary search will be performed on sortedNetIDbyID.

To be able to display the major and GPA of the student found, we will maintain sortedMajorbyID and sortedGPAbyID, which are the arrays parallel to sortedNetIDbyID. That is, sortedMajorbyID[i] and sortedGPAbyID[i] are respectively the major and the GPA of the student whose netID is sortedNetIDbyID[i].

You can use one of the sorting algorithms (bubble sort or selection sort) taught in class. However, to maintain the parallelism of the arrays, whenever you swap elements of sortedNetIDbyID, you should also swap the elements at the same indices of sortedMajorbyID and sortedGPAbyID.

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 the GPA array by GPA. To preserve the original GPA array, sort a copy of GPA called sortedGPAbyGPA. To be able to display the netID and major, you should also maintain arrays parallel to sortedGPAbyGPA called sortedMajorbyGPA and sortedNetIDbyGPA. Like before, you can use one of the sorting algorithms taught in class, but need to maintain the parallelism. Whenever you swap elements of sortedGPAbyGPA, you should also swap the elements at the same indices of sortedMajorbyGPA and sortedNetIDbyGPA.

You may assume the user always types a positive integer. If the user types a value of n greater than the number of students, the program should list all the students sorted by GPA, starting with the highest.

f) Outline of mainimage text in transcribed

g) Display arrays

GPAs should be displayed with a decimal point followed by 2 digits.

i) Search functions

Implement the following functions. You must use the same function name, parameter list and return type to pass the unit tests.image text in transcribed

h) Sort functions

Implement the following functions. You must use the same function name, parameter list and return type to pass the unit tests.image text in transcribed

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: 5 points (refer to the Homework Notes for the style requirements)
  • Code compilation and execution (zylabs) *Output test-1 - Print error message if file does not exist Output matches exactly output-1: 2 points *Output test-2 - Display the original arrays - Output starts like output-2: 8 points *Output test-3 - Display the arrays sorted by GPA - Output contains output-3: 13 points *Output test-4 - Display the arrays sorted by netID and menu - Output ends like output-4: 13 points *Output test-5 List the top n students - Output ends like output-5: 13 points *Output test-6 Search for a student- Output ends like output-6: 13 points *Output test-7 Search for a non existent student - Output ends like output-7: 13 points *Unit test-1 modifiedSortID: 5 points *Unit test-2 modifiedSortGPA: 5 points *Unit test-3 - linear search: 5 points *Unit test-4 - binary search: 5 points
  • Output screenshots

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

- in1.txt:

image text in transcribed

- in2.txt:

image text in transcribed

- in3.txt:

image text in transcribed

const int NUM ELMTS18; // netID, major and GPA are parallel arrays int netID [NUM_ELMTS] // Original netID array string major [NUM ELMTS; / Original major array double GPA [NUM ELMTS] // Original GPA array // sortedGPAbyGPA is sorted by GPA, and sortedNetIDbyGPA and sortedMajorbyGPA are parallel arrays double sortedGPAbyGPA [NUM ELMTS] // GPA array, sorted by GPA int sortedNetIDbyGPA [NUM ELMTS]; string sortedMajorbyGPA [NUM_ELMTS]; // sortedNetIDbyID is sorted by netID, and sortedMajorbyID and sortedGPAbyID are parallel arrays int sortedNetIDbyID [NUM_ELMTsi // netID array, sorted by netID string sortedMajorbyID [NUM ELMTS]; double sortedGPAbyID [NUM_ELMTS] 1001 cs 3.8 Prompt the user for a file name open the file and read the data to initialize the netID, major and GPA arrays. If the file cannot be opened, print "Could not open file" and "Exiting" and terminate. Copy netID to sortedNetIDbyGPA, copy GPA to sortedGPAbyGPA and copy major to sortedMajorbyGPA. Sort sortedGPAbyGPA by GPA, while maintaining the parallelism of sortedNetIDbyGPA and sortedMajorbyGPA Copy netID to sortedNetIDbyID, copy GPA to sortedGPAbyID and copy major to sortedMajorbyID. Sort sortedNetIDbyID by ID, while maintaining the parallelism of sortedGPAbyID and sortedMajorbyID Display the contents of all 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 /* This function implements linear search on an array. It takes the following arguments: arr the array, numElems: number of elements of array, value: 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 where the match is found, -1 if no match is found int linearsearch ID (int arr[], int numElems , nt value, int &niter) // Function body /* This function implements binary search on an array. It takes the following arguments: arr: the array, numElems: number of elements of array, value : 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 where the match is found, -1 if no match is found int binarySearchID (int arr[l, int numElems, int value, int &nIter) // Function body /*This function sorts arrayl and maintains the parallelism of array2, and array3 Whenever elements of arrayl are swapped, the elements of array 2 and array3 at the same indices are also swapped void modifiedsortGPA (double array1[, int array2, string array3 I, int size) // Function body /* This function sorts array2 and maintains the parallelism of arrayl and array3 Whenever elements of array2 are swapped, the elements of array 1 and array3 at the same indices are also swapped vold modifiedsortID(int array2[], double array![], string array3[], int size) // Function body Enter file name : n0.txt Could not open file Exiting Output-1 Enter file name: in1.txt Original arrays: index netID major GPA index netID major GPA CS 3.20 CS 3.30 ME 3.00 BME 3.70 CS 3.50 0 1007 2 1018 4 1031 6 1043 8 1054 ME 3.80 BME 3.75 2.90 EE 3.10 CS 3.95 1 1013 3 1025 5 1038 7 1048 9 1061 Output-2 Arrays sorted by GPA: index netID major GPA index netID major GPA ME 3.00 CS 3.20 CS 3.50 BME 3.75 CS 3.95 0 1031 2 1043 4 1025 6 1048 8 1007 ME 2.90 EE 3.10 CS 3.30 BME 3.70 ME 3.80 1 1038 3 1013 5 1061 7 1018 9 1054 Output-3 Arrays sorted by netID: index netID major GPA index netID majo GPA CE 3. 20 BME 3. 40 ME 3.10 0 1000 2 3000 4 5000 CS 3.70 ME 3.00 CS 3.50 1 2000 3 4000 5 6000 Menu of choices 1List top n students 2- Search on a netID 3 Quit Exiting Output-4 Menu of choices 1List top n students 2Search on a netID 3Quit Enter n: 12 Top 10 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 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 1List top n students 2Search on a netID 3- Quit Exiting Output-5 Menu of choices 1List top n students 2 -Search on a netID 3 Quit Enter netID: 1013 Result of linear search: Student found at index 1, GPA 13 3.20 It took 2 iterations Result of binary search: Student found at index 1, GPA is 3.20 It took 2 iterations Menu of choices 1-List top n students 2Search on a netID 3Quit Exiting Output-6 Menu of choices 1 - List top n students 2 - Search on a netID 3 Quit Enter netID: 1111 Result of linear search: Student not found It took 10 iterations Result of binary search: Student not found It took 4 iterations Menu of choices 1 List top n students 2- Search on a netID 3 Quit Exiting Output-7 in1 - Notepad File Edit Format View Help 1007 ME 3.8 1013 CS 3.2 1018 BME 3.75 1025 CS 3.3 1031 ME 2.9 1038 ME3 1043 EE 3.1 1048 BME 3.7 1054 CS 3.95 1061 CS 3.5 in2 Notepad File Edit Format View Help 1000 CS 3.7 2000 CE 3.2 3000 ME 3.0 4000 BME 3.4 5000 CS 3.5 in3 Notepad File Edit Format View Help 000 cs 3.7 2000 CE 3.2 3000 ME 3.0 4000 BME 3.4 5000 CS 3.5 6000 ME 3.1 const int NUM ELMTS18; // netID, major and GPA are parallel arrays int netID [NUM_ELMTS] // Original netID array string major [NUM ELMTS; / Original major array double GPA [NUM ELMTS] // Original GPA array // sortedGPAbyGPA is sorted by GPA, and sortedNetIDbyGPA and sortedMajorbyGPA are parallel arrays double sortedGPAbyGPA [NUM ELMTS] // GPA array, sorted by GPA int sortedNetIDbyGPA [NUM ELMTS]; string sortedMajorbyGPA [NUM_ELMTS]; // sortedNetIDbyID is sorted by netID, and sortedMajorbyID and sortedGPAbyID are parallel arrays int sortedNetIDbyID [NUM_ELMTsi // netID array, sorted by netID string sortedMajorbyID [NUM ELMTS]; double sortedGPAbyID [NUM_ELMTS] 1001 cs 3.8 Prompt the user for a file name open the file and read the data to initialize the netID, major and GPA arrays. If the file cannot be opened, print "Could not open file" and "Exiting" and terminate. Copy netID to sortedNetIDbyGPA, copy GPA to sortedGPAbyGPA and copy major to sortedMajorbyGPA. Sort sortedGPAbyGPA by GPA, while maintaining the parallelism of sortedNetIDbyGPA and sortedMajorbyGPA Copy netID to sortedNetIDbyID, copy GPA to sortedGPAbyID and copy major to sortedMajorbyID. Sort sortedNetIDbyID by ID, while maintaining the parallelism of sortedGPAbyID and sortedMajorbyID Display the contents of all 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 /* This function implements linear search on an array. It takes the following arguments: arr the array, numElems: number of elements of array, value: 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 where the match is found, -1 if no match is found int linearsearch ID (int arr[], int numElems , nt value, int &niter) // Function body /* This function implements binary search on an array. It takes the following arguments: arr: the array, numElems: number of elements of array, value : 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 where the match is found, -1 if no match is found int binarySearchID (int arr[l, int numElems, int value, int &nIter) // Function body /*This function sorts arrayl and maintains the parallelism of array2, and array3 Whenever elements of arrayl are swapped, the elements of array 2 and array3 at the same indices are also swapped void modifiedsortGPA (double array1[, int array2, string array3 I, int size) // Function body /* This function sorts array2 and maintains the parallelism of arrayl and array3 Whenever elements of array2 are swapped, the elements of array 1 and array3 at the same indices are also swapped vold modifiedsortID(int array2[], double array![], string array3[], int size) // Function body Enter file name : n0.txt Could not open file Exiting Output-1 Enter file name: in1.txt Original arrays: index netID major GPA index netID major GPA CS 3.20 CS 3.30 ME 3.00 BME 3.70 CS 3.50 0 1007 2 1018 4 1031 6 1043 8 1054 ME 3.80 BME 3.75 2.90 EE 3.10 CS 3.95 1 1013 3 1025 5 1038 7 1048 9 1061 Output-2 Arrays sorted by GPA: index netID major GPA index netID major GPA ME 3.00 CS 3.20 CS 3.50 BME 3.75 CS 3.95 0 1031 2 1043 4 1025 6 1048 8 1007 ME 2.90 EE 3.10 CS 3.30 BME 3.70 ME 3.80 1 1038 3 1013 5 1061 7 1018 9 1054 Output-3 Arrays sorted by netID: index netID major GPA index netID majo GPA CE 3. 20 BME 3. 40 ME 3.10 0 1000 2 3000 4 5000 CS 3.70 ME 3.00 CS 3.50 1 2000 3 4000 5 6000 Menu of choices 1List top n students 2- Search on a netID 3 Quit Exiting Output-4 Menu of choices 1List top n students 2Search on a netID 3Quit Enter n: 12 Top 10 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 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 1List top n students 2Search on a netID 3- Quit Exiting Output-5 Menu of choices 1List top n students 2 -Search on a netID 3 Quit Enter netID: 1013 Result of linear search: Student found at index 1, GPA 13 3.20 It took 2 iterations Result of binary search: Student found at index 1, GPA is 3.20 It took 2 iterations Menu of choices 1-List top n students 2Search on a netID 3Quit Exiting Output-6 Menu of choices 1 - List top n students 2 - Search on a netID 3 Quit Enter netID: 1111 Result of linear search: Student not found It took 10 iterations Result of binary search: Student not found It took 4 iterations Menu of choices 1 List top n students 2- Search on a netID 3 Quit Exiting Output-7 in1 - Notepad File Edit Format View Help 1007 ME 3.8 1013 CS 3.2 1018 BME 3.75 1025 CS 3.3 1031 ME 2.9 1038 ME3 1043 EE 3.1 1048 BME 3.7 1054 CS 3.95 1061 CS 3.5 in2 Notepad File Edit Format View Help 1000 CS 3.7 2000 CE 3.2 3000 ME 3.0 4000 BME 3.4 5000 CS 3.5 in3 Notepad File Edit Format View Help 000 cs 3.7 2000 CE 3.2 3000 ME 3.0 4000 BME 3.4 5000 CS 3.5 6000 ME 3.1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

=+What's the purpose of the piece?

Answered: 1 week ago

Question

=+What benefits are there in direct mail?

Answered: 1 week ago

Question

=+How will this product help them?

Answered: 1 week ago