Engr 101 - Exam 1_key F12

3 once all the results have been read from the file

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: open files before they are used and close them when you are finished. Each line in votes.txt will appear in one of two possible formats. The first format contains two integers, where each is greater than or equal to zero, formatted as follows: <precinct> <numVotes> where: <precinct> is a precinct number whose votes are in the next numVotes lines in votes.txt <numVotes> is the number of votes cast by the voters in precinct The second possible line format contains one integer formatted as follows: <candNum> where: <candNum> corresponds to a vote for the candidate whose number is equal to candNum In order for your program to be correct, it must do the following: 1. Using iteration, continually read precinct data from votes.txt for an arbitrary number of precincts. Data from the file must be read one line at a time and you can assume that the first line of the file will be in the first format described above (i.e. <precinct> <numVotes>). 2. Each time you encounter a line in the file that corresponds to the first format from above, you must again use iteration to read the next numVotes lines from the file, each of which will correspond to the second format from above (i.e. <candNum>), and do the following for each line: a. Increment the total number of votes by 1 b. If <candNum> is a 1, increment the number of votes for candidate 1 by 1 c. If <candNum> is a 2, increment the number of votes for candidate 2 by 1 d. If <candNum> is neither a 1 nor a 2,...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online