1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
patriot [66]
3 years ago
8

Write a program to declare a matrix A[][] of order (MXN) where ‘M’ is the number of rows and ‘N’ is the

Computers and Technology
1 answer:
Liula [17]3 years ago
4 0

Answer:

import java.io.*;

import java.util.Arrays;

class Main {

   public static void main(String args[])

   throws IOException{

       // Set up keyboard input

       InputStreamReader in = new InputStreamReader(System.in);

       BufferedReader br = new BufferedReader(in);

 

       // Prompt for dimensions MxN of the matrix

       System.out.print("M = ");

       int m = Integer.parseInt(br.readLine());

       System.out.print("N = ");

       int n = Integer.parseInt(br.readLine());

       // Check if input is within bounds, exit if not

       if(m <= 2 || m >= 10 || n <= 2 || n >= 10){

           System.out.println("Matrix size out of range.");

           return;

       }

       // Declare the matrix as two-dimensional int array

       int a[][] = new int[m][n];

 

       // Prompt for values of the matrix elements

       System.out.println("Enter elements of matrix:");

       for(int i = 0; i < m; i++){

           for(int j = 0; j < n; j++){

               a[i][j] = Integer.parseInt(br.readLine());

           }

       }

       // Output the original matrix

       System.out.println("Original Matrix:");

       printMatrix(a);

       // Sort each row

       for(int i = 0; i < m; i++){

         Arrays.sort(a[i]);

       }

       // Print sorted matrix

       System.out.println("Matrix after sorting rows:");

       printMatrix(a);

   }

   // Print the matrix elements separated by tabs

   public static void printMatrix(int[][] a) {

       for(int i = 0; i < a.length; i++){

           for(int j = 0; j < a[i].length; j++)

               System.out.print(a[i][j] + "\t");

           System.out.println();

       }

   }

}

Explanation:

I fixed the mistake in the original code and put comments in to describe each section. The mistake was that the entire matrix was sorted, while only the individual rows needed to be sorted. This even simplifies the program. I also factored out a printMatrix() method because it is used twice.

You might be interested in
A meeting of the quality team June is nervous about suggesting ideas because he is not sure he understands why they are qualifie
Flura [38]

Answer: Believe in himself Despite the idea will good or bad

Explanation: We are humans, so he can make a mistake with his ideas, the most important thing for a positive workplace behaviors in the meetings is to show security about your thinking, your ideas also when you are talking, keep in mind that is normal the wrongs and the mistakes, but it is no final you can start again and do it better than the last time, and improve your behavior

3 0
3 years ago
Read 2 more answers
Ryan needs to see the space available to insert content on a slide in his presentation. Which feature of presentation program sh
emmainna [20.7K]

He should use the Format feature.

4 0
4 years ago
Read 2 more answers
Which of the following is not related to text formatting?​
s2008m [1.1K]

Explanation:

Searching, hope that helps. its 100% correct, goodluck.

3 0
3 years ago
Mainframe computers have several CPUs, have very fast processor speeds, can supporting multiple operating systems and have huge
Marrrta [24]

Answer:

True.

Explanation:

Mainframe computers were developed and introduced in the early 1950s.

Mainframe computers have several CPUs, have very fast processor speeds, can supporting multiple operating systems and have huge amounts of storage capacity. As a result, mainframe computers are mostly or commonly used by large companies, business firms or governmental institutions for performing various complex tasks such as census, financial transactions, e-commerce, data sequencing, enterprise resource planning, etc.

7 0
3 years ago
Choose
Arisa [49]

Answer:

1. option A simultaneously

2.option A undo

6 0
3 years ago
Other questions:
  • ................njkkjjiooiuuuu
    6·1 answer
  • Import java.util.scanner; public class sumofmax { public double findmax(double num1, double num2) { double maxval; // note: if-e
    10·1 answer
  • The dfs replication engine uses a form of replication called
    6·1 answer
  • What is the definition of delimited text?
    14·2 answers
  • What are Boolean​ connectors? A. sites that connect you to more information on your topic B. databases that link your topic to a
    10·1 answer
  • How does the actual amount left to spend differ from the budgeted amount for the remaining three months of the project?
    12·1 answer
  • Why should you delete files from your computer?
    8·2 answers
  • You and a friend have just started a small business. How could you use the Internet to make your business successful?
    15·2 answers
  • The US government appropriates less than one million dollars in funding each year to STEM education programs. True False
    7·2 answers
  • Question 1 of 10
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!