# Finding Max and Min Value

• 11-28-2006
will15
Finding Max and Min Value
Hi,
The problem is that I have a program which processes a 2D array. One task is to find the min and max of one column (ignoring the second). Here is my code so far:
Code:

```#include <iostream.h> #include <stdlib.h> #include <fstream> using namespace std; double data[4][2]; // declare 2D array for data in file. int count; int noOfReadings; int counter=0; int numberOfSpeedIntervals; int numberOfSectors; int main() {  ifstream inPutFile;//Creates stream to input file.  inPutFile.open("c:\\grade.txt");//Opens stated file.  if (!inPutFile)//Deals with problems opening file.       cout << "Error opening file!" << endl; //Finds how many lines of data there are. int item; while (!inPutFile.eof()) { inPutFile >> item; counter++; } const int rows = (counter/2); cout<<"Number of rows: "<<rows<<endl; cout<<"Please type how many intervals do you require: "; cin>> numberOfSpeedIntervals; cout<<"Please type how many sectors do you require: "; cin>> numberOfSectors; //initialise current max and min   // find max and min   // initialise current max and min   double maxRead = data[i][0];   double minRead = data[i][0];     for (int i = 1; i < data; i++)   {       if (maxRead < data[i])     {         maxRead = data[i];     }         if (minRead > data[i])     {         minRead = data[i];     }        }       system("PAUSE");       return 0; }```
The last lines are faulty. Any ideas?

Will
• 11-28-2006
vart
for (int i = 1; i < data; i++)
the limit is incorrect - should be 4 (best declare it as const somewhere and use in every place in the code you need the array size)
• 11-28-2006
will15
Thanks,
I changed data to rows which is the user set variable set by user. Code now reads:
Code:

```. . . . while (!inPutFile.eof()) { inPutFile >> item; counter++; } const int rows = (counter/2); cout<<"Number of rows: "<<rows<<endl; . . . . //initialise current max and min   // find max and min   // initialise current max and min   double maxRead = data[0][0];   double minRead = data[0][0];     for (int i = 1; i < rows; i++)   {       if (maxRead < data[i][0])     {         maxRead = data[i][0];     }         if (minRead > data[i][0])     {         minRead = data[i][0];     }      cout<<"Min= "<<minRead<<endl;   cout<<"Max= "<<maxRead<<endl;   }       system("PAUSE");       return 0; }```
I get max and min returned as zero. Any ideas?
Cheers
Will
• 11-28-2006
vart
you havn't initialized the data array