[go: up one dir, main page]

0% found this document useful (0 votes)
23 views9 pages

Document 1

Uploaded by

thanushree77777
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)
23 views9 pages

Document 1

Uploaded by

thanushree77777
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/ 9

Question 17:

Write a program to declare a Square, Matrixx a[][] of order m*n where ‘m’ is the number of rows and ‘n’ is
the number of columns, such that m and n both must be greater than 2 and less than 10 . accept the value of m
and n as user input. Allow the user to input the integers into Matrixx and perform the following task.
1. Display the original Matrixx
Original Matrixx:
1 2 3 4
5 6 7 8
9 10 11 12

2. Rotate the matrix 90° clockwise as shown below:


Rotated matrix:
9 5 1
10 6 2
11 7 3
12 8 4

3. Rotate the matrix 90° anticlockwise as shown below:


Rotated matrix:
4 8 12
3 7 11
2 6 10
1 5 9

Program 17: Code:


import java.util.*;
class r17
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int choice=0;
System.out.println("Enter the number of rows");
int m=sc.nextInt();
System.out.println("Enter the number of columns");
int n=sc.nextInt();
int a[][]=new int[m][n];
System.out.println("Enter the elements of the array");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=sc.nextInt();
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]+"\t"); // printing the original Matrix.
}
System.out.println();
}
System.out.println("\nMENU");
System.out.println("1.Rotate the Matrix, 90° clockwise");
System.out.println("2.Rotate the Matrixx, 90° anticlockwise");
System.out.println("Enter your choice from the above menu");

choice =sc.nextInt();
if(choice==1)
{
int c[][] = new int[n][m]; //creating a new array to store the rotated matrix-clockwise
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
c[j][m - 1 - i] = a[i][j]; //rotating the matrix
}
}
System.out.println("Rotated matrix (90° clockwise):");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(c[i][j] + "\t"); // printing, the rotated Matrix
}
System.out.println();
}
}
else if(choice==2)
{
int ac[][] = new int[n][m]; //creating a new array to store the rotated matrix-anticlockwise

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


{
for (int j = 0; j < n; j++)
{
ac[n - 1 - j][i] = a[i][j]; // rotating the Matrix
}
}
System.out.println("Rotated matrix (90° anticlockwise):");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(ac[i][j] + "\t"); // printing, the rotated Matrix.
}
System.out.println();
}
}

else
{
System.out.println("invalid array input");
}
}
}

Program 17: Variable Description.


VARIABLE DATATYPE PURPOSE
a[] int Array to storage in matrix
c[] int Array to store clockwise Matrixx
ac[] int Array to store anticlockwise Matrixx
i,j int Loop variables
m int Number of rows
n int Number of columns
choice int To store the choice of action

Program 17: Variable Description.


VARIABLE DATATYPE PURPOSE
a[] int Array to storage in matrix
k int Loop variables
j int Loop variables
i int Loop variables
m int Number of rows
n int Number of columns
e int To store the minimum element of the column
temp int Temporary Variable for swapping
Program 17: Output:
Question 18:

Write a program to declare a Square Matrixx, a [][] order m*n where m is the number of rows and n is the
number of columns such that m and n both must be greater than 2 and less than 10 . Accept the value of ‘m’ and
‘n’ as user input and perform the following task.
Display the original Matrixx.
Sort each column of the matrix in ascending order using selection sort technique.
Play the changed Matrixx after sorting each row.
Test your program for the following data and some random data:
Example 1:
Input:
m=4
n=3
Enter the element of matrix:
11 -2 3 5 16 7 9 0 4 3 1 8
OUTPUT:
ORIGINAL MATRIX:
11 -2 3
5 16 7
9 0 4
3 1 8
MATRIX AFTER SORTING:
3 -2 3
5 0 4
9 1 7
11 16 8
Example 2:
INPUT:
m=11
n=5
OUTPUT:
Metric size out of range.

Program 18:Code:

import java .util.*;


class r18
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int m = sc.nextInt();
System.out.print("Enter the number of columns: ");
int n = sc.nextInt();

if (m < 3 || m > 9 || n < 3 || n > 9)


{
System.out.println("Matrixx out of range"); //Check if size is Less than 10 and greater than 2
System.exit(0);
}

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

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


for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}

System.out.println("Original Matrixx:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + "\t"); //Printing the original Matrixx
}
System.out.println();
}

// Sort each column using selection sort


for (int j = 0; j < n; j++)
{
for (int i = 0; i < m - 1; i++)
{
int e= i; //The minimum element of each column.
for (int k = i + 1; k < m; k++) {
if (a[k][j] < a[e][j])
{
e= k;
}
}
// Swap
int temp = a[i][j];
a[i][j] = a[e][j];
a[e][j] = temp;
}
}
System.out.println("Matrix after sorting each column:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + "\t"); //Printing this sorted Matrixx.
}
System.out.println();
}}}

Program 18: Output:

You might also like