# Thread: HELP! Min Max values using array.

1. ## HELP! Min Max values using array.

Hi guys im currently doing an assignment where i am given a .txt file containing an unknown amount of float numbers (there could be thousands).

I have then got to create functions to calculate the min, max, and average of all the float numbers that could be contained in the file.

Any ideas?

Im mainly stuck on the min and max, i have worked out how to do averages but im not so great at using arrays which i believe is needed to be able to work this all out.

2. an unknown amount of float numbers (there could be thousands).
This is not a problem at all.. more than likely, just a scare tactic given to you by your teacher. All you have to do is continue to read in entries from your .txt file, until the file can no longer be read:

Code:
```while(infile >> file_data)
{
//determine if the newly read file_data qualifies as a min
//determine if the newly read file_data qualifies as a max
// += accumulate the newly read file_data as a step in calculating the average
//++bump a counter so you'll know how many entries were read from the .txt file.. also a step in calculating the average
}```

using arrays to calculate a max.. populate an array of floats.. use a nested for loop as a method to compare one element vs. every other element in the array:

Code:
```float max = 0.0f;

for(int i=0; i<array_size; i++)
for(int j=0; j<array_size; j++)
{
if(array[i] > array[j] && array[i] > max)

max = array[i];
}```

Just do the opposite to calcuate a min.

3. ## Thanks.

Cheers for the quick reply, still having difficulty working out what i am doing! Here is what i have done so far, the average works as far as i know, i just dont have a clue how to incorporate the array min and max into it all!

Code:
```#include <iostream.h>
#include <fstream.h>

void average()
{
ifstream fin;

float Num, Total;
int Count;
char ifname[100];

Count = 0;
Total = 0.0;

cout << "Enter input file name :\n";
cin >> ifname;

fin.open(ifname, ios::nocreate);

if (fin.fail())
{
cout << "Unable to open input file\n";
cout << "PROGRAM TERMINATED\n\n";
return;
}

fin >> Num;

while(!fin.fail())
{
Total = Total + Num;
Count++;
fin >> Num;
}

if (Count > 0)
cout << endl << "Average is " << Total / Count << endl;
else
cout << endl << "No data in file" << endl;

fin.close();
}

void main()
{

average();
maximum();

}```
So the function for average works fine, the file imported to calculate the min max and average is the same one each time is there an easier way to open it once instead of 3 times (once for each function).

My attempts at an array failed miserably.

4. In the loop where you add the numbers up, you need to compare each number

- to the current maximum and adjust if the number from the file is higher

- to the current minimum and adjust if the number from the file is lower

You will need two variables in your function to store the current minimum and maximum.

This way, you will only read the file once.

You don't need an array at all by the way. If you see a test question stating something as an unspecified number, you will not use an array. It either works without storing every single value ( like this thread ) or you will need another storage container you will learn about later.

5. Code:
```#include <iostream>
#include <fstream>
using namespace std;

void average()
{
ifstream fin;

float Num, Total, Max,Min;
int Count;
char ifname[100];

Count = 0;
Total = 0.0;

cout << "Enter input file name :\n";
cin >> ifname;

fin.open(ifname);

if (fin.fail())
{
cout << "Unable to open input file\n";
cout << "PROGRAM TERMINATED\n\n";
return;
}

fin >> Num;
Max = Num;
Min = Num;
while(!fin.eof())
{
Total = Total + Num;
Count++;
fin >> Num;
if(Num > Max) Max = Num;
if(Num < Min) Min = Num;
}

if (Count > 0)
cout << endl << "Average is " << Total / Count << endl;
else
cout << endl << "No data in file" << endl;
cout << "Maximum value is " << Max << " and the minimum is " << Min << endl;

fin.close();
}

int main()
{

average();
return 0;

}```
You should't really be using void main! always use int main and return 0 on successful completetion. See above code to find min and max values, it is pretty simple!

6. Code:
`while(infile >> file_data)`
Brain: does that really work the way you think? >> operator always returns a reference to the fstream object, so the while test will always be true, making that an infinite loop.
Code:
`while( !infile.eof() )`
I thik this is what you wanted.

7. infile >> file_data would be prefered to !infile.eof(). The reason is that all istreams will go into a fail state by flipping a bit in a state variable when EOF is read. This will cause the conditional to be interpretrted as false, thus stopping the loop. eof() detects the failed state, not EOF, so it won't return false until after the failed state is set. Therefore, when using return value of eof() as the conditional of a loop, the loop body is evaluated one time too many with undefined behaviour resulting. In either case, the stream must be cleared from the failed state in order for it to be reused.