Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FILES BELOW: INPUT FILE 2: CS 202 - Assignment #1 Purpose: Refresh concepts regarding C++ simple 10. arrays, functions, and variable scoping, and compilation/linking. Wednesday

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

FILES BELOW:

image text in transcribed

INPUT FILE 2:

image text in transcribed

CS 202 - Assignment #1 Purpose: Refresh concepts regarding C++ simple 10. 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. The input data file name is obtained from the command line. The input format for each line is: F=firstname, L=lastname, v=votes 99 LITTLE BUGS IN THE CODE 99 LITTLE BUGS TAKE ONE DOWN PATCH IT AROUND 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: 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. IN THE CODE 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: ed@ed-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. o 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 { string first; 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 (1) - 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 pScore = X 100% ES=1 Sc where so is the score for candidate c and C is 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: ed@vm ./ast01 elections1.txt ALL CANDIDATES: First Last Votes & Score ---------- John Smith 3342 13.838 Mary Blue 2003 8.298 Bill Warren 1588 6.578 Robert Powell 5332 22.078 Laura Rose 3341 13.838 Peter Green 3343 13.838 Thomas Anderson 1622 6.718 Catherine Johnson 2004 8.298 Barbara Moore 1589 6.588 ALL CANDIDATES: Pirst Last Bill Barbara Thomas Mary Catherine Laura John Peter Robert Warren Moore Anderson Blue Johnson Rose Smith Green Powell Votes ----- 1588 1589 1622 2003 2004 3341 3342 3343 5332 * Score ------- 6.578 6.58% 6.718 8.298 8.298 13.838 13.838 13.838 22.078 winner: FIRST NAME: Robert LAST NAME: Powell VOTES: 5332 * GAINED: 22.078 lowest score: FIRST NAME: Bill LAST NAME: Warren VOTES: 1588 % GAINED: 6.57% ALL CANDIDATES: Pirst Last Votes Bill Barbara Thomas Mary Catherine Laura John Peter Robert Warren Moore Anderson Blue Johnson Rose Smith Green Powell 1588 1589 1622 2003 2004 3341 3342 3343 5332 8 Score -------- 7.008 7.008 7.008 8.008 8.008 14.008 14.008 14.008 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: ed@vm$ ./altst ast01 The test script compares the program output to predefined expected output (based on the example I/O). Note, the ed@vm 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[]) { elections - 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 File Edit Format View Help 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 FrMouse, 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 to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

What is an asset? Give three examples. AppendixLO1

Answered: 1 week ago

Question

What processes are involved in perceiving?

Answered: 1 week ago