Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Program No.

-
Question
Write a java program to transpose a square matrix without using a second array.
(Transpose of a matrix: Columns become rows and rows become columns).
Algorithm:
Step 1: Begin
Step 2:[Creating objects and initializing variables]
Creating Scanner class object to accept data.
An integer variable n is used to store the number of rows and columns input by the user.
An integer array arr[ ][ ] is initialized
n user input
arr[ ][ ] n,n
Step 3: [Generate a loop to accept the elements of the matrix from the user]
for r0 to n-1
{
for c0 to n-1
{
Input: arr[ r ][ c ]
cc+1;
}rr+1;
}
Step 4:[Generate a loop to print the elements of the original matrix]
for r0 to n-1
{
for c0 to n-1
{
Print: arr[ r ][ c ]
cc+1;
}rr+1;
}

Step 5: [Generate a loop to print the elements of the transposed matrix]


for r0 to n-1
{
for c0 to n-1
{
Print: arr[ c][ r]
cc+1;
}rr+1;
}

Step 6: End

Source Code:
import java.util.*;
class transpose
{
public static void main()
{
int r,c,m,n;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of rows and
columns");
n=sc.nextInt();
int arr[][]=new int[n][n]; //Initializing array
System.out.println("Enter the elements:"); //Taking
input
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
arr[r][c]=sc.nextInt();
}
}
System.out.println("The original matrix:");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(arr[r][c]); //Printing the
original matrix
}
System.out.println(" ");

}
System.out.println("The transposed matrix:");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(arr[c][r]); //Printing the
transposed matrix
}
System.out.println(" ");

}
}
}

Variable description
Variable Data Type Purpose

r integer Iteration

c integer Iteration

n integer Stores the number


of elements in an
array input by
user

Method Description
Methods Description
main() Comprises the entire program, to
transpose a square matrix without using
a second array.
nextInt() Accepts integer from the user
Scanner(...) Parameterised Constructor

Class Description
Class Description
transpose User Defined Class to transpose a
square matrix without using a second
array..
Scanner To create an object of Scanner class

Output:
Program No -
Question
Write a program to declare a square matrix A[ ][ ] of order ‘n’. Accept positive integers
into this matrix. Perform the following tasks on the matrix:
(i)Output the original matrix (ii) Find the SADDLE POINT for the matrix. If the matrix
has no saddle point, output the message “NO SADDLE POINT”.
[Note: A saddle point is an element of the matrix such that it is the minimum element for
the row to which it belongs and the maximum element for the column to which it
belongs. Saddle point for a given matrix is always unique.]
Example:In the following matrix 4 5 6 7 8 9 5 1 3
Saddle point = 7 because it is the minimum element of row 2 and maximum element of
column 1.
Algorithm:
Step 1: Begin
Step 2:[Creating objects and initializing variables]
Creating Scanner class object to accept data.
An integer variable n is used to store the number of rows and columns input by the user.
An integer array arr[ ][ ] is initialized
n user input
arr[ ][ ] n,n
Step 3: [Generate a loop to accept the elements of the matrix from the user]
for r0 to n-1
{
for c0 to n-1
{
Input: arr[ r ][ c ]
cc+1;
}rr+1;
}
Step 4:[Generate loop to print the elements of the original matrix]
for r0 to n-1
{
for c0 to n-1
{
Print: arr[ r ][ c ]
cc+1;
}rr+1;
}

Step 5:[ Generating a loop to find minimum element of the row]

For r0 to n

{ Initializing variables min,

minarr[r][0];

minc0;

minr0;

maxr0;

For c0 to n

If arr[r][c]<min

minarr[r][c];

mincc;

minrr;

}cc+1;

Step 6: Finding the maximum element in the column corresponding to the


minimum element of row
maxarr[0][minc]; // Initializing max with first element of that column
for c0 to n
{

If arr[c][minc]>max
{
maxarr[c][minc];
}cc+1;
}
Step 7 : Checking if the minimum element is also the maximum element of
the column. If true the counter a is increased to 1
If min=max
{
Print max;
a1;
}
End loop
Step 8 : If a  0 then Print:” No Saddlepoint”
Step 9:End

Source Code:
import java.util.*;
class saddlepoint
{
public static void main()
{
int r,c=0,n,x=0,r1,max,min,minc,minr,maxr,a=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of rows and
columns");
n=sc.nextInt();
int arr[][]=new int[n][n]; //Initializing array
System.out.println("Enter positive integer
elements:"); //Taking input
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
arr[r][c]=sc.nextInt();
}
}
System.out.println("The original matrix:");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(arr[r][c]); //Printing the
original matrix
}
System.out.println(" ");
}
for(r=0;r<n;r++)
{
min=arr[r][0];
minc=0;
minr=0;
maxr=0;
for(c=0;c<n;c++)
{
if(arr[r][c]<min)
{
min=arr[r][c];
minc=c;
minr=r;
}
}
/*Finding the maximum element in the column
corresponding to the
minimum element of row */
max=arr[0][minc]; // Initializing max with first
element of that column
for(c=0;c<n;c++)
{

if(arr[c][minc]>max)
{
max=arr[c][minc];
}
}
if(min==max)
{
System.out.println("The saddle point is
"+max);
a=1;
}
}
if(a==0)
{
System.out.println("No Saddle Point");
}

}
}

Variable description
Variable Data Type Purpose

n integer To store the


number of rows
and columns
input by user

r integer Iteration
c integer Iteration

max integer Stores the


maximum
element

min character Stores the


minimum element

minr integer Stores the row


number of min

minc ineger Stores the


column number
of min
maxr integer Stores the row
number of max
a integer Counter

Method Description
Methods Description
main() Comprises the entire program, to
declare a square matrix A[ ][ ] of order
‘n’. Accept positive integers into this
matrix. Perform the following tasks on
the matrix: (i)Output the original
matrix (ii) Find the SADDLE POINT
for the matrix. If the matrix has no
saddle point, output the message “NO
SADDLE POINT”.
nextInt() Accepts integer from the user
Scanner(...) Parameterised Constructor

Class Description
Class Description
saddlepoint User Defined Class to , to declare a
square matrix A[ ][ ] of order ‘n’.
Accept positive integers into this
matrix. Perform the following tasks on
the matrix: (i)Output the original
matrix (ii) Find the SADDLE POINT
for the matrix. If the matrix has no
saddle point, output the message “NO
SADDLE POINT”.
Scanner To create an object of Scanner class

Output:
Program No-
Question
Write a program to print the diagonal elements of a matrix in the form of ‘X’.
Algorithm:
Step 1: Begin
Step 2:[Creating objects and initializing variables]
Creating Scanner class object to accept data.
An integer variable n is used to store the number of rows and columns input by the user.
An integer array arr[ ][ ] is initialized
n user input
arr[ ][ ] n,n
Step 3: [Generate loop to accept the elements of the matrix from the user]
for r0 to n-1
{
for c0 to n-1
{
Input: arr[ r ][ c ]
cc+1;
}rr+1;
}
Step 4:[Generate loop to print the elements of the original matrix]
for r0 to n-1
{
for c0 to n-1
{
Print: arr[ r ][ c ]
cc+1;
}rr+1;
}

Step 5: [Generate loop to print the diagonal elements of the matrix]


for r0 to n-1
{
for c0 to n-1
{
If cr , Print arr[r][c]
Else If (r+c)(n-1), Print arr[r][c]
Else Print whitespace
cc+1;
}rr+1;
}

Step 6: End
Source Code:
import java.util.*;
class diagonal
{
public static void main()
{
int r,c,n;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of rows and
columns");
n=sc.nextInt();
int arr[][]=new int[n][n]; //Initializing array
System.out.println("Enter the elements:"); //Taking
input
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
arr[r][c]=sc.nextInt();
}
}
System.out.println("The original matrix:");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(arr[r][c]); //Printing the
original matrix
}
System.out.println(" ");

System.out.println("The diagonal elements:");


for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
if(c==r)
System.out.print(arr[r][c]);
else if((r+c)==(n-1))
System.out.print(arr[r][c]);
else
System.out.print(" ");
}
System.out.println(" ");
}
}
}

Variable description
Variable Data Type Purpose

N integer To store the


number of rows
and columns
input by user

R integer Iteration

C integer Iteration
Method Description
Methods Description
main() Comprises the entire program, to print
the diagonal elements of a matrix in the
form of ‘X’.
nextInt() Accepts integer from the user
Scanner(...) Parameterised Constructor

Class Description
Class Description
diagonal User Defined Class to print the
diagonal elements of a matrix in the
form of ‘X’.
Scanner To create an object of Scanner class
Output:

You might also like