Can someone please modify this code so it is for a two-dimensional array? Thank
ID: 663779 • Letter: C
Question
Can someone please modify this code so it is for a two-dimensional array? Thank you! Will rate high!
// the applet ComputersR sorts and displays the stores inventory
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
public class ComputersR {
public static void main(String[] args) {
System.out.println(" Welcome to Computers R Us. We carry the following items:");
// define variables
int[] Stock;
Stock = new int[4];
String[] Software;
Software = new String[4];
int i;
// stock variables
Stock[0] = 54;
Stock[1] = 26;
Stock[2] = 33;
Stock[3] = 11;
// software variables
Software[0] = "Lotus 123";
Software[1] = "WordPerfect";
Software[2] = "Microsoft Word";
Software[3] = "Microsoft Excel";
// unsorted
System.out.println(" Software: " + Software[0] + " Stock amount: " + Stock[0]);
System.out.println("Software: " + Software[1] + " Stock amount: " + Stock[1]);
System.out.println("Software: " + Software[2] + " Stock amount: " + Stock[2]);
System.out.println("Software: " + Software[3] + " Stock amount: " + Stock[3]);
// sort
System.out.println(" The following is a sorted list according to stock:");
Arrays.sort(Stock);
for(i=0; i < Stock.length; i++) {
System.out.println("Software: " + Software[i] + " Stock amount: " + Stock[i]);
}
}
}
Explanation / Answer
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
public class ComputersR {
public static void main(String[] args) {
System.out.println(" Welcome to Computers R Us. We carry the following items:");
// define variables
int[][] Stock = new int[10][10];
// Input from user for fill arrays
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
System.out.print("Enter a number between 0 and 100 : ");
Stock[i][j] = sc.nextInt();
}
}
// unsorted
System.out.println(" The following is a unsorted list according to stock:");
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
System.out.print(l[i][j]+" ");
}
System.out.println();
}
// sort
System.out.println(" The following is a sorted list according to stock:");
for (int i = 0; i < 10; i++){
Stock[i] = Arrays.sort(Stock[i]);
}
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
System.out.print(l[i][j]+" ");
}
System.out.println();
}
}
}