Question

In: Computer Science

Language for this question is Java write the code for the given assignment Given an n...

Language for this question is Java

write the code for the given assignment

Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements in row wise order.Output:
Print the elements of the matrix in sorted order.Constraints:
1<=T<=100
1<=n<=100
1<=a[n][n]<=100Example:
Input:
2
4
10 20 30 40 15 25 35 45 27 29 37 48 32 33 39 50
3
1 3 4 2 6 7 5 8 9Output:
10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50
1 2 3 4 5 6 7 8 9

Solutions

Expert Solution

// Java program to print the elements of
// a 2 D array or matrix
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Matrix {

   public static void main(String args[]) throws IOException
   {
       Scanner sc = new Scanner(System.in);
       int noOfTestCases = sc.nextInt();
       //taking nooftestcases input from the user
       //result is for adding sorted array values
       ArrayList<String> result = new ArrayList<String>();
       //traversing
       for(int k=0;k<noOfTestCases;k++)
       {
       int size = sc.nextInt();
       int mat[][] = new int[size][size];
      
       //converting 2D to 1D for sorting
       int temp[] = new int[size*size];
       int index = 0;
       // Loop through all rows
       for (int i = 0; i < mat.length; i++)
           // Loop through all elements of current row
           for (int j = 0; j < mat[i].length; j++)
               {
               mat[i][j] = sc.nextInt();
               temp[index++] = mat[i][j];
               }
      
       //doing sorting
       Arrays.sort(temp);
      
       //puting in arraylist for printg
       String values = "";
       for (int i = 0; i < temp.length; i++)
               values+=temp[i]+" ";
      
       result.add(values);
       }
       //printing results
       for(String i:result)
           System.out.println(i);
      
   }
}


Related Solutions

USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Write a JAVA pogram for the following scenario. Given an n × n × n cube...
Write a JAVA pogram for the following scenario. Given an n × n × n cube containing n3 cells, we are to place n queens in the cube so that no two queens challenge each other (so that no two queens are in the same row, column, or diagonal). In JAVA, implement it on your system to solve problem instances in which n = 4 and n = 8.
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Write in Java. Separate code for each question. The answer to the first question should NOT...
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable. 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that...
Java Language The int t contains an integer between 1 and 50 (inclusive). Write code that outputs the number in words and stores the result in the String inwords. For example, if t is 35 then inwords should contain "thirty five". Test Cases Test case #1 Expected result: When t is 2, your code sets inwords to "two" Test case #2 Expected result: When t is 50, your code sets inwords to "fifty" Test case #3 Expected result: When t...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT