minimum number in file

Ok, the task at hand is one of finding the min, max, and average number in a file consisting of only numerals. I've gotten the average but, I'm having difficulty finding the min a max numbers.
What I've got so far (the average works, the min doesn't)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
int main()
{
    //char fileName[160];         
    FILE *inFile;               //file stream name
    double nextNum;             //num read from     
    double total = 0.0;         //total of numbers
    int numRead = 0;            //amount of numbers read 
    int min, max;  
    
    //printf("Which file do you want to count? ");
    //scanf("%s", &fileName);
    inFile = fopen("F:\\temps.txt", "r");
    if(inFile == NULL){
        printf("File could not be opened\n");  
        system("pause");
        return 1;
    }        
    while (fscanf(inFile, "%lf", &nextNum)!= EOF){
        numRead++;        
        total += nextNum;
    }
    fclose(inFile);
    if (numRead == 0)
        printf("no nums read from file\n");
    else
        printf("Total numbers read: %d\n", numRead);
        printf("Average of numbers read: %.2f\n", total / numRead);    
        
    while (fscanf(inFile, "%lf", &nextNum)!= EOF){
        if(nextNum < 120)
            nextNum = min;         
            (min > nextNum)
                min = nextNum;  
    }
    printf("The min number is: %.2f\n", min);
    system("pause");
    return 0;
}    

How do I go about finding the min? I'm sure I can figure out the max.
Just take the first number you get as the initial minimum. After that, compare all of the following numbers with the initial minimum, if any of them is smaller it becomes the new minimum.

Maximum is done the same way.

That's probably the easiest way to go about it.
Finding the minimum and the maximum are actually almost, if not exactly, the same. Try working on the max portion then see if you can't edit it to work for the min part as well.

As for your actual code, unless you want the minimum to be shown as 120 if all the numbers happen to be greater, you should initialize min to the fire number you read, then start the loop.

Just take the first number you get as the initial minimum. After that, compare all of the following numbers with the initial minimum, if any of them is smaller it becomes the new minimum.

How would I go about this? Because I've basically tried that. Even though min started a 120, I was attempting to change it to the to first nextNum less than that and compare the each number after that to it.
Here's a bit of (pseudo)code to look at:
1
2
3
4
Read number into nextNum
min = nextNum
loop reading the file
    if(nextNum < min) min = nextNum

If that's basically what you had before, post that bit and we can help debug it.
Here's what I got
1
2
3
4
5
6
7
8
9
fscanf(inFile, "%lf", &nextNum);
min = nextNum;
    while(fscanf(inFile, "%lf", &nextNum)!= EOF){
        if(nextNum < min)
            min = nextNum;      
            }
    printf("The min number is: %.2f\n", min);
    system("pause");
    return 0;

Uh yes, and doesn't that work? It should.
It doesn't, here's the entire thing maybe I made a misstep else where though I don't see one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
int main()
{
    //char fileName[160];         //array of file
    FILE *inFile;               //file stream name
    double nextNum;             //num read from     
    double total = 0.0;         //total of numbers
    int numRead = 0;            //amount of numbers read 
    int min, max;  
    
    //printf("Which file do you want to count? ");
    //scanf("%s", &fileName);
    inFile = fopen("F:\\temps.txt", "r");
    if(inFile == NULL){
        printf("File could not be opened\n");  
        system("pause");
        return 1;
    }        
    while (fscanf(inFile, "%lf", &nextNum)!= EOF){
        numRead++;        
        total += nextNum;
    }
    
    if (numRead == 0)
        printf("No numbers read from file\n");
    else
        printf("Total numbers read: %d\n", numRead);
        printf("Average of numbers read: %.2f\n", total / numRead);    

       
    fscanf(inFile, "%lf", &nextNum);
    min = nextNum;
    while(fscanf(inFile, "%lf", &nextNum)!= EOF){
        if(nextNum < min)
            min = nextNum;    
    }
    printf("The min number is: %.2f\n", min);
    fclose(inFile);
    system("pause");
    return 0;
}
   
Last edited on
Topic archived. No new replies allowed.