Question
To elect a president the US uses the electoral college. In this method, individual voters of a state vote for a candidate (we will assume
To elect a president the US uses the electoral college. In this method, individual voters of a state vote for a candidate (we will
assume only 2 candidates). For each state, the candidate with the most individual (popular) votes will win the states electoral
votes. Each state has some number of electoral votes. For example, California has 55 electoral votes. If Clinton gets 100,000
popular votes while Trump gets 90,000 votes in California then Clinton gets all 55 electoral votes from California since she had
more popular votes. The winner is the candidate who has the most electoral votes (not necessarily total/popular votes).
Your task for this problem is to find the results of an election that follows this process.
Your program should determine the winner and output the following information.
1. The winner of the election (most electoral votes).
2. The total electoral votes for each candidate
3. The totals of the popular vote (raw individual votes) for each candidate.
4. Identify the winning candidate's best state (i.e. in which state they did the best, and what percent of the vote they received
for that state)
5. Identify the losing candidate's best state and percentage they received for that state
Note: A few other countries use this technique, not just the US. Thus there could be any number of statesnot just 50 or 51
(Washington D.C. is the 51 electoral "state").
We have provided a small test input with an election between "Trojan" and "Bruin" so that you can easily debug your code on a
small problem. We also provide the data from the 2012 election. However, your code should work on ANY number of states no
matter how many or how few. You cannot assume it is just 51 states at most.
Input files and their format
You will be given two files: one that describes the electoral votes and one that has popular vote totals for each state.
The electoral college file gives the number of states, the state names, and how many electoral votes each state has. The
voting data file assumes the same number of states, N, as the electoral file and shows the candidate names and how many
votes each candidate got in each state.
Explanation for Example Files Above and Sample Output
In the above files, we see that Trojan had more votes in BayArea and SouthernCal , while Bruin had more in
OtherPlace and Westwood . Thus Trojan gets the 17+13=30 electoral votes from BayArea and SouthernCal ,
while Bruin gets the 14+12=26 electoral votes from Westwood and OtherPlace . Thus, Trojan wins the election.
We will provide the filenames at the command line (electoral file first) and run your program as:
$./election elec_college1.txt vote_totals1.txt
The correct output for this set of programs is:
Trojan defeated Bruin in the electoral college.
The electoral vote count was 30 votes to 26 votes.
The popular vote total was 1050 to 925
Trojan's best state was SouthernCal where they won 75 percent of the votes
Bruin's best state was Westwood where they won 86.6667 percent of the votes
Requirements
- We will provide you with the following struct in the skeleton code:
// Stores a state's name and how many
// electoral votes that state has
struct StateElectoralInfo{
string state;
int elecVotes;
};
- You should read in the state names and each state's electoral votes into an array of these structs. That array must be
dynamically allocated.
- The states may/will be in a different order in the Electoral College file vs. the voting file. Therefore, when you read in a
state's total/popular votes you'll need to go find how many electoral votes that state has. To help perform this task, you
should implement the following function and then call it as needed:
int getStateElecVotes(StateElectoralInfo state_info[],
int len,
string stateName)
You will need this function to find the number of electoral votes that a given state (string StateName) has by searching in
the state_info[] array (of size len) of structs and returning the correct number of electoral votes. We will test this function
without your main(), so you cannot skip this function by reproducing the functionality inside your main().
- You may assume the file formats will be correct (i.e. we won't put stray characters or error-filled input values). However, you
must check that the files we provide on the command actually exist and can be opened. If any file cannot, you
should output one of the error messages below (based on which file could not be opened) and exit the program by returning
1. Electoral college file not found. or
Voter totals file not found.
- Complete the rest of the main() function. We declare variables at the top of main() that you must fill in with the
correct answers. Then we provide code at the bottom to do ALL the output (couts) other than error messages if a file does
not exist. Please ensure your code places the correct answers in the variables we have declared already.
- You should delete any dynamically allocated memory before the program quits
Tips & Hints
1. When reading data from the files, you should NOT need getline() . It can be done more simply.
2. If you store the electoral college info in an array of StateElectoralInfo structs, then you should NOT need to store
the voter totals in an array. You should be able to just read in the voter data and process it immediately.
Assumptions
- Neither the candidate names, nor the state names will include spaces (all one word). This will help you parse the input files
more simply.
- You may use C++ strings.
- There will be no draws (i.e. equal votes for both candidates) in either the electoral or popular vote.
Electoral File Format Voter File Format
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