Finding min and max values

Posted by user01 on Stack Overflow See other posts from Stack Overflow or by user01
Published on 2012-09-25T03:34:55Z Indexed on 2012/09/25 3:37 UTC
Read the original article Hit count: 346

Filed under:

I am trying to write a simple program that reads integers from a data file and outputs the minimum and maximum value. The first integer of the input file will indicate how many more integers will be read, and then the integers will be listed.

My program compiles without any problem, however, it returns values that are not part of the set in my test data file. Could anyone help with diagnose this issue?

int main(){
FILE *fp = fopen("data.txt", "r");
int count;
int num;
int i;
int min = 0;
int max = 0;

fscanf (fp, "%d", &count);

for (i = 0; i < count; i++)
    fscanf( fp, "%d", &i);  
    if (num < min)
        min = num;
    if (num > max)
        max = num;
fclose (fp);

printf("Of the %d integers, the minimum value is %d and the maximum value is %d \n", count, min, max);} 

© Stack Overflow or respective owner

Related posts about c

    Related posts about max