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;
    fout.open("Text.txt");
    ifstream fin;
    fin.open("Lab6.txt");

    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)
    {
        count++;
        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;

    system("pause");
    return 0;
}

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)
    {
        count++;
        sum += c;
        mean = sum / count;

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

    }

nvm I got it thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.