I have to key in 7 values, representing a distance. Have to sort with exchange sort in ascending order.
I also set up a parallel array to keep track of the days of the week, so that when you output the sorted data the corresponding weekday is listed.
I can sort the array with no problem, but when I enter the days it will print out in the order I input it, not with the corresponding value. I am not able to figure out how to enter the day with the corresponding value AFTER it gets sorted. I know my logic is off. Thanks in advance!
Sample output:
7 km on Thursday
8 km on Monday
4 km on Tuesday
and so on...
import java.io.*;
import java.util.*;
public class exchangeSort
{
public static void main()
{
Scanner kbReader= new Scanner (System.in);
double dist []= new double [7];
String days []= new String [7];
for (int i=0; i<dist.length; i++)
{
System.out.println("Enter distance");
dist[i]= kbReader.nextDouble();
System.out.println("Now enter a day");
days[i]= kbReader.next();
}
System.out.println(" ");
sort (dist);
for (int i=0; i<days.length; i++)
{
System.out.println(dist[i] + " km on " + days[i]);
}
}
public static void sort (double num [] )
{
int i, j;
double temp;
for ( i=0; i< num.length-1; i++ )
{
for ( j=i+1; j <num.length; j++ )
{
if( num[i] > num[j] )
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
}
}