08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 ■ Arrays

Your output should specify the total number of votes, the number of

valid votes, and the number of spoilt votes. This is followed by the votes

obtained by each candidate and the winner(s) of the election.

Suppose you are given the following data in the file, votes.txt:

Victor Taylor

Denise Duncan

Kamal Ramdhan

Michael Ali

Anisa Sawh

Carol Khan

Gary Olliverie

3 1 2 5 4 3 5 3 5 3 2 8 1 6 7 7 3 5

6 9 3 4 7 1 2 4 5 5 1 4 0

Your program should send the following output to the file, results.txt:

Invalid vote: 8

Invalid vote: 9

Number of voters: 30

Number of valid votes: 28

Number of spoilt votes: 2

Candidate

Score

Victor Taylor 4

Denise Duncan 3

Kamal Ramdhan 6

Michael Ali 4

Anisa Sawh 6

Carol Khan 2

Gary Olliverie 3

The winner(s):

Kamal Ramdhan

Anisa Sawh

We need to store the names of the 7 candidates and the votes obtained by each. We will use

an int array for the votes. In order to work naturally with candidates 1 to 7, we will write the

declaration

int vote[8];

and use vote[1] to vote[7] for counting the votes for the candidates; vote[c] will hold the count

for candidate c. We will not use vote[0].

236

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!