Hey everyone

need some help again with finding the min and max from a text file. its my college lab assignment.
here is my code till now.

#include <iostream> 
#include <fstream>

using namespace std; 

int main()
    ofstream fout;
    ifstream fin;

    float c;
    int count = 0;
    float sum = 0;
    float mean = 0;
    float min = 0;
    float max = 0;
    float range = 0; //<<<<<they al have to be float because the text file has float numbers>>>>

    while (fin >> c)
        sum += c;
        mean = sum / count;

    cout << "Total Amount of Numbers: " << count << endl;
    cout << "Sum: " << sum << endl;
    cout << "Mean: " << mean << endl;

    fout << "Total Amount of Numbers: " << count << endl;
    fout << "Sum: " << sum << endl;
    fout << "Mean: " << mean << endl;

    return 0;
4 Years
Discussion Span
Last Post by xxwikkixx

need some help again with finding the min and max from a text file

And where is your logic to calculate min and max? What do you think the algorithm should be on paper ?


that the problem I cant think of it... :S...min is the smallest number in my text file and max is the largest. I am sure if you give me the algorithm I could probably figure out the code.


oh and also can u give me the formula for standard deviation like the beasic formula so i can implement in my code. the formula my professor gave me makes no sense to me


I would suggest using the ifstream operator when working with files. If, and only if, your text file is filled with numbers, than you could do it like this:
○ take first number as min
○ start going through all the numbers from the file, comparing each number with your min.
○ if you find a number smaller than your current min, you set min to that number, and keep ○ it going till you reach the end of your file.
For max is preatty much the same.


i still cant figure it out will it be like

while (fin >> c)
        sum += c;
        mean = sum / count;

        //this part below for min ???
        if(c < count)
            min == c

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.