# HELP! Min Max values using array.

• 11-27-2005
steverushby
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.

• 11-28-2005
The Brain
Quote:

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.
• 11-28-2005
steverushby
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.
• 11-28-2005
nvoigt
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.
• 11-28-2005
jlf029
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!
• 11-28-2005
Ancient Dragon
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.
• 11-28-2005