Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

CS1337 Homework #4 Assigned Feb 9, due Feb 16 at 11:59 PM This homework assignment gives you the opportunity to practice sorting/searching and arrays of

CS1337 Homework #4 Assigned Feb 9, due Feb 16 at 11:59 PM This homework assignment gives you the opportunity to practice sorting/searching and arrays of structures. HW4 (Graded out of 100) Write a program that manages students information. Each students information consists of a netID and a GPA, maintained in a structure. The program will 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 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 and the 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) Definitions Define this structure struct Student { string netID; 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) Array initialization A netID is a string of 3 lower case letters followed by 6 digits. A GPA is a double. So that all the submissions initialize the netID and GPA to the same values, copy and paste this code in the main function: srand(100); // Set seed of random generator // Initialize the original array of Student structures for (int i = 0; i < NUM_ELMTS; i++) { studArray[i].netID = randNetID(); } for (int i = 0; i < NUM_ELMTS; i++) { studArray[i].GPA = randGPA(); } Add these functions: /* This function generates a random netID a netID is a string of 3 lower case letters followed by 6 digits */ string randNetID() { string res =""; for (int i = 0; i < 3; i++) res.append(1, randLetter()); for (int i = 0; i < 6; i++) res.append(1, randDigit()); return res; } /* This function generates a random lower case letter as a char */ char randLetter() { return static_cast(rand()% 26 + 'a'); } /* This function generates a random digit as a char */ char randDigit() { return static_cast(rand()% 10 + '0'); } /* This function generates a random GPA between 2.0 and 4.0 */ double randGPA() { return (rand()% 201 + 200)/100.0; } c) 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 studArraysortedbyID. 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 display the number of iterations, for both linear search and binary search. d) 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 studArraysortedbyGPA and sort the copy. Like before, you can use one of the sorting algorithms taught in class. e) Outline of main Initialize the studArray, using code provided Make the copies and sort the copies 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). Loop as long as the user does not choose to quit f) Display arrays You are not required to duplicate exactly the screenshots, but you should display the elements in tabular form. GPAs should be displayed with a decimal point followed by 2 digits. g) Sort functions Implement the following functions: /* This function sorts the array of Student structures according to GPA */ void modifiedSortGPA(Student array1[], int size) { // Function body } /* This function sorts the array of Student structures according to netID */ void modifiedSortID(Student array2[], int size) { // Function body } h) Search functions The search functions should return the index if the element is found, -1 otherwise. In addition, the search functions should keep track of the number of iterations and pass that information back to main for display. Use a reference variable to pass the number of iterations back to the main function. i) Style Make sure you follow the style requirements in the Homework Notes to avoid losing points. 2. Expected Output To test your program, type the same input as in the screenshots below, and check the output of your program matches the screenshots. To save on typing, you can copy and paste from 1337-HW4-Input. Continued below Continued below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions