Hi everyone. It's my first time here, but I'll try not to be an obnoxious rookie.
I'm doing a beginning course in C in college (yeah, original, I know :p), and at the moment I'm trying to work a project. First off, I'm not trying to have someone do the dirty work for me; I just need someone to point me in the right direction. I get a little long-winded, though, so I apologize for that in advance.
First, my project involves reading from a data file. I'm reading four values at a time; these include the ID number of a candidate in an election (candID) and the number of votes for that candidate in three districts (j1, j2, and j3). I'm printing to a .txt file. On each line, I'll print the candId, j1, j2, and j3 values for each candidate and the total number of votes received for that candidate (totalScore). However, I am only listing candidates who received no more than 5 votes to a district.
Next, I'll print the total number of candidates listed (totalCands).
Finally, I will print the four candidates with the highest number of total votes (showing both the candID and totalScore for those candidates).
Here is a sample of the output I'll be aiming for:
Candidate Results
Id J1 J2 J3 totalScore
3 3 5 2 10
1 5 4 4 13
6 0 2 1 3
4 4 3 5 12
2 2 0 3 5
5 1 1 0 2
6 Total Candidates
Top 4 candidates were:
Id totalScore
1 13
4 12
3 10
2 5
Now, I know perfectly well how to scan from and print to files. However, when it comes to organizing the values and then determining the top 4 scores, I am stuck on using arrays. Granted, I guess I could dirty-code it together by placing the top totalCands values and their respective candId values into variables named top1Id, top1Total, top2Id, top2Total and so on, but that's not what I'm aiming for, and this is where my question comes in. How would I start going about organizing these things into an array? If anyone could help me past this stage, I'd be incredibly grateful. I think I can handle the "top 4 scores" thing by myself once I know how to deal with the arrays.
Here is where my code stands thus far:
#include <stdio.h>
#include <stdlib.h>
main()
{
int candId, j1, j2, j3, totalCands, totalScore, e;
FILE *inFile, *outFile;
inFile = fopen("data4.dat","r");
outFile = fopen("proj4out.txt","w");
fprintf(outFile," Candidate Results\n");
fprintf(outFile,"Id J1 J2 J3 totalScore\n");
totalCands = 0;
e = fscanf(inFile,"%d %d %d %d", &candId, &j1, &j2, &j3);
while (e == 4)
{
if (j1<=5 && j2<=5 && j3<=5)
{
totalScore = j1 + j2 + j3;
fprintf(outFile,"%2d %d %d %d %2d\n", candId, j1, j2, j3, totalScore);
totalCands++;
}
e = fscanf(inFile,"%d %d %d %d", &candId, &j1, &j2, &j3);
}
fprintf(outFile," %2d Total Candidates\n", totalCands);
fprintf(outFile,"Top 4 candidates were:\n");
fprintf(outFile,"Id totalScore\n");
}
Here's the output that code produces:
Candidate Results
Id J1 J2 J3 totalScore
3 2 1 0 3
14 1 0 0 1
13 0 0 0 0
6 3 0 0 3
12 0 3 4 7
18 0 0 5 5
8 0 2 0 2
4 5 4 3 12
8 Total Candidates
Top 4 candidates were:
Id totalScore
And here's the data file I'm reading from:
3 2 1 0
11 0 7 0
7 6 5 6
14 1 0 0
1 0 8 0
16 4 0 7
13 0 0 0
6 3 0 0
12 0 3 4
5 0 0 8
18 0 0 5
8 0 2 0
4 5 4 3
9 7 0 2
15 8 6 1