Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views7 pages

Project 18 F

The document outlines a programming project that involves creating a matrix of integers based on user-defined dimensions, M and N, which must be between 3 and 19. It details tasks such as displaying the matrix, finding the maximum and minimum values with their positions, sorting the matrix elements in descending order, and outputting the rearranged matrix. The document also includes an algorithm and Java code to implement these functionalities, along with variable descriptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Project 18 F

The document outlines a programming project that involves creating a matrix of integers based on user-defined dimensions, M and N, which must be between 3 and 19. It details tasks such as displaying the matrix, finding the maximum and minimum values with their positions, sorting the matrix elements in descending order, and outputting the rearranged matrix. The document also includes an algorithm and Java code to implement these functionalities, along with variable descriptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROJECT 18

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

rows and ‘N’ is the number of columns such that both M and N must be greater than 2 and

less than 20. Allow the user to input integers into this matrix.

Perform the following tasks on the matrix.

(a) Display the input matrix.

(b) Find the maximum and minimum value in the matrix and display them along with their

position.

(c) Sort the elements of the matrix in descending order using any standard sorting technique

and rearrange them in the matrix.

(d) Output the rearranged matrix.

Test your program for the following data and some random data:

Example 1

INPUT: M = 3

N=4

Enter elements of the matrix:

8793

-2 0 4 5

1 3 6 -4

OUTPUT: ORIGINAL MATRIX

8793

-2 0 4 5

1 3 6 -4

LARGEST NUMBER: 9

ROW = 0

COLUMN = 2

SMALLEST NUMBER: -4

ROW = 2

COLUMN = 3
REARRANGED MATRIX

-4 -2 0 1

3345

6789

Example 2

INPUT: M = 3

N=3

Enter elements of the matrix:

793

-2 4 5

1 16 4

OUTPUT: ORIGINAL MATRIX

793

-2 4 5

1 16 4

LARGEST NUMBER: 16

ROW = 2

COLUMN = 1

SMALLEST NUMBER: -2

ROW = 1

COLUMN = 0

REARRANGED MATRIX

-2 1 3

445

7 9 16

Example 3

INPUT: M = 3

N = 22

OUTPUT: SIZE OUT OF RANGE


ALGORITHM

Step 1: Start

Step 2: Input the number of rows M and columns N.

Step 3:
Check if M > 2 and M < 20 and N > 2 and N < 20
→ If not, print "SIZE OUT OF RANGE" and stop.

Step 4:
Declare a 2D array A[M][N]
Declare a 1D array flat[M*N] for storing elements linearly

Step 5:
Set max = -∞, min = ∞
Set variables maxRow, maxCol, minRow, minCol

Step 6:
Loop i = 0 to M-1
Loop j = 0 to N-1
Input A[i][j]
Store A[i][j] in flat array
Update max and min and store their positions

Step 7:
Display the original matrix A

Step 8:
Print the maximum and minimum values along with their positions

Step 9:
Sort the flat array in descending order
(You can sort in ascending first and then reverse it)

Step 10:
Re-fill the sorted values into matrix A row-wise

Step 11:
Display the rearranged matrix

Step 12: End

CODING
import java.util.Scanner;

import java.util.Arrays;

public class MatrixOperations {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter number of rows (M): ");

int M = sc.nextInt();

System.out.print("Enter number of columns (N): ");

int N = sc.nextInt();

if (M <= 2 || M >= 20 || N <= 2 || N >= 20) {

System.out.println("SIZE OUT OF RANGE");

return;

int[][] A = new int[M][N];

int totalElements = M * N;

int[] flat = new int[totalElements];

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

int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;

int maxRow = 0, maxCol = 0, minRow = 0, minCol = 0;

int index = 0;

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

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

A[i][j] = sc.nextInt();

flat[index++] = A[i][j];

if (A[i][j] > max) {

max = A[i][j];

maxRow = i;

maxCol = j;

if (A[i][j] < min) {

min = A[i][j];

minRow = i;

minCol = j;
}

System.out.println("ORIGINAL MATRIX:");

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

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

System.out.print(A[i][j] + " ");

System.out.println();

System.out.println("LARGEST NUMBER: " + max);

System.out.println("ROW = " + maxRow);

System.out.println("COLUMN = " + maxCol);

System.out.println("SMALLEST NUMBER: " + min);

System.out.println("ROW = " + minRow);

System.out.println("COLUMN = " + minCol);

Arrays.sort(flat);

for (int i = 0; i < totalElements / 2; i++) {

int temp = flat[i];

flat[i] = flat[totalElements - 1 - i];

flat[totalElements - 1 - i] = temp;

index = 0;

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

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

A[i][j] = flat[index++];

System.out.println("REARRANGED MATRIX:");
for (int i = 0; i < M; i++) {

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

System.out.print(A[i][j] + " ");

System.out.println();

OUTPUT

VARIABLE DESCRIPTION
Variable Name Data Type Purpose

M int Stores the number of rows of the matrix

N int Stores the number of columns of the matrix

A[][] int[][] 2D array to store the matrix elements

flat[] int[] 1D array to store all matrix elements for sorting

i, j int Loop counters for rows and columns

index int Index for accessing elements in the 1D array

max int Stores the maximum value in the matrix

min int Stores the minimum value in the matrix

maxRow int Stores the row index of the maximum value

maxCol int Stores the column index of the maximum value

minRow int Stores the row index of the minimum value

minCol int Stores the column index of the minimum value

temp int Temporary variable for swapping elements (used in reversing array)

sc Scanner Scanner object to take input from the user

You might also like