08.01.2023 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10 ■ Structures

} PersonData;

PersonData candidate[MaxCandidates];

typedef struct {

int valid, spoilt;

} VoteCount;

int main() {

void initialize(PersonData[], int, FILE *);

VoteCount processVotes(PersonData[], int, FILE *, FILE *);

void printResults(PersonData[], int, VoteCount, FILE *);

PersonData candidate[MaxCandidates+1];

VoteCount count;

FILE *in = fopen("votes.txt", "r");

FILE *out = fopen("results.txt", "w");

initialize(candidate, MaxCandidates, in);

count = processVotes(candidate, MaxCandidates, in, out);

printResults(candidate, MaxCandidates, count, out);

fclose(in);

fclose(out);

} //end main

The declarations of PersonData and VoteCount come before main. This is done so that other

functions can refer to them, without having to repeat the entire declarations. If they were declared

in main, then the names PersonData and VoteCount would be known only in main, and other

functions would have no access to them.

Now that we know how to read and process the votes, it remains only to determine the

winner(s) and print the results. We will delegate this task to the function printResults.

Using the sample data, the array candidate will contain the values shown below after all the

votes have been tallied (remember, we are not using candidate[0]).

name

numVotes

1 Victor Taylor 4

2 Denise Duncan 3

3 Kamal Ramdhan 6

4 Michael Ali 4

5 Anisa Sawh 6

6 Carol Khan 2

7 Gary Olliverie 3

299

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!