ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 12 Weeks Ago, 6 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 11 May 2017 My Price 9.00

Assignment 6 -- Voter Participation Data

Assignment 6 -- Voter Participation Data


Instructions: For this assignment, you will write a program that reads voter participation data from a file and processes that data in several different ways, including writing a report to a different file.

Data File Format

1952
1533500
1392594
1116414
1956
1622500
1451375
1164104
...

The voter participation data we will work with is stored in a simple text file.  Each line of the file contains one of 4 different things:

  • a year
  • the estimated number of eligible voters for that year
  • the number of registered voters for that year
  • the number of ballots cast for that year

The file format is highly structured.  Years are listed in order.  On the line after each year is the estimated number of eligible voters for that year.  On the next line is the number of registered voters for that year.  On the next line is the number of ballots cast for that year.  This sequence is repeated for each year.  The first two years of data from one of the sample files are shown at right to illustrate this.  Here are links to two sample files: PresidentialElections.txt  MidTermElections.txt  [Data source: http://www.sos.wa.gov/elections/voter_participation.aspx (Links to an external site.)]

Program Specification

Write a program that does the following:

  • Allow the user to specify the name of an input file containing voter participation data (that way, different data files can be tested). 
  • Read the data from the file and write a report out to a new file as follows:
    • The name of the output file should be the name of the input file preceded with the word 'REPORT-'.  So, if the input file is named 'PresidentialElections.txt', then the output file should be named 'REPORT-PresidentialElections.txt', but if the input file is named 'MidTermElections.txt', then the output file should be named 'REPORT-MidTermElections.txt', etc.  Your program should work with any properly formatted data file -- not just the two listed here.
    • The report should include one line for each year in the data file.  The line should show a sentence containing the year, the percentage of eligible voters who registered, and the percentage of eligible voters who cast ballots (voted).  The percentages should be rounded to two places after the decimal point.  Here is an example of what the first part of the output file would look like for the data shown above:
In 1952, 90.81% registered and 72.80% voted.
In 1956, 89.45% registered and 71.75% voted.
...
  • Finally, show the following information in the Python shell window in a nicely-formatted, easy to understand report:
    • the total number of years listed
    • the total number of ballots cast in all those years
    • the average percentage of eligible voters who registered
    • thenumberof years with less than 60% of registered voters casting ballots
    • thepercentageof years with more than 80% of registered voters casting ballots
    • A sentence showing the name of the report file

Here is an example of what the report would look like for the first file:

The total number of years listed: 17

Total ballots cast in all these years: 34,436,792

Average percentage of eligible voters registered: 79.55%

Number of years with less than 60% of registered voters casting ballots: 0

Percentage of years with more than 80% of registered voters casting ballots: 47.1%

An output file named REPORT-PresidentialElections.txt has been created.

Development Tips:

  1. Use a loop to read the data from the file and process the data.  Only one loop is needed to do everything!
  2. Use acounterto count the total number of years.
  3. Usecountersalong with 'if' statements inside the loop to count the number of years with less than 60% or more than 80% of registered voters casting ballots.
  4. Use anaccumulatorto add up the number of ballots cast.  You will also need one more accumulator.
  5. Remember that counters and accumulators always need to beinitializedbefore the beginning of the loop.
  6. Plan your program carefully on paper before you start writing code.  Remember that you need to "process" the fileone record at a time.  You can't "jump around" to get the info you need like you can do as a human reader.  "Be the computer!"
    The easiest solution involves a loop that repeats one time for each YEAR.  Hint: if you have a line in the file with a year, the file format guarantees that it will be followed by three more lines containing the information for that year.  As a result, you can safely read those values into the program right in the body of the loop.

Testing -- Be sure to carefully test your program with different files.  You can create a simple, short file to make testing easy.  As always, be sure to document your testing in comments at the end of the program.  Your comments should also say if you think any part of the assignment requirements have not been met. 

Grading 

/10 correctly determines 5 quantities to be shown in the shell window: # years, # ballots counted, average percentage of eligible voters who registered, <60% years and >80% years
/12 reads and writes files properly (reads data, writes out a report file, closes files, no errors)
/3 documented test cases 
/5 proper comments; good variable names; declarations; indenting; blank lines

Answers

(11)
Status NEW Posted 11 May 2017 01:05 AM My Price 9.00

-----------

Not Rated(0)