Professional Documents
Culture Documents
Assignment 2
Assignment 2
-
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 r0 to n-1
{
for c0 to n-1
{
Input: arr[ r ][ c ]
cc+1;
}rr+1;
}
Step 4:[Generate a loop to print the elements of the original matrix]
for r0 to n-1
{
for c0 to n-1
{
Print: arr[ r ][ c ]
cc+1;
}rr+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
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 r0 to n-1
{
for c0 to n-1
{
Input: arr[ r ][ c ]
cc+1;
}rr+1;
}
Step 4:[Generate loop to print the elements of the original matrix]
for r0 to n-1
{
for c0 to n-1
{
Print: arr[ r ][ c ]
cc+1;
}rr+1;
}
For r0 to n
minarr[r][0];
minc0;
minr0;
maxr0;
For c0 to n
If arr[r][c]<min
minarr[r][c];
mincc;
minrr;
}cc+1;
If arr[c][minc]>max
{
maxarr[c][minc];
}cc+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;
a1;
}
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
r integer Iteration
c integer Iteration
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 r0 to n-1
{
for c0 to n-1
{
Input: arr[ r ][ c ]
cc+1;
}rr+1;
}
Step 4:[Generate loop to print the elements of the original matrix]
for r0 to n-1
{
for c0 to n-1
{
Print: arr[ r ][ c ]
cc+1;
}rr+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(" ");
Variable description
Variable Data Type Purpose
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: