Help with an Array Problem

(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;
}
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?
did pretty much the same last week, just sorted y instead of x.
think of contours_poly as a struct containing x and y.
here is my code:
1
2
3
4
5
6
7
8
9
10
	for(int i=contours_poly.size()-1; i>=1; --i)
	{
		int index = i;
		for(int j=i-1; j>=0; --j)
		{
			if(contours_poly[j].y > contours_poly[index].y)
				{index = j;}
		}
		std::swap(contours_poly[i], contours_poly[index]);
	}
Ahh not so good with structures but i will try out Darkmasters approach..thanks guys!
Topic archived. No new replies allowed.