Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in C++, how do i put this code toghere, Ihve tried but i keep getting errors. The first part shows you what the code shoukd
in C++, how do i put this code toghere, Ihve tried but i keep getting errors. The first part shows you what the code shoukd do. i expect the code to implement those requirements. Thank you
Lis theidoon atudents. acarchifar atudest gest the pogerin student not found mesige is cisplifrich. a) Array definition grading eaciet. constine Now ftMrs + 18: 1/ netro, wajor and CPA are parditel esreds int netiofMuliuts], / originai netto errwy parallei arrays double sockedGpabyopd [uW_Aters]. ff GPA lerrer. sorted by GeA int sorteahet fotyGPA(mM_kurs] strine sortecth Jorbuoarnumiewts]: If Lortedietrobyio is sortes by net ID, and sorteakajocbyID and sortedepabylo are parallel arrays Iint ioctedietiotyip(nim,eurrs), if netro array, worted by netio double sorkeosp Abvio [Nilineurs]; b) Amays intialization. The arrays wil be intigized by resdim from a fie, and the program prompts the user for the fle name. File The file is a teat file containing student recoeds. A studere record consias of a studenr's neti0, followed by that students majoc followed by that studentk GPA. An example of student record is as follows for b) Arayz initialization The orrays will be initialized by reading from af fle, and the program prompts the user for the file name. c) File The file is a fext file contsining student records. A student record consiets 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 smplicity, assume the student's netio is an int: 1001cs3.8 In the above record, the netiD a1001, major is CS, and GPA \&:3.3: You may assume the data is preperly formatted and therefore there is no need for input validation when reading from the flle. The rumber hot records in the file is unknown but you may assume it is at most NUM_.ELMTS. d) Selirch for a student When the user solects 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 adiaptatione. The lioear search is pertormed on the onignal array, netD[WuM__LMTS] The binary search can only be perlormed on a sated arroy, but netio is not sorted. Normally when an array is sorted, the values in the arroy are modifed 5 ince we want to preserve the values of the netio array lin onder to do linear search on the onginal arrayl we make a copy of netiD called sortedlNetiDbylD, and sort the cogy by need. The binary tearch will be performed on sortedNetibbyiD. To be abin to disiay the major and GPA of the student found, we will maintain sortedMajorbylD and sortedCPAbylD, which are the array parallel to sortedNetiobyD. That is sorted MajorbylOCD and sortedCPAbyIOD are respectively the major and the OPA of the student whose neti is sortediNetiDbyiDli You can use one of the sorting alyonithens dsubbir sort or selection sort taught in clas. However, to mainain the paralelign of the acroys, atenever you swap elments of sortedNet DbylD, you should also swag the elements at the same indices of sectedMajorby O and sorted GPAbyID: Your code should also kep track of how many iterations the search went through and the main function should display the number of iserations, for both lines search and binayy search ti) List the top n students To fist the top students, your program wil sort the CPA array by. GPA. To preserve the original GPA array, sort a copy of GPA celled zorted GPAb,GPA. To be able to display the netiD and major, you should also maintain arrays parnliei to yortedGPAby GPA called sortedMajcrbyGPA and sortealNetiDbyGPA. the before, you can une one of the soeting algorithms taught in elass, but need to maintain the parallelism. Whenever you swap slements of sorted CPAbyGPA you should also swap the element: at the same indices of sortedMayorbyGPA and sortedevetiDbyGPA You may assume the user alwoys fypes a postive integer If the user types a value of n greater than the number of students, the program should list al the students sarted by GPA, starting with the highest. 1) Outine of main Se-sure-code-vorks-c-please-make-sure-match-outputs-shown-please-write-original-f-g 108 clements at the same indices of sorted Yazceby GPA and sortedllet Dby GPA You mar assume the user always types a postive integer if the uscrtypen bvalue of n grevter than the number of stutents, the orogram should fis al the students sorted by GPA starting with the highest: fi Butine or main peroatit the uset fos tile nare. gpen the file and need the doto to initalize the netID, major and GeA arrays. If terninate Copp netio to sortedictiobgtaA, copy GPA to soetescelajcpa dos copy asjor to sartedentrobsipa and sortealojerbyura. Copy netio to sorteoverioby, copy GPA to sortedocheyio and copy fadori to sortedaspaby 10 and sortraiajorbyto Display the contents of al1 the arrays Olsplay the nens of cholces, and pertorm the action selected by the wien on the nenu (iearch, 1ist the top n stupents, or quit). It the user chooses mearchin, the main function should calz both the Jioear search and binary search finctions, and aisplay the resuit of both searches, along with the nuaber of Iterations. The main function should loop on displayleg the menu as long as the user does not choose to quit. If the ustri chaoses to quit, the procran should arint "lxitinele" and terninate. g) Display arrays GPAs should be disployed weh a decinal point followed by 2 aigis. We Search functions implenest the following functions. You muat wse the same function name, parameter liat and retum. fype to pass the unit tests. 1. This function Leplentits Iinesr zeorch on an arroy. It takes the following araupentsi arr: the array, nuatlenst nunber of elenents of array, Valuez search value, and niter, niter is a reference variable used to pass the number of iferations back to the calling function. The functlon returns the index whipre the mateh is found, -1 if no match is found y int HinearfearehID(int arrf1. int nunelens, Int velue, int infter) II Function body Y int linearsearchtD(Int arriJ, int aumelens, int value, int intter) f 1/ Function body. 1 1* This function inglenents binary search on an array. It takes the following argunents: arri: the acray, numelensti number of elements of array, valuet 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 matem is found, 1 if no match is found Int binarysearchIo (int arr[1, lit nuatelens, int value, int sniter) i. Il Function body h) Sort functions Implement the following functions. You must use the same function name, parameter list and return type to pass the unit tests: y= This functlon sorts arrayd and maintains the parallelism of array2, and array 3 Whenever elenents of array1 are swapped, the elements of array 2 . and array 3 at the sane indices are a1so swapped void modifiedsortGPA(dowble arrayd [1, int arrayz[], string array3[], int size) i) /I Function body. ). 14 This function sorts array2 and mointains the parallelism of arrayl and array 3 Whenever elements of array 2 are swapped, the eleaents of array 1 and array 3 at the same indices are also simapped + void oodifledSortiD(int array2[], double array1[], string array3[], int size) \{ /I Function body 7 i) Style Make sure you follow the style requirements in the "Homework Notes" to avoid losing points: 2 Grading enteria Source code inspection (grader) "Style: 10 points (refer to the "Homework Notes" for the style requirements): Code compilation and execution (zylabs) -Test-1 - Print error message if file does not exist - Output matches exactly output-1:2 points -Test-2 - Display the original arrays - Outputstarts like output-2 8 points -sure-code-works-c-please-make-5ure-match-outputs-shown-ple3se-write-of g1 Source code inspection (grader) "Style: 10 points refer to the "Homework Notes " for the style requirements) Code compilation and execution (zylabs) - Test-1 - Print error message if file does not exist - Output matches exactly outout-1. 2 poin - Test-2. Display the original arrays- Output starts like output-2: 8 points - Test-3 - Display the arrays sorted by GPA - Dutput centains output:-3:12 points -Test-4 - Display the arrays sorted by netiD and menu - Output ends like output-4. 12 points - Test 5 - List the top students - Output ends like output-5, 12 points - Test-6 - Search for a student. Output ends like output-6. 12 points' - Test-7 - Search for a non existent student - Qutput ends libe output-7/ 12 points - Test - 8 - modifiedsertiD unit test 5 peints - Test-9 - modifiedSortGPAtunit test: 5 points - Test-10 - Inear search unit test: 5 points -Test-11- binary search unit test: 5 points 3. OUTPUTS SCREENSHOOTS Here are the expected screenshots when the program is executed on an extemal DE. Enter file name: ine,txt Could not open file Exiting Output-1 Enter file name: inl.txt original arrays: Output-2 Arrays sorted by GPA: Output-3 Arcays sorted by netID: Anfoys sorted by netrof: Mhen of erouces 1 - Hst cop in students 2. Search on a netio 3 in puit Exiteinet Output-4 Nenu of choices 1 . Lst top n Atudents 2. Search on a netto 3 - ouit 1 Lnter ni 12 rop 20 students are: Menu of cheices 1 - uist sop n studeats 2. Searcn od a netto 1. Quit Exitine Outgut:s Henu of chalices 1. List-top a students Output-5 Menu of choices 1. - List top n studeats 2 - Search an a netio 3 - Quit 2 Enter netID: 1913 Result of linear search: Student found at index 1 , GPA is 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 2 - Search on a netio 3 - Quit 3 Exiting Output-6 Menu of choices 1 - List top n students 2 - Search on a netiD 3 - Quit 2 Enter netID: 1111 Result of Iinear search: Student not found It took 10 iterations Result of binary search: Student not found It took 4 iterotions Menu of chotces Enter net 10: 1013 Resuat of linear scarch: Student found at todex 1 , fuph is 3.20 It took 2. Lterstions Aesuat of binary search: Stuaent found at Index 3,6fn,1.23 It took. 2 iterations Menu of cholces 1. List top n-students 2. - Search on a netlo 3- Quit 3. Exiting: Outpat-6 Menu of chalces 1. List top n students 2. Search on a netto. 3 - Quit 2 Enter netri: 1113 Result of 11ncar search: Student not found It took 13 iteratioas Result of binary search: Student not found It took 4 iteratioes Menu ar choices 1 - List top n stodents: 2 . Search on a netio 3 - Quit 3 Fxiting Fel Expert Answer This solution was written by a subject matter expert. Its designed to help students lie you learn core concepts. Step-by-step Step 1/2 Here is the complete program in C++ which implements the requirements: Hinclude ciostreams \#include cistreamb \#include estring? using namespace std; const int NUM_ELMTS =18; int netID[NUM_ELMTS] string major [NUM EUMTSI: double GPA[NUM_ELMTS); double sortedGPAbyGPA[NUM ELMTS]. int sortedNetIDbyGPA[NUM ELMTS]. string sortedMajorbyGPAINUM_ ELMTS]: int sortedNetiDbyID[NUM_ELMTS]. string sorted MajorbyIDINUM ELMTS] double sortedGPAbyIDINUM_ELMTS]: VI Function to initialize the arrays by reading from the file void initializeArrays(string fileName) \& int i=0 - ifstream fileffile Namel: while (file >> net[D[i] >> major[i] GPA[D] \{ tileclosed 1/ Function to sort the arrays sortedGPAbyGPA, sortedNetiDbyGPA and sortedMajorbyGPA by GPA void sortty GPAOI 4 Copying elements from the original arrays to the sorted arrays. for (int i=0;iSGPA[] ) ( 1 file closet, A Function to sort the arrays sortedGPA GyGPA, sortedNetiDoyGPA and sortedMajorbyGPA by GPA void sortByGPAOl. 4. Copying elements from the oniginal arrays to the sorted arrays for (intin =0, i N NUM ELMTS:ir+) sortedGPA byGPA[ = GPA 1 sortedNetDDbyGPA[i] = netiD[ti sortedMajorbyGPA[D = majorld step2/2 U/ Sorting tha areaycuing helection zert Int minthaex. mintudex +1 if For (lnt j=1+24,1 Nul eumb j++)1 elninder yi thin if (ininser in i) 1/ Suppling elevests of sortedspasycDA 13 Swappline rienents of ser tealietfolycif ans wer ceolejor tyerh Shap (sortede thobges [1], sortedettobyougininies 1). imep(sertediezorbyGra[1], iortealajorbycoainiatidex]) ) y 3 3. Function to sort the arrays sortedNetiDbyiD, sorted MajorbyID and sortedGPAbyID by netiD void sortBylD0 \{ 4 Copying elements from the original arrays to the sorted arrays for (int i=0,iStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started