65.4k views
4 votes
Lab Assignment 7A For the lab this week, you will sort an array - using any of the sort methods discussed in Chapter 23 or the Selection sort. It's your choice. Use the following criteria for your assignment: Write a program that uses a two-dimensional array to store daily minutes walked and carb intake for a week. Prompt the user for 7 days of minutes walked and carb intake; store in the array. Write a sort ( your choice which sort ) to report out the sorted minute values -lowest to highest. Write another to report out the sorted carb intake - highest to lowest. These methods MUST be your original code. Your program should output all the values in the array and then output the 2 sorted outputs. You must use an array to receive credit for this assignment

1 Answer

4 votes

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

}

}

User EL MOJO
by
4.6k points