I have x and y array...After I have sort y, it succefully sort the y. but for x, there is some number that are not sort like this

x y30 1
45 1
46 1
15 1

I want the x sorting also...like this

x y30 1

15 1
30 1
45 1
46 1


anybody knows how to sort x also...
the input file is in the attachement file.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int y[200],x[200];
int myfile;
int main()
{
	int i,a,b,count;

    void selectionSort();
	ifstream infile;
	infile.open("916-3-2-1.txt");
	if (!infile)
	{
        cout << "Tak Boleh Buka Fail";
        exit(1); // terminate with error
    }
		ofstream myfile;
		myfile.open("916-3-7.txt");
		if (myfile.is_open())
		{
		myfile<<"Before Sort"<<endl;
			myfile<<"-----------"<<endl;

			for(i=0; i<100; i++)
			{
			y[i];	
			}

			count=0;
			while(infile>>b>>a)
			{
				y[count]=a;
				x[count]=b;
				//myfile<<setw(5)<<x[count]<<"   "<<y[count]<<endl;
				count=count+1;
			}
				myfile<<endl;
    
				infile.close();
				int j,min,minat;
    
			

				for(i=0;i<count;i++)
				{
				minat=i;
				min=y[i];

					for(j=i+1;j<count;j++) //select the min of the rest of array
					{
					if(min<y[j] )   //ascending order for descending reverse
		 
						 {
						 minat=j;  //the position of the min element 
						 min=y[j];
			  			}
					 }

				int temp=y[i], temp1=x[i] ;
				y[i]=y[minat];  //swap 
				y[minat]=temp;
				x[i]=x[minat];
				x[minat]=temp1;

				}
    //myfile<<"After Sort in Descending Order"<<endl;
	//myfile<<"------------------------------"<<endl;
	for(i=0;i<count;i++)
    myfile<<setw(5)<<x[i]<<"   "<<y[i]<<endl;
	myfile<<"\n";
	
	{
	for(i=0;i<count;i++)
	{
		minat=i;
		min=y[i];
      for(j=i+1;j<count;j++) 
	  {
		  if(  min<y[j] && min>x[j])  
			  
		  {
			  minat=j;  
			  	min=y[j];

		  }
	  }
	  int temp1=x[i],  temp=y[i];
	 x[i]=x[minat];
	  x[minat]=temp1; 
	  y[i]=y[minat];  //swap 
	  y[minat]=temp;
	}
    myfile<<"After Sort "<<endl;
	myfile<<"----------"<<endl;
	for(i=0;i<count;i++)
    myfile<<setw(5)<<x[i]<<"   "<<y[i]<<endl;
	return 0;
	}
}return 0;
	}

Dani AI

Generated

The problem is that x and y must be treated as a single paired record when you sort. A second ad‑hoc pass that compares elements of x against y (or does swaps without keeping pairs together) will break the pairing. The simplest, most robust fix is to store each row as one object and sort those objects with a comparator that orders by y and uses x as a tie‑breaker (or vice versa), rather than trying to juggle two parallel arrays.

Example using a vector of pairs and std::sort:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility>
#include <iomanip>

int main() {
    std::ifstream in("input.txt");
    if (!in) return 1;
    std::vector<std::pair<int,int>> rows; // {x,y}
    int x,y;
    while (in >> x >> y) rows.emplace_back(x,y);

    std::sort(rows.begin(), rows.end(),
        [](const auto &a, const auto &b) {
            if (a.second != b.second) return a.second < b.second; // sort by y
            return a.first < b.first; // tie-break by x
        });

    std::ofstream out("output.txt");
    for (auto &p : rows) out << std::setw(5) << p.first << "   " << p.second << '\n';
}

Notes and troubleshooting:

  • If you want descending y, flip the comparator. If you want stability for equal keys, use std::stable_sort.
  • Avoid fixed-size raw arrays and magic limits; vectors grow safely.
  • Check the file read order matches how you store pair members (x then y vs y then x).
  • Remove the extra manual sorting pass that mixes x and y comparisons; if you must implement selection sort yourself, always swap both elements from the same index together.
  • ’s point about tidy indentation is useful: clean formatting often makes these logic errors obvious.

When are you going to start formatting your code so that the code within brackets lines up? I don't know how many times you have been asked to do that so that your code is readable. Either use spaces or use tabs, but not both. Maybe that will help. You either don't know what we are talking about regarding indentation or you simply refuse to do it. There is also the "Preview Post" button. Hit it before posting and it'll show how things will look. Fix it so it lines up right, then post.

commented: Well said, Bravo!! +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.