Hello everyone!
I would like to request some help with the following problem.
I am supposed to read from a file some data, put them in the corresponding variables/arrays and do something with them.
The first three variables are particlesN, distanceL, firstKcollisions (see code below). Next, and that is where the problem begins, pairs of numbers have to be stored to two Nx2 arrays, a and b.
When printing the contents of the arrays I get erroneous results (Some of the numbers are, however, stored - but not in the right order-). I am guessing it's something wrong with my iterators but I can't seem to get it right. There is a lot of redundant code written while trying to figure this out...
I am mainly interested on why this doesn't work.
On the following input file, array a should contain pairs (0, 41) to (48, 870) and array b pairs (0, 98) to (48, 884).
Any help will be highly appreciated!!
input1.txt
10 1000 4
0 41
10 106
11 175
16 473
22 397
28 508
34 522
42 585
44 786
48 870
0 98
10 107
11 114
16 353
22 220
28 511
34 554
42 628
44 787
48 884
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
using namespace std;
void print_table(const int table[][2], int table_rows)
{
const int WIDTH = 10;
cout << fixed << setprecision(0);
for (int i = 0; i < table_rows; i++)
{
for (int j = 0; j < 2; j++)
cout << setw(WIDTH) << table[i][j];
cout<< "\n";
}
}
int main()
{
int start_s = clock();
ifstream instream;
instream.open("input1.txt");
int particlesN, distanceL, firstKcollisions;
instream >> particlesN >> distanceL >> firstKcollisions;
long int fileIndexPosition = instream.tellg();
cout << fileIndexPosition << endl;
int a[particlesN][2], b[particlesN][2];
for (int i = 0; i < particlesN; i++)
{
for(int j = 0; j < 2; j++)
{
instream >> a[i][j];
fileIndexPosition = instream.tellg();
cout << "File index position for data read into a is " << fileIndexPosition << " at iteration i = " << i << endl;
}
}
for (int i = particlesN; i < (particlesN)*2; i++)
{
for(int j = 0; j < 2; j++)
{
instream >> b[i][j];
cout << "File index position for data read into b is " << fileIndexPosition << " at iteration i = " << i << endl;
}
}
instream.close();
cout << particlesN <<" "<< distanceL <<" "<< firstKcollisions << endl;
print_table(a, particlesN);
print_table(b, particlesN);
int stop_s = clock();
cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;
return EXIT_SUCCESS;
}
|