Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a complete c++ programming on main.cpp file, screenshot the code and explanation on each code, otherwise willl downvote! main.cpp Text Files : districts.txt Barryland,1,5,7
Write a complete c++ programming on main.cpp file, screenshot the code and explanation on each code, otherwise willl downvote!
main.cpp
Text Files:
districts.txt
Barryland,1,5,7 Rabbitville,1,55,12,2,654,0,3,79,711 Jelly Bean Forest,1,11,49,2,337,99,3,764,64091,4,79666,22278,5,116364,56350 Earth,1,0,1,2,45,67 New Donut,1,555,231,2,6543,8790,3,4567,444
eligible_voters.txt
Barryland,5234 New Donut,967854 Rabbitville,134567 Jelly Bean Forest,5635099 Earth,1098
Image credits: https://www.wired.com/story/the-math-behind-gerrymandering-and-wasted-votes/ Project Summary Recently gerrymandering has been a popular topic in the news and the subject of an ongoing Supreme Court case. Gerrymandering is creating voting districts in such a way that gives one party an unfair advantage over the other. Political parties do this to try to help themselves stay in power. Recently, a research group came up with a mathematical definition for what constitutes gerrymandering. For this project, you will write a code that determines whether the state's districts are gerrymandered, according to the researchers' definition. The input data about states' districts and total voters comes from two input files. Even if you disagree with this definition of gerrymandering, it is interesting to understand it better as the courts are currently debating it. You are going to write a program that can: Starter Code with Example 1. load in voting data (and display it to the screen), Input \& Output 2. search the data for a particular state, 3. plot all the districts' votes of that particular state, Full potential output and some 4. display the calculated stats for that particular state. edge cases: Sample Output Text Pay close attention to the formatting of the output, it will need to be exact to pass the test cases and receive credit. The best way to do this is through the test cases when they are available. These will highlight differences between your output and the expected output. To get started an example input and output text file are provided. Note that states that are gerrymandered have more stats printed when the "stats" command is called. See details in test cases. Only Read on Load You may only read each file once and file reading is only allowed when load is called. The data should all be stored in vector(s) (using ourvector only) to be used for the remaining commands. You will need to submit an analysis of how many vectors are created and justify how you minimized the amounts. See the ourvector analysis section for details. From the examples, you will notice that searching for the state should be case insensitive. Therefore, you will need to be consistent with capitalization during storage and search. Example Execution Here is a quick tour of one execution of the program (console input is in red): Welcome to the Gerrymandering App! Data loaded? No State: N/A Enter command: load districts.txt eligible_voters.txt Reading: districts.txt ...Maryland...8 districts total ...Pennsylvania...18 districts total Reading: eligible_voters.txt ...Alabama...3606103 eligible voters ....Wyoming...431011 eligible voters Data loaded? Yes State: N/A Enter command: search illinots Data loaded? Yes State: Illinois Enter command: stats Gerrymandered: No Wasted Democratic votes: 921521 Wasted Republican votes: 850785 Eligible voters: 8983758 Data loaded? Yes State: Illinois Enter command: plot District: 1 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDRRRRRRRRRRRRRRRRRRRRRRRRRR District: 18 DDDDDDDDDDDDDDDDDDDDDDDDDRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR Data loaded? Yes State: Illinois Enter command: exit Data Files Congressional District Data: districts.txt Each line of districts.txt contains a state name followed by district information in groups of three. The first of the three is the district number, the second is the democratic votes in that district and the third the republican votes in that district. Some states do not number the districts. For example, any state with one district has a district "number" AL. Search the text file to see for yourself. To keep things simple, you should store this single district with the number 1 instead of AL, which is also how it will print in the console. See test cases for details. Depending on the size of the state, there will be a different number of districts. For example: Arkansas, 1,63555,124139,2,103477,123073,3,0,151630,4,87742,110789 Alaska,AL, 114596,142566 Rhode Island, 1,87060,58877,2,105716,63844 Notice that the data is separated with commas (", ") instead of the spaces. We are using commas because some state names contain spaces and we want to preserve those spaces. You should use C++ strings and C++'s string library functions. You should also use C++ streams for file reading. It is recommended to use getline instead of the extraction operator ( >) for the file reading. See lecture notes and lab for file reading template code. The district numbers and the vote counts will always be integers. Number of eligible voters in each state: eligible_voters.txt Every state name in districts.txt is also in eligible_voters.txt, so you do not need to worry about a state having district data but no eligible voter data. If there is a state in eligible_voters.txt, but not contained in district.txt, that state should be ignored (should not be stored in your vector, should not be found when search is called, and should not be printed out during load). Each line of eligible_voters.txt contains a state name, followed by the eligible voter count for that state. For example: Alabama,3606103 Alaska,519501 Arizona,4738332 Arkansas, 2148441 California, 25278803 Note: Your code will be tested on other files of the same format. Do not assume that the files only have some number of lines, only have US state names, or anything else about the files other than what has been described here about their format. Two files will always be provided to loading, the first districts and the second eligible voters but the filenames may change from the examples (e.g. load 1.txt 2.txt would contain districts in 1.txt and voters in 2.txt) Note 2: Think about how you are going to store this data. Think about it...now. Don't get too deep into file reading and string parsing and calculations before reading through the rest of the project and deciding where you want to put all this. One ourvector? Multiple ourvectors? Structs? The choice is yours. No abstractions or data structures are allowed other than Qurvector. A little planning will go a long way! Calculating Gerrymandering You can determine gerrymandering for states with three or more districts by counting up and comparing the wasted votes cast for each party. For states with less than three districts, you cannot determine gerrymandering. We will define a wasted vote as any vote not needed to win the election. That means all votes for the party that loses the district seat are wasted as well as all votes for the winning party other than the "over half" they need to win the majority. The "over half" is defined as the least number of votes that is over half of the total number of votes in the district. For example, if there were 10 total votes, then 6 would be over half. If there were 9 total votes, then 5 would be over half. Having calculated this data, we can sum up the wasted votes for each district. It is impossible to make voting districts exactly fair and so we shouldn't expect the wasted vote counts to be equal. We define the efficiency gap as the difference in the totals of the wasted votes for each party, expressed as a percentage of total votes cast. NOTE: We subtract the smaller number from the larger when possible, to ensure a nonnegative efficiency gap. We could also take the absolute value of the difference. NOTE2: Total votes cast is not the same as eligible voters per state. The total votes cast must be calculated by adding all democratic and republican votes in all of a states' districts. Researchers have discovered that it is almost impossible for the disadvantaged party to recover if the efficiency gap is greater than or equal to 7 percent. Therefore, the researchers, as well as us for the purposes of this assignment, will consider a state gerrymandered when there is a 7% or greater efficiency gap. States with less than three districts cannot be gerrymandered. To check your work and calculations (and to further understand the two paragraphs above), please see the following tables for intermediate calculations for two sample states (Illinois and Connecticut). This will help you verify your calculations are being done the same as the solution More Details about plot command The "plot" command will visualize the votes for all districts in a state. As a reminder from above, it looks like this: Sample Output Text For each district, you are going to print 100 letters (either D's or R's). The D's will be on the left and the R's will be on the right. The plot is intended to look like a text-based histogram. The way you will determine how many D's and how many R's will be described with an example. For Illinois, District 1 has 162268 democratic votes and has 59749 republican votes. This means that 73%(162268/(162268+59749)=0.7308809686) of District 1 are democratic votes and 27% of District 1 are republican votes. Your plot then should print 73 D's followed by 27 R's for District 1 (which is what is shown above). In order to stay consistent with the solution, calculate the percentage of Democratic votes and always round down to the nearest integer. Then, give the remaining percentage of the votes to Republicans. NOTE: I realize rounding properly makes more sense, but this is just easier. Ourvector Analysis Open ourvectorich and go to the member function_stats. Make sure this portion of the code is not commented out: cerr "guryector stats:" endl: cerr " \# of vectors created: " Vectors endl: Gerrr " of elements inserted: " Inserts endl: Gerr " \# of elements accessed: " Accesses end]: Run your code (make sure to run all commands) with this version of ourvector. h, and you should see some vector stats print at the bottom of your code: ourvector stats: \# of vectors created:
Step 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