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):
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.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 (); }
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!