Hi all!
First post here. I am having a problem with simple file scanning.
Below is what is contained in the file
Code:
3 5
84 24 25 25 2
82 96 100 13 9
23 45 77 23 12
Here is what I have so far:
Code:
# include <stdio.h>
int main()
{
// Open the input file.
FILE *ifp = fopen("input.txt", "r");
if(!ifp)
{
printf("Unable to open file\n");
return -1;
}
int num_students, num_scores, i;
fscanf(ifp, "%d", &num_students);
fscanf(ifp, "%d", &num_scores);
int min, max, j, current_score;
float weighted_score;
for (i = 0; i < num_students; i++)
{
int j, current_score, min=101, max=-1, sum=0;
for (j = 0; j < num_scores; j++)
{
fscanf(ifp, "%d", ¤t_score);
//printf("the score is: %d\n", current_score);
if (current_score < min)
{
min = current_score;
}
else if (current_score > max)
{
max = current_score;
}
sum += current_score;
//printf("The sum is: \n%d\n\n", sum);
}
//printf("The min score is:%d\nThe max score is:%d\n\n", min, max);
weighted_score = (float)(sum-max-min)/ 3;
printf("Student (number): Max Score:%d Min Score:%d Weighted Score:%.2f\n", max, min, weighted_score);
//printf("Student 2: Max Score:%d Min Score:%d Weighted Score:%.2f\n", max, min);
//printf("Student 3: Max Score:%d Min Score:%d Weighted Score:%.2f\n", max, min);
}
fclose(ifp);
system("PAUSE");
return 0;
}
In the first set of numbers (starting at 84 and ending at 2) for some reason it is not setting 84 as the maximum, instead it is setting 25 as the maximum. I tried to debug it to see if the program was skipping all first numbers altogether, but it wasn't, it just seems as if the program is skipping the first number, 84. Any idea why?