Finding Min and Max numbers in a txt file

Viewed 814

See the below photo for reference:

pic1

I have a problem in finding the min and max number in .txt file. The number is stored in a file as one number per line. The Programm should go through these numbers and find the biggest and smallest number.

Update: I solve this problem, but I face another issue due to the type of numbers. Let say I have the following numbers: 0005.00 0005.23 52340.53 0000.01 0111.10 0001.00 2523.00

How can I get the right answers?

#include<fstream>
#include<cstdlib>
#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
int counter=0, number;
float  sum = 0, average=0;
char file_name [20];
cout << "enter filename: ";
cin >> file_name;
ifstream input;
input.open(file_name);
if (! input)
{
cout << "Can't open file" << file_name;
//exit (0);
}

input  >> number;
float min = number;
float max = number;

while (input>>number)
{
counter++;
sum=sum+number;
// Now, we can also check for Min/Max...
if (number > max) max = number;
if (number < min) min = number;
}
average=sum/counter;

cout<< fixed<<cout.precision(3);
cout<< "The average file in file test is was "<<average<<endl;
cout<< fixed<<cout.precision(3);
cout<<"The largest number is: "<<max<<endl;
cout<< fixed<<cout.precision(3);
cout<<"The smallest number is: "<<min<<endl;
input.close();
return 0;
}

When i run it, the min and max values are zero! Any help is appreciated.

2 Answers

Your first while loop will read to the end of file; thus, your second while loop won't actually read anything - you will already be at the file's end before it starts.

You should incorporate the code that is the 'body' of the second loop into the first one, and remove that 'redundant' second loop. You are also assigning the Min and Max variables the wrong way round:

while (input>>number)
{
    counter++;
    sum=sum+number;
    // Now, we can also check for Min/Max...
    if (number > Max)
        Max = number;
    if (number < Min)
        Min = number;
}
average = sum / counter;

You should also give your Min and Max better initial values (unless you know there will be both positive and negative numbers in the data):

float number, sum = 0, average, Max = -FLT_MAX, Min = FLT_MAX;

Feel free to ask for further clarification and/or explanation.

This loop reads all numbers in the file:

while (input>>number)
{
    counter++;
    sum=sum+number;
}

After that loop, input is already at the end of the file and the next loop will not read any numbers.

Read the numbers once and do all processing in a single loop. Alternatively, store the numbers in a std::vector and then work on that.

Moreoever, you never modify Min and Max in your code. What this loop:

while (input>>number)
{
    if (number>Max)
    number=Max;
    else
    number=Min;
}

does instead is to clip the numbers such that any number bigger than 0 (Max) is set to 0 and any other number is set to 0 (Min).

You probably want something along the line of

double number;
input  >> number;
double min = number;
double max = number;
while ( input >> number)  {
   if (number > max) max = number;
   if (number < min) min = number;
}

I used the first number to initialize min and max. If you don't to that, 0 is no good initial value for min and max, but you can use std::numerical_limits<double>::min as initial value for max and std::numerial_limits<double>::max as initial value for min.

Related