Thread: Finding Max and Min Value

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    10

    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

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    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

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you havn't initialized the data array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Finding MAX or MIN within a string
    By DBA1 in forum C Programming
    Replies: 10
    Last Post: 02-27-2004, 08:33 PM
  5. help finding max and min values from list
    By ericp023 in forum C Programming
    Replies: 7
    Last Post: 02-22-2003, 06:51 PM