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;
}