Dec 16, 2012 at 7:54pm UTC
(using c++ programing) I need to Read x, y (real number) data from a file, and write to another file the same data pairs sorted in order of increasing x value. I can sort the x values fine, i just dont know how to sort the y values so that each y value is with the right x value. Here is what i have:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
// Cody Anderson
// Computer Science 201
int main()
{
int x, y, i, value, value2, value3;
const int nums = 10;
const int num = 10;
string dat;
ifstream datafile;
string str;
ofstream outfile;
outfile.open("in.dat");
datafile.open("out.dat");
for(i=1; i<=10; i=i+1)
{
datafile >> x >> y;
int value[num] = {2, 4, 0, -4, -1, 10, 1, 20, 3, 11};
int value2[nums] = {3, 8, 0, 9, -2, -8, 1, -20, -12, 0};
int numbers = 10;
std::sort(value, value + numbers);
for (i=0; i < numbers; ++i)
{
std::cout << value[i] << " " << value2[i] << endl;
outfile << "(" << value[i] << "," << value2[i] << ")" << endl;
}
}
outfile.close();
return 0;
}
Dec 16, 2012 at 9:23pm UTC
std::sort() is nice, but to use it for your problem you will have to "bind" the arrays somehow.
How are you with structures and operator overloading?
Dec 18, 2012 at 2:39am UTC
Ahh not so good with structures but i will try out Darkmasters approach..thanks guys!