View the step-by-step solution to:

# The results from the mayor's race have been reported by each precinct as follows:

The results from the mayor’s race have been reported by each precinct as follows:

Precinct Candidate A Candidate B Candidate C Candidate D
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 408 39
5 267 13 382 29

Write a program to do the following:
a. Display the table with appropriate labels for the rows and columns.
b. Compute and display the total number of votes received by each candidate and the percentage of the
c. If any one candidate received over 50 percent of the votes, the program should display a message
declaring that the candidate the winner.
d. If no candidate received 50 percent of the votes, the program should display a message declaring a
runoof between the two candidates receiving the highest number of votes; the two candidates should
be identified by their letter names.
e. Run the program once with the data shown and once with candidate C receiving only 108 votes in
Precinct 4.‖

Additional specifications: The data will be stored in a file that will be passed as the only command line
parameter. The format of the file will match that of the table above, except that the labels will not be in the file.
Instead of presenting the candidates in alphabetical order, they should be presented from the largest total to
smallest. You must write a sort() function that sorts the election matrix based on the total votes of each
may have nothing in it but variable declarations, the four function calls, and the return statement. There will
always be five precincts and four candidates. Your output should match mine exactly. As a reminder, here is
a way to test for differences between your output and mine.

election election1.txt > mine.txt
~davis/30/p6/election.out election1.txt > seans.txt
diff mine.txt seans.txt

Hints: By making the number of rows in your matrix one larger than needed, you can store the total votes for
each candidate in the extra row. Write show_table() before writing sort() so that you can make sure you are
reading in the values correctly. Because you will re-ordering the columns in the matrix, you need a way to
keep track of the candidate letters while you are sorting so your labels are correct. Have a char array, names,
that contains 5 elements that are initially ―ABCD‖. As you swap the columns in the matrix in your sort()
function, swap the letters in names at the same time. You can then use your names array to help print the
Candidate labels.

[ p6]\$ cat election1.txt
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 408 39
5 267 13 382 29
[ p6]\$ election
[ p6]\$ election election1.txt
Candidate Candidate Candidate Candidate
Precinct C A B D
1 206 192 48 37
2 312 147 90 21
3 121 186 12 38
4 408 114 21 39
5 382 267 13 29
Total: 1429 906 184 164

Candidate C is the winner.
[ p6]\$
[ p6]\$ cat election2.txt
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 108 39
5 267 13 382 29
[ p6]\$ election election2.txt
Candidate Candidate Candidate Candidate
Precinct C A B D
1 206 192 48 37
2 312 147 90 21
3 121 186 12 38
4 108 114 21 39
5 382 267 13 29
Total: 1129 906 184 164

Candidates C and A will have a runoff.
[ p6]\$
[ p6]\$ cat election3.txt
1 192 48 206 37
2 147 90 312 21
3 186 12 121 38
4 114 21 108 39
5 267 13 82 29
[ p6]\$
[ p6]\$ election election3.txt
Candidate Candidate Candidate Candidate
Precinct A C B D
1 192 206 48 37
2 147 312 90 21
3 186 121 12 38
4 114 108 21 39
5 267 82 13 29
Total: 906 829 184 164

Candidates A and C will have a runoff.
[ p6]\$

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

### -

Educational Resources
• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents