Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Computer Science: Structs and Arrays TEST FILE1: TEST FILE 2: CS 202 Assignment #1 Purpose: Refresh concepts regarding C++ simple I/O, arrays, functions, and variable

Computer Science: Structs and Arrays

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

TEST FILE1:

image text in transcribed

TEST FILE 2:

image text in transcribed

CS 202 Assignment #1 Purpose: Refresh concepts regarding C++ simple I/O, arrays, functions, and variable scoping, and compilation/linking. Wednesday (1/29) Due: Points: 25 Assignment: Write a program to process a file containing some formatted data for a local election. 99 LITTLE BUGS IN THE CODE 99 LITTLE BUGS TAKE ONE DOWN PATCH IT AROUND 137 LITTLE BUGS The input data file name is obtained from the command line. The input format for each line is: F=firstname, L=lastname,V=votes Read only valid lines into the array. The input file given to you is intentionally faulty. For reference, a correct line is as follows: F=John, L=Smith,V=3342 The line errors that your program needs to detect, are as follows: Incorrect token / separator. For example: IN THE CODE F=Stan,L-Smithv=4429 (comma missing). Lines with this error need to be ignored. Space in token. For example: F-Thomas, X-Anderson, V=1622. Lines with this error need to be read, fixed, data included in your data set. Empty lines should be ignored. Submission: When complete, submit the ast01.cpp source file: A copy of the source file via the class web page (assignment submission link) by class time on the due date. You may re-submit as many times as desired. As per the syllabus, late assignments will be accepts with 24 hours of the original due date with reduced points. Specifically, 2% per hour which means 48% loss in 25 hours after which case it becomes 0 points. Please ensure that you follow the Required Coding Style & Documentation Revised Spring 2020. All program submissions must have the correct file type, be named correctly, and include the required comment block at the top of the main program file. Makefile: A simple makefile is provided. Assuming the program is named ast01.cpp, the following command is how to compile and link: edled-vm$ make Which will create the executable file, ast01. Function Descriptions: The following are detailed descriptions of the required data structures. ARR_SIZE size of the array. o The number of candidates in file will not exceed the ARR_SIZE. o When iterating over the candidates' list, do not iterate over the entire array, but just over the records where data is filled in. score (votes) number of votes earned by candidate. o pScore percentage score calculated using formula (1) Declare array of structs (size ARR_SIZE): struct Candidate { first; string string last; int votes; double pScore; }; The following are detailed descriptions of the required functions. void readFile(Candidate candidates |]) reads the input file, fills the candidates[] array. Hint: use substr) and find() functions. Set pScore to 0. void displayList(Candidate candidates ]) prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking list. The filed sizes are 10 for each with two spaces between each on the display. void displayCandidate(Candidate cand) prints the complete information about the candidate. Candidate getWinner(Candidate candidates[]) returns single struct element: candidate with highest score. Candidate getLast(Candidate candidates[]) returns single struct element: candidate with lowest score. void sortByVotes(Candidate candidates[]) function sorts the candidates[] array by number of votes, the order in candidates[] array is replaced. void calculateScores(Candidate candidates[]) - calculates the percentage score for each candidate. Use the following formula: Sc X 100% S. pScore 1 where se is the score for candidate c and Cis the number of candidates void roundScore(Candidate &cand) - updates single element, passed by reference. Function is rounding the pScore (example: 74.29% is rounded to 74%, 74.64% is rounded to 75%). Example Execution An example execution of the program is as follows: edevme ./ast01 elections1.txt ALL CANDIDATES: Votes 8 Score First Last Smith John 3342 13.838 Mary Bill Blue 2003 8.298 Warren 1588 6.578 Robert Powell 5332 22.078 Laura Rose 3341 13.838 13.838 Peter Green 3343 Thomas Anderson 1622 6.718 8.298 Catherine Johnson 2004 Barbara Moore 1589 6.588 ALL CANDIDATES: 8 Score First Last Votes Bill Warren 1588 6.578 Barbara Moore 1589 6.588 Thomas Anderson 1622 6.718 Mary Catherine Blue 2003 8.298 8.298 Johnson 2004 Rose Laura 3341 13.838 John Smith Green 3342 13.838 Peter 3343 13.838 5332 Robert Powell 22.078 winner: FIRST NAME: Robert Powell LAST NAME: VOTES: 5332 8 GAINED: 22.078 lowest score: FIRST NAME: Bill LAST NAME: Warren VOTES: 1588 8 GAINED: 6.578 ALL CANDIDATES: * Score First Last Votes Bill Warren 1588 7.008 Barbara Moore 1589 7.008 Thomas Anderson 1622 7.008 Mary Blue 2003 8.008 Catherine Johnson 2004 8.008 3341 Laura Rose 14.008 John Smith 3342 14.008 Peter Green 3343 14.008 Robert Powell 5332 22.008 The I/O examples and spacing should be followed as per the example. Testing A script file to execute the program on a series of predefined inputs will be provided. Please follow the I/O examples. The test utility should be downloaded into the working directory. The script file will require execute privilege (i.e., chmod +x altst). The test script, named altst, here can be executed as follows: ede vm$ ./altst ast01 The test script compares the program output to predefined expected output (based on the example I/O). Note, the edevm% is the prompt on my machine. Code Skeleton: #include #include #include #include #include #include using namespace std; const int ARR SIZE = 100; void readFile (string, Candidate[]); void displayList (Candidate []); void sortByVotes (Candidate []); void displayCandidate (Candidate) ; Candidate getWinner (Candidate []); Candidate getLast (Candidate[]); void calculateScores (Candidate []); void roundScores (Candidate []); int main (int argc, char *argv ) { } void readFile (string inFileName, Candidate candidates []) { void displayList (Candidate candidates []) { } void sortByVotes (Candidate candidates []) { void displayCandidate (Candidate cand) { Candidate getWinner (Candidate candidates[]) { Candidate getLast (Candidate candidates []) { void calculateScores (Candidate candidates []) { } void roundScores (Candidate candidates []) { elections1 - Notepad File Edit Format View Help F=John, L=Smith,V-3342 F=Mary, L-Blue,V=2003 F=Bill,L=Warren, V=1588 F=Robert, L=Powell,V=5332 F=Stan, L=SmithV=4429 F=Laura, L=Rose,V=3341 F=Peter, L=Green, V=3343 F=Thomas, X=Anderson, V=1622 F=Catherine, L=Johnson,V=2004 F=Barbara, L=Moore, V=1589 elections2 - Notepad Format View Help File Edit F=Who, L=Doctor,V=2987 F=Bunny, L=Bugs, V=3812 F=Bear, L-Fozzie, V=2333 F=Frog, L=Kermit,V=1233 F=Jetson, L=George, V=7614 F=Sqquirel, L=Rocky,V=2731 F=Mouse, L-Minnie, V=11234 F=Boggs, L=Randell,V=2337 F=Bird, L=Tweety,V=5891 F=Sack, L-Sad, V=0034 F=Bond, L=James,V=1311 F=Waternoose, L=Henry,V=3454 F=Rubble, L=Barney, V=5714 F=Flinston, L=Fred,V=3218 F=Coyote, L=Wily,V=5614 F=Fry, L=Phillip, V-0001 F=Sulley, L=James, V=3114 F=Joel=Camel,V=1254 F=Doo, L=Scooby,V=1654 F=Martin, L-Marvin, V=5364 F=Vader, L=Darth,V=8763 F=Barbarian, L=Conan, V=4678 F=Rich, L=Richy,V=0329 F=Mouse, L=Mickey,V=5412 F=Duck, L-Daisy,V=4431 F=Simpson, L=Homer, V=0344 CS 202 Assignment #1 Purpose: Refresh concepts regarding C++ simple I/O, arrays, functions, and variable scoping, and compilation/linking. Wednesday (1/29) Due: Points: 25 Assignment: Write a program to process a file containing some formatted data for a local election. 99 LITTLE BUGS IN THE CODE 99 LITTLE BUGS TAKE ONE DOWN PATCH IT AROUND 137 LITTLE BUGS The input data file name is obtained from the command line. The input format for each line is: F=firstname, L=lastname,V=votes Read only valid lines into the array. The input file given to you is intentionally faulty. For reference, a correct line is as follows: F=John, L=Smith,V=3342 The line errors that your program needs to detect, are as follows: Incorrect token / separator. For example: IN THE CODE F=Stan,L-Smithv=4429 (comma missing). Lines with this error need to be ignored. Space in token. For example: F-Thomas, X-Anderson, V=1622. Lines with this error need to be read, fixed, data included in your data set. Empty lines should be ignored. Submission: When complete, submit the ast01.cpp source file: A copy of the source file via the class web page (assignment submission link) by class time on the due date. You may re-submit as many times as desired. As per the syllabus, late assignments will be accepts with 24 hours of the original due date with reduced points. Specifically, 2% per hour which means 48% loss in 25 hours after which case it becomes 0 points. Please ensure that you follow the Required Coding Style & Documentation Revised Spring 2020. All program submissions must have the correct file type, be named correctly, and include the required comment block at the top of the main program file. Makefile: A simple makefile is provided. Assuming the program is named ast01.cpp, the following command is how to compile and link: edled-vm$ make Which will create the executable file, ast01. Function Descriptions: The following are detailed descriptions of the required data structures. ARR_SIZE size of the array. o The number of candidates in file will not exceed the ARR_SIZE. o When iterating over the candidates' list, do not iterate over the entire array, but just over the records where data is filled in. score (votes) number of votes earned by candidate. o pScore percentage score calculated using formula (1) Declare array of structs (size ARR_SIZE): struct Candidate { first; string string last; int votes; double pScore; }; The following are detailed descriptions of the required functions. void readFile(Candidate candidates |]) reads the input file, fills the candidates[] array. Hint: use substr) and find() functions. Set pScore to 0. void displayList(Candidate candidates ]) prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking list. The filed sizes are 10 for each with two spaces between each on the display. void displayCandidate(Candidate cand) prints the complete information about the candidate. Candidate getWinner(Candidate candidates[]) returns single struct element: candidate with highest score. Candidate getLast(Candidate candidates[]) returns single struct element: candidate with lowest score. void sortByVotes(Candidate candidates[]) function sorts the candidates[] array by number of votes, the order in candidates[] array is replaced. void calculateScores(Candidate candidates[]) - calculates the percentage score for each candidate. Use the following formula: Sc X 100% S. pScore 1 where se is the score for candidate c and Cis the number of candidates void roundScore(Candidate &cand) - updates single element, passed by reference. Function is rounding the pScore (example: 74.29% is rounded to 74%, 74.64% is rounded to 75%). Example Execution An example execution of the program is as follows: edevme ./ast01 elections1.txt ALL CANDIDATES: Votes 8 Score First Last Smith John 3342 13.838 Mary Bill Blue 2003 8.298 Warren 1588 6.578 Robert Powell 5332 22.078 Laura Rose 3341 13.838 13.838 Peter Green 3343 Thomas Anderson 1622 6.718 8.298 Catherine Johnson 2004 Barbara Moore 1589 6.588 ALL CANDIDATES: 8 Score First Last Votes Bill Warren 1588 6.578 Barbara Moore 1589 6.588 Thomas Anderson 1622 6.718 Mary Catherine Blue 2003 8.298 8.298 Johnson 2004 Rose Laura 3341 13.838 John Smith Green 3342 13.838 Peter 3343 13.838 5332 Robert Powell 22.078 winner: FIRST NAME: Robert Powell LAST NAME: VOTES: 5332 8 GAINED: 22.078 lowest score: FIRST NAME: Bill LAST NAME: Warren VOTES: 1588 8 GAINED: 6.578 ALL CANDIDATES: * Score First Last Votes Bill Warren 1588 7.008 Barbara Moore 1589 7.008 Thomas Anderson 1622 7.008 Mary Blue 2003 8.008 Catherine Johnson 2004 8.008 3341 Laura Rose 14.008 John Smith 3342 14.008 Peter Green 3343 14.008 Robert Powell 5332 22.008 The I/O examples and spacing should be followed as per the example. Testing A script file to execute the program on a series of predefined inputs will be provided. Please follow the I/O examples. The test utility should be downloaded into the working directory. The script file will require execute privilege (i.e., chmod +x altst). The test script, named altst, here can be executed as follows: ede vm$ ./altst ast01 The test script compares the program output to predefined expected output (based on the example I/O). Note, the edevm% is the prompt on my machine. Code Skeleton: #include #include #include #include #include #include using namespace std; const int ARR SIZE = 100; void readFile (string, Candidate[]); void displayList (Candidate []); void sortByVotes (Candidate []); void displayCandidate (Candidate) ; Candidate getWinner (Candidate []); Candidate getLast (Candidate[]); void calculateScores (Candidate []); void roundScores (Candidate []); int main (int argc, char *argv ) { } void readFile (string inFileName, Candidate candidates []) { void displayList (Candidate candidates []) { } void sortByVotes (Candidate candidates []) { void displayCandidate (Candidate cand) { Candidate getWinner (Candidate candidates[]) { Candidate getLast (Candidate candidates []) { void calculateScores (Candidate candidates []) { } void roundScores (Candidate candidates []) { elections1 - Notepad File Edit Format View Help F=John, L=Smith,V-3342 F=Mary, L-Blue,V=2003 F=Bill,L=Warren, V=1588 F=Robert, L=Powell,V=5332 F=Stan, L=SmithV=4429 F=Laura, L=Rose,V=3341 F=Peter, L=Green, V=3343 F=Thomas, X=Anderson, V=1622 F=Catherine, L=Johnson,V=2004 F=Barbara, L=Moore, V=1589 elections2 - Notepad Format View Help File Edit F=Who, L=Doctor,V=2987 F=Bunny, L=Bugs, V=3812 F=Bear, L-Fozzie, V=2333 F=Frog, L=Kermit,V=1233 F=Jetson, L=George, V=7614 F=Sqquirel, L=Rocky,V=2731 F=Mouse, L-Minnie, V=11234 F=Boggs, L=Randell,V=2337 F=Bird, L=Tweety,V=5891 F=Sack, L-Sad, V=0034 F=Bond, L=James,V=1311 F=Waternoose, L=Henry,V=3454 F=Rubble, L=Barney, V=5714 F=Flinston, L=Fred,V=3218 F=Coyote, L=Wily,V=5614 F=Fry, L=Phillip, V-0001 F=Sulley, L=James, V=3114 F=Joel=Camel,V=1254 F=Doo, L=Scooby,V=1654 F=Martin, L-Marvin, V=5364 F=Vader, L=Darth,V=8763 F=Barbarian, L=Conan, V=4678 F=Rich, L=Richy,V=0329 F=Mouse, L=Mickey,V=5412 F=Duck, L-Daisy,V=4431 F=Simpson, L=Homer, V=0344

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

Question

How do I compute variable cost?

Answered: 1 week ago