Answer:
See explaination
Step-by-step explanation:
import java.util.Scanner;
public class sort {
static void lowestToHighest(float arr[][])
{
float temp;
for(int i=0;i<11;i++)
for(int j=0;j<12-i-1;j++) //Using bubble sort to sort
{
if(arr[j][0]>arr[j+1][0])
{
temp = arr[j][0];
arr[j][0] = arr[j+1][0];
arr[j+1][0] = temp;
}
}
for(int i=0;i<11;i++) //Using bubble sort to sort
{
for(int j=0;j<12-i-1;j++)
if(arr[j][1]>arr[j+1][1])
{
temp = arr[j][1];
arr[j][1] = arr[j+1][1];
arr[j+1][1] = temp;
}
}
System.out.println("Data in the array after sorting lowest to highest: ");
for(int i=0;i<12;i++)
System.out.printf(arr[i][0]+" "+arr[i][1]+"\\");
}
static void highestToLowest(float arr[][])
{
float temp;
for(int i=0;i<11;i++)
for(int j=0;j<12-i-1;j++) //Using bubble sort to sort
{
if(arr[j][0]<arr[j+1][0])
{
temp = arr[j][0];
arr[j][0] = arr[j+1][0];
arr[j+1][0] = temp;
}
}
for(int i=0;i<11;i++) //Using bubble sort to sort
{
for(int j=0;j<12-i-1;j++)
if(arr[j][1]<arr[j+1][1])
{
temp = arr[j][1];
arr[j][1] = arr[j+1][1];
arr[j+1][1] = temp;
}
}
System.out.println("Data in the array after sorting highest to lowest: ");
for(int i=0;i<12;i++)
System.out.printf(arr[i][0]+" "+arr[i][1]+"\\");
}
public static void main(String[] args){
float temperature[][]=new float[12][2];
Scanner input = new Scanner(System.in);
System.out.println("Enter 12 months of highest and lowest temperatures for each month of the year: ");
for(int i=0;i<12;i++)
for(int j=0;j<2;j++)
temperature[i][j]=input.nextFloat();
System.out.println("Data in the array: ");
for(int i=0;i<12;i++)
System.out.printf(temperature[i][0]+" "+temperature[i][1]+"\\");
lowestToHighest(temperature);
highestToLowest(temperature);
}
}