954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sort y but it not sort x

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;
	}
Attachments 916-3-2-1.txt (1.14KB)
nurulshidanoni
Posting Whiz in Training
219 posts since Nov 2007
Reputation Points: 9
Solved Threads: 0
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You