0

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
4   56
3   46
1   25
6   22
2   20
7   20
8   18
19   16
24   16
25   16
26   16
18   15
20   15
22   15
23   14
5   12
21   12
72   11
108   11
133   11
136   11
107   10
139   10
27   9
99   9
98   8
102   8
17   8
101   7
95   6
103   6
104   6
132   6
134   6
137   6
49   5
69   5
12   5
74   5
96   5
29   5
109   5
110   5
130   5
68   4
70   4
75   4
100   4
111   4
73   3
53   2
59   2
60   2
61   2
62   2
63   2
64   2
65   2
66   2
67   2
45   2
46   2
39   2
47   2
76   2
80   2
86   2
87   2
88   2
89   2
90   2
91   2
92   2
93   2
94   2
40   2
48   2
41   2
42   2
43   2
115   2
121   2
122   2
123   2
124   2
125   2
126   2
127   2
128   2
129   2
44   2
33   2
56   1
57   1
30   1
58   1
97   1
11   1
10   1
77   1
78   1
9   1
31   1
32   1
105   1
106   1
14   1
16   1
79   1
52   1
36   1
112   1
113   1
114   1
81   1
116   1
117   1
118   1
119   1
120   1
82   1
83   1
84   1
85   1
51   1
54   1
37   1
55   1
71   1
38   1
131   1
50   1
13   1
34   1
135   1
28   1
35   1
138   1
15   1
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by VernonDozier
1

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.

Votes + Comments
Well said, Bravo!!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.