Computer Science 1511
Computer Science I

Programming Assignment 7
Array Processing (35 points)
Due Monday, December 6, 1999

Introduction

The VotesRUs company needs some help. They have been hired by a state organization to count its election votes. Unfortunately they don't know how to proceed, so they've hired you. Your job is to write a program to figure out which candidate wins each election.

The Problem

For each state, voters will be voting for representatives and senators. Each state is apportioned into districts. There will be between 2 and 6 districts in a state (VotesRUs plans to take on bigger states after they get this program working for the small states). For each district there will be 2 candidates for representative (numbered 1 and 2). For example, a state may have 4 districts, in which case a voter lives (and votes) in one of the 4 districts and votes for a representative for that district. The candidates for each district's representative are separate (candidate 2 for representative in district 1 is different from candidate 2 in district 3).

Each voter also votes for one of the two senate positions in that state. Each senate race has 2 candidates (numbered 1 and 2). All of the voters in a district vote in only one senate race. Furthermore, the districts are divided up by which senate race they vote in, for example, in the state with 4 districts the voters in districts 1 and 4 might vote in the first senate race while districts 2 and 3 vote in the second senate race. An election is a tie if both candidates receive the same number of votes. If either candidate receives more votes they are the winner.

The data for each election comes in a file. The first line of the file indicates how many districts there are in the state (it will be an integer from 2 to 6). Following that is a line containing a single number for each district. Each of these lines indicates which senate race a district votes in (1 for the first senate race and 2 for the second). For example, if a state had 5 districts and districts 2, 3, and 5 voted in senate race 1 and districts 1 and 4 votes in senate race 2 the first 6 lines of the file would be:

5
2
1
1
2
1

Following the information regarding the districts will be one or more vote lines. Each vote line will have the following form:

District# Rep# Senator#

Where the District# is the number of the district the vote was made in, the Rep# is either 1 or 2 indicating whether the voter picked candidate 1 or 2 as the representative for that district and the Senator# is a 1 or 2 indicating whether the voter picked candidate 1 or 2 for the senate race that voter votes in. For example, using the information above (5 districts, 2, 3, and 5 vote for senate race 1 and 1 and 4 vote for senate race 2) we might get the following votes:

2 1 1
4 2 1
5 1 2
3 2 1

The first vote is for representative candidate 1 in district 2 and senate candidate 1 in the first senate race. The second vote is for representative candidate 2 in district 4 and senate candidate 1 in the second senate race. The third vote is for representative candidate 1 in district 5 and senate candidate 2 in the first senate race. The fourth vote is for representative candidate 2 in district 3 and senate candidate 1 in the first senate race.

No maximum is set for the number of votes. The last line in the file will be a line with three -1 values:

-1 -1 -1

This indicates the last vote has been tallied.

Sample Input and Output

6
1
2
2
1
1
2
1 2 1
3 1 2
4 1 1
5 2 1
2 2 1
1 1 1
2 2 2
3 2 2
4 2 2
5 1 1
1 1 2
2 2 1
3 1 2
4 2 1
5 1 2
1 2 2
2 1 1
3 1 2
4 2 1
5 1 2
-1 -1 -1

For this data your program should produce the following response:

Vote Results:
                Votes
  Reps.:        C1 C2
    District 1:  2  2   Candidates 1 and 2 are tied.
    District 2:  1  3   Candidate 2 wins!!
    District 3:  3  1   Candidate 1 wins!!
    District 4:  1  3   Candidate 2 wins!!
    District 5:  3  1   Candidate 1 wins!!
    District 6:  0  0   No votes tallied.  Check polling areas.
  Senators:     C1 C2
    District 1:  7  5   Candidate 1 wins!!
    District 2:  3  5   Candidate 2 wins!!

NOTE: for the purposes of this program you may assume the data does not contain errors.

How To Proceed

The key to this program is figuring out how you are going to represent all of the data. You can use two two-dimensional arrays to count the votes for representative elections and senate elections. The question then becomes how to update this array. For a district the update is easy, figure out which district (row) of the table the vote belongs in and update the proper candidates vote total (column). For senate votes, you need to figure out which senate race is involved. Note that each district votes in only one of the senate races. If at the beginning of the program you store the number of the senate race a district votes in in an array by district number, when you get a vote in a district you can look up that districts senate race by consulting your array.

What To Hand In

Hand in a full lab report for your program. Also show output for your program on several different election files. Make sure to test the different things that can occur in an election.

Extra Credit

2 points Allow for the possibility that you may get errors in the votes (you might get a vote for a non-existent district or a vote for a candidate not numbered 1 or 2 in a race). For such votes you should discard the entire vote and add a line at the end of your output indicating how many votes were discarded.

2 points Allow a file to contain one of more states data. (After one states data ends, another starts). Number the election results by state (State 1 Results, State 2 Results) and report results for each state in the file. For this you may assume that the data ends when you read in a -1 as the number of districts in a state.

3 points Allow the number of districts in a state to be arbitrary. You will need to do this using dynamic memory allocation to receive the extra points.