Determing the maximum number (coordinate) from imput file

This is a discussion on Determing the maximum number (coordinate) from imput file within the C++ Programming forums, part of the General Programming Boards category; Hey. I need some help with C or C++. I am a beginer, so the code might be weird. I ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Determing the maximum number (coordinate) from imput file

    Hey.
    I need some help with C or C++. I am a beginer, so the code might be weird.

    I have to import a file.txt with real E notated (like 2.3E-12) numbers in 2 columns in it (coordinates basicly - x and y) in to C or C++ (does not matter which, I am learning both). Then I have to determine the maximum number from the second column (y) and then write out the full apropriate coordinate (x, max. y).

    I managed to find out the max. y, coordinate in case I have integer numbers, the problem is I need it for real numbers, which are quite big: up to E+-30.

    Another problem is the number of lines (or buffer). There are approx. 70000 - 600000 rows in the columns, so 70000 values, from which I have to find the max. (or min.).

    This is the code, but only works for Integers (I still have to make it output both coordinates):
    Code:
           #include <iostream>
            #include <fstream>
            #include <stdlib.h>
            using namespace std;
    
    
            int matrix[70000][2]; 
    
    
            int main () {
              ifstream inFile;
              inFile.open("input.txt");
    
               if (!inFile) {
                cout << "Unable to open file";
                exit(1); 
                }
    
    
    
              int numRows = 0;
              int max = matrix[0][1];
              int min = matrix[0][1];
    
             while (inFile >> matrix[numRows][0]) {
        
               inFile >> matrix[numRows][1];
           
                  if (matrix[numRows][1] > max){ 
                      max = matrix[numRows][1];
                      }
                  else if (matrix[numRows][1] < min){
                      min = matrix[numRows][1];
                      }
    
               numRows++;
              }
                          printf ("Maximum element in an array : %d\n", max);
                          printf ("Minimum element in an array : %d\n", min);
    
            inFile.close ();
          }
    I guess you cannot import real numbers as easy as integers? Or is there a quick fix to above code? The code works for int, and has not been tested with larger number of lines.

    And this code bellow, I found on the net and modified (I still have to make it read from the secound coloumn, I guess the same way as for integers?), but I guess the buffer size is the problem here, it only works for small number of rows (for example, if I leave only 100 lines in input.txt, it works fine)?, for larger number of lines (10000 numbers or more) runing this program in cmd (windows) returns an error like: "...program.exe has encountered a problem and needs to close..." :

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
       double largestNum, smallestNum;
       float values[9725];
       double sum = 0;         // holds total of all numbers
       char buffer[9725][9725];      // character arrays store numbers in "doubles.txt"
       int count = 0;
    
       ifstream file;
       file.open("doubles.txt");
    
       while(!file.eof())             // reads "doubles.txt" until end of file
       {
          file.getline(buffer[count],9725,'\n');   // reads line by line to buffer[]
          values[count] = atof(buffer[count]);      // converts char buffer[] to float values[]
          //sum += values[count];            // adds all numbers from values[]
          count++;
       }
    
       largestNum = values[0];    // seeding largestNum with first value
       smallestNum = values[0];   // seeding smallestNum with first value
    
       for(count=0;count<9725;count++)
       {
          if(values[count] < smallestNum)     // compares smallestNum to values[]
             smallestNum = values[count];  // new smallestNum stored here
          if(values[count] > largestNum)      // compares largestNum to values[]
             largestNum = values[count];   // new largestNum stored here
       }
    
       cout << " Largest : " << largestNum << endl; // prints largest number
       cout << "Smallest : " << smallestNum << endl;   // prints smallest number
      // cout << " Average : " << sum / 25 << endl;   // prints average
    
       system("pause");
       return 0;
    }

    The imput.txt files I will need to read are like this bellow (for the second code I used only secound column, for the first code I used simple integer two coulumn test file), just with the extra 520 000 lines..it is quite big, 12Mb:

    15894.6 6.00085E-13
    31789.1 7.21448E-13
    47683.7 7.32097E-13
    63578.3 6.43489E-13
    79472.9 5.90027E-13
    95367.4 7.74707E-13
    111262 5.11338E-13
    127157 7.45003E-13
    143051 6.44474E-13
    158946 4.85771E-13
    174840 8.93456E-13
    190735 7.39305E-13
    206629 8.06542E-13
    222524 8.5784E-13
    238419 7.12852E-13
    254313 4.8251E-13
    270208 6.47216E-13
    286102 7.0523E-13
    301997 7.24186E-13
    317891 5.6712E-13

    Is there any other simple way, other than programing (In case the buffer size is to big problem here) to find out the maximum and minimum, any idea?
    Any help appreciated and Thanks in advance!

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    I guess you cannot import real numbers as easy as integers? Or is there a quick fix to above code? The code works for int
    your file contains floating point numbers, when you test the size of them you are reading them into an array that is declared as storing int values, would this make sense to you?

    dunno if i am reading your stuff right tho, not sure what you on about
    Last edited by rogster001; 11-17-2009 at 09:33 AM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Hey, rogster0001

    The first code pasted above was not used with floating point numbers, just with intigers, for example:

    1 2
    3 5
    6 3
    5 6
    2 6

    And yes, it declares int values and is working for inputing int numbers only (not for the file that contains floating point numbers) - it was just a test if it would work that way, to start things easier...

    The problem is, that I am not able to make that code to read the floating point numbers (instead of int numbers) into an array that would be declared as storing floating point numbers... Just renaming "int" into "float" or "double" does not work... Program returns starting value, 0.

    So the q. are:
    1) I guess you have to store floating point numbers into char and then convert char buffer to float values, like is done in the 2. example code? But is this the only way (or is it possible to "repair" the 1. code, which I find simpler)?

    2) I do not know, what should be done with the 2. code, so it would not crash when run. It compiles ok, even works ok with small sample of rows. For example, if you take just this numbers from second coulmn:

    6.00085E-13
    7.21448E-13
    7.32097E-13
    6.43489E-13
    5.90027E-13
    7.74707E-13
    5.11338E-13
    7.45003E-13
    6.44474E-13
    4.85771E-13
    8.93456E-13
    7.39305E-13
    8.06542E-13
    8.5784E-13
    7.12852E-13
    4.8251E-13
    6.47216E-13
    7.0523E-13
    7.24186E-13
    5.6712E-13

    ...it does the job fine! But it cannot read all 70 000 - 700 000 lines without crashing. So the question: Is this buffer problem and how to solve it?

    Any idea would do, even a non-programing solution to finding the maximum y coordinate.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409

    Stack

    Its system specific and i am no expert on this but you could be overflowing the stack yes, you can use malloc in c dunno bout cplus, i had similar prob recently but with much bigger array if i rem correct,try reducing your array size gradually to see fail

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    I think the question you should be asking yourself is:

    If I just have to find the largest and the smallest value, do I have to store all the values in memory?

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    If I just have to find the largest and the smallest value, do I have to store all the values in memory?
    I guess not, I just need to got through the file and check it simultaniously, what I think the first code above is doing. I dont know any other way to do it, do you?

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    If you think that's what the code is doing, why do you keep all the old values in an array in memory? And yes I can think of a way of doing this, it's more or less the same as yours, just without the storing ALL the values part.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    So this is what I have now, but I can`t make it read doubles. How should I use "ifstream:perator<<(double)"? I tried several times, but does not compile. Also, I am not sure if this code would run with the full input file, having 70 000 - 500 000 rows of numbers, I guess there is no reason it shouldn`t?

    Like this, it returns 0 (initial values are not the problem, I think, in my oppinion import should be read correctly), when run with testing 2 column double numbers.

    Code:
            #include <iostream>
            #include <fstream>
            #include <cstdlib>
            #include <limits.h>
            using namespace std;
    
    
            double matrix[70000][2];
    
            int main () {
    
              ifstream inFile;
              inFile.open("input.txt");  
          //how to make it read double?
          //ifstream::operator<<(double) inFile does not compile...
    
               if (!inFile) {
                cout << "Unable to open file";
                return 0;
                }
    
    
              int numRows = 0;
              double min = INT_MAX;
              double max = INT_MIN;
    
             while (inFile >> matrix[numRows][0]) {
       //  reads the first column, x values
    
               inFile >> matrix[numRows][1];
        //  and reads the coresponding y values (to x)
    
       //from y values read, it determines max y and min y 
      
                  if (matrix[numRows][1] > max){
                      max = matrix[numRows][1];
                      }
                  else if (matrix[numRows][1] < min){
                      min = matrix[numRows][1];
                      }
    
               numRows++;
               printf ("\n");
              }
                          printf ("Maximum element in an array : %d\n", max);
                          printf ("Minimum element in an array : %d\n", min);
    
            inFile.close ();
            return 0;
          }
    JacobN, maybe an example of what you have in mind?


    I am a beginner, so this is the simples way, I can think of - to read the values. How to flush them out of memmory? No idea. If I use:

    while(!file.eof())
    { ....and in it the loop } is probably the same. What I meant was that using buffer like in secound code example posted is unneccessary...
    Last edited by quack6; 11-18-2009 at 07:05 AM.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Alright, I have something like this:
    Code:
    #include <iostream>
    #include <limits>
    #include <fstream>
    
    int main( void )
    {
    	std::ifstream input( "input.txt" );
    
    	if( !input )
    	{
    		std::cout<<"Unable to open file.\n";
    		return 1;
    	}
    
    	double in[2]  = { 0.0, 0.0 };
    	double max[2] = { 0.0, std::numeric_limits<double>::min() };
    	double min[2] = { 0.0, std::numeric_limits<double>::max() };
    
    	while( input>>in[0]>>in[1] )
    	{
    		if( in[1] > max[1] )
    		{
    			max[0] = in[0];
    			max[1] = in[1];
    		}
    		else if( in[1] < min[1] )
    		{
    			min[0] = in[0];
    			min[1] = in[1];
    		}
    	}
    
    	std::cout<<"Maximum element in the file is: ("<<max[0]<<","<<max[1]<<")\n";
    	std::cout<<"Minimum element in the file is: ("<<min[0]<<","<<min[1]<<")\n";
    
    	return 0;
    }
    (If some admin thinks it's too much help then go ahead and remove it)

    But basically, I just keep the largest and the smallest value in a value, read the next coordinate into a variable, compare and assign as needed.

    The lines:
    Code:
    double max[2] = { 0.0, std::numeric_limits<double>::min() };
    double min[2] = { 0.0, std::numeric_limits<double>::max() };
    Simply initalizes the max and min arrays to the maximum and minimum values that can be contained in a double.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Quick response!

    Will have a look in the code, looks interesting (I guess I will not understand some parts from the start). Thnx!

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409

    Step back

    Its not the code but the concept you should be thinking over, as suggested forget storing whole file and just evaluate each entry against last highest or lowest found, you could maintain an array 4 max, min, mean, median values, and do sort 4 mode too

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Hey!
    Yes, I get the point, this code does exactly what I was talking about, without storing all values:
    ""If I just have to find the largest and the smallest value, do I have to store all the values in memory?"
    "I guess not, I just need to got through the file and check it simultaniously...""
    Elegantly done! Kudos to JacobN!

    If I understand it correct, the main difference is here:

    double in[2] = { 0.0, 0.0 };
    //just storing the 2 values (x and coresponding y), instead of "double matrix[70000][2];"

    I have a question though. In "for { }" loops/statements you have to put: i++ (or numRows++
    in my code above for example) so program is running value by value from 0 to max value, I guess that is not necessary for "while { }" statements? Or is it missing here (works perfectly well without it, so maybe I just missed sth.):
    Code:
    while( input>>in[0]>>in[1] )
    	{
    		if( in[1] > max[1] )
    		{
    			max[0] = in[0];
    			max[1] = in[1];
    		}
    		else if( in[1] < min[1] )
    		{
    			min[0] = in[0];
    			min[1] = in[1];
    		}
    	}
    Thnx, Q.
    Last edited by quack6; 11-19-2009 at 06:50 AM.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    The while-loop relies on the state of the "input" ifstream object, if it has reached the end of the file(or if there somehow was an error while reading i suppose) it will break out of the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Double print
    By thestrap in forum C Programming
    Replies: 1
    Last Post: 12-05-2007, 06:17 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21