0% found this document useful (0 votes)
4 views13 pages

Matrix Operations and Algorithms Guide

The document outlines algorithms for various matrix operations, including matrix multiplication, row arrangement, upper and lower triangular matrices, symmetry checks, transposition, saddle point identification, mirroring, wondrous matrix validation, and matrix rotation. Each algorithm follows a structured approach, detailing steps for input, processing, and output. The algorithms utilize nested loops for matrix manipulation and include user input validation for matrix size.
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)
4 views13 pages

Matrix Operations and Algorithms Guide

The document outlines algorithms for various matrix operations, including matrix multiplication, row arrangement, upper and lower triangular matrices, symmetry checks, transposition, saddle point identification, mirroring, wondrous matrix validation, and matrix rotation. Each algorithm follows a structured approach, detailing steps for input, processing, and output. The algorithms utilize nested loops for matrix manipulation and include user input validation for matrix size.
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

Class -- MatProd

Algorithm for main() function


Step 1: Start
Step 2: Declare and initialize variables
 Create a Scanner object sc for user input.

 Declare three 3×3 integer matrices:

o mat1 for the first matrix

o mat2 for the second matrix

o prod for storing the product of mat1 and

mat2
Step 3: Accept elements for mat1
 Display message: "Enter the elements of Matrix

1"
 Use a nested loop:

o Outer loop runs from i = 0 to i < 3 (rows)

o Inner loop runs from j = 0 to j < 3 (columns)

o Read input from sc and store it in mat1[i][j]

Step 4: Accept elements for mat2


 Display message: "Enter the elements of Matrix

2"
 Use a nested loop:

o Outer loop runs from i = 0 to i < 3 (rows)

o Inner loop runs from j = 0 to j < 3 (columns)

o Read input from sc and store it in mat2[i][j]

Step 5: Compute the product of mat1 and


mat2
 Use three nested loops:

o Outer loop runs from i = 0 to i < 3 (rows of

result matrix)
o Middle loop runs from j = 0 to j < 3

(columns of result matrix)


o Inner loop runs from k = 0 to k < 3
(columns of mat1 and rows of mat2)
o Compute prod[i][j] += mat1[i][k] * mat2[k]

[j]
Step 6: Display the resulting matrix
 Display message: "Resulting Matrix ::"

 Use a nested loop:

o Outer loop runs from i = 0 to i < 3 (rows)

o Inner loop runs from j = 0 to j < 3 (columns)

o Print prod[i][j] with space separation

Step 7: End
Class--RowArrange
Algorithm for main() function
Step 1: Start
Step 2: Declare and initialize variables
 Create a Scanner object sc for user input.
 Declare an integer variable temp to store temporary values during sorting.
 Display the message: "Enter the number of rows" and read the value into m.
 Display the message: "Enter the number of columns" and read the value into n.
 Declare a 2D integer array mat of size m × n.
Step 3: Accept elements into the matrix
 Use a loop from i = 0 to i < m (for rows):
o Display message: "Enter the elements of Row: (i+1)"
o Use an inner loop from j = 0 to j < n (for columns):
 Read the input value and store it in mat[i][j].
Step 4: Sort each row in ascending order
 Use a loop from i = 0 to i < m (for each row):
o Use a nested loop from j = 0 to j < n (for columns):
 Use another loop from k = 0 to k < n - j - 1 (for bubble sort):
 If mat[i][k] > mat[i][k+1], swap mat[i][k] and mat[i][k+1].
Step 5: Display the sorted matrix
 Display message "Sorted Matrix:".
 Use a loop from i = 0 to i < m (for rows):
o Use an inner loop from j = 0 to j < n (for columns):
 Print mat[i][j] followed by spaces.
o Print a newline after each row.
Step 6: End
Class--UpperTriang
Algorithm for main() function
Step 1: Start
Step 2: Declare and initialize variables
 Create a Scanner object sc for user input.
 Declare two 3×3 integer matrices:
o arr to store the input matrix.
o utm to store the upper triangular matrix.
Step 3: Accept elements for the matrix
 Display message: "Enter the elements"
 Use a nested loop:
o Outer loop runs from i = 0 to i < 3 (rows).
o Inner loop runs from j = 0 to j < 3 (columns).
o Read input from sc and store it in arr[i][j].
Step 4: Construct the upper triangular matrix
 Use a nested loop:
o Outer loop runs from i = 0 to i < 3 (rows).
o Inner loop runs from j = 0 to j < 3 (columns).
o If j < i, set utm[i][j] = 0.
o Else, set utm[i][j] = arr[i][j].
Step 5: Display the upper triangular matrix
 Display message "Upper Triangular matrix:"
 Use a nested loop:
o Outer loop runs from i = 0 to i < 3 (rows).
o Inner loop runs from j = 0 to j < 3 (columns).
o Print utm[i][j] followed by a space.
o Print a newline after each row.
Step 6: End
Class --SymMat
Algorithm for main() function
Step 1: Start
Step 2: Accept the size of the square matrix
 Create a Scanner object sc for user input.
 Display the message: "Enter the size of Matrix" and read the value into n.
Step 3: Validate the input
 If n is not within the range (2 < n < 10), print "Invalid Input" and exit.
 Otherwise, proceed to initialize the matrix.
Step 4: Declare and input matrix elements
 Declare a 2D array mat of size n × n.
 Use a nested loop to accept values:
o Outer loop runs from i = 0 to i < n (for rows).
o Inner loop runs from j = 0 to j < n (for columns).
o Read input from sc and store it in mat[i][j].
Step 5: Display the original matrix
 Display "ORIGINAL MATRIX:"
 Use a nested loop:
o Outer loop runs from i = 0 to i < n (for rows).
o Inner loop runs from j = 0 to j < n (for columns).
o Print mat[i][j] followed by spaces.
o Print a newline after each row.
Step 6: Check for symmetry
 Declare an integer c = 0 to count mismatches.
 Use a nested loop to compare mat[i][j] with mat[j][i]:
o If mat[i][j] ≠ mat[j][i], increment c.
Step 7: Calculate the sum of diagonals
 Declare two variables:
o ld = 0 for left diagonal sum.
o rd = 0 for right diagonal sum.
 Use a loop from i = 0 to i < n:
o Add mat[i][i] to ld (left diagonal).
o Add mat[i][n - i - 1] to rd (right diagonal).
Step 8: Display results
 If c == 0, print "THE GIVEN MATRIX IS SYMMETRIC MATRIX", else print "THE GIVEN MATRIX
IS NOT A SYMMETRIC MATRIX".
 Print "Sum of Right Diagonal: rd".
 Print "Sum of Left Diagonal: ld".
Step 9: End
Class --TransposeMat
Algorithm for main() function
Step 1: Start
Step 2: Declare and initialize variables
 Create a Scanner object sc for user input.
 Declare a 3 × 3 integer matrix arr to store the input matrix.
 Declare another 3 × 3 integer matrix trans to store the transposed matrix.
Step 3: Accept elements into the matrix
 Display message "Enter the elements".
 Use a nested loop to accept values:
o Outer loop runs from i = 0 to i < 3 (for rows).
o Inner loop runs from j = 0 to j < 3 (for columns).
o Read input from sc and store it in arr[i][j].
Step 4: Compute the transpose of the matrix
 Use a nested loop:
o Outer loop runs from i = 0 to i < 3 (for rows).
o Inner loop runs from j = 0 to j < 3 (for columns).
o Set trans[i][j] = arr[j][i] to swap rows and columns.
Step 5: Display the transposed matrix
 Display message "The transposed matrix is:".
 Use a nested loop:
o Outer loop runs from i = 0 to i < 3 (for rows).
o Inner loop runs from j = 0 to j < 3 (for columns).
o Print trans[i][j] followed by spaces.
o Print a newline after each row.
Step 6: End
Class--LowerTriang
Algorithm for main() function
Step 1: Start
Step 2: Declare and initialize variables
 Create a Scanner object sc for user input.

 Declare two 3 × 3 integer matrices:

o arr to store the input matrix.

o ltm to store the lower triangular matrix.

Step 3: Accept elements into the matrix


 Display the message "Enter the elements".

 Use a nested loop to accept values:

o Outer loop runs from i = 0 to i < 3 (for rows).

o Inner loop runs from j = 0 to j < 3 (for

columns).
o Read input from sc and store it in arr[i][j].

Step 4: Construct the lower triangular matrix


 Use a nested loop:

o Outer loop runs from i = 0 to i < 3 (for rows).

o Inner loop runs from j = 0 to j < 3 (for

columns).
o If j > i, set ltm[i][j] = 0 (zero for upper

triangular elements).
o Else, set ltm[i][j] = arr[i][j].

Step 5: Display the lower triangular matrix


 Display message "Lower Triangular matrix:".

 Use a nested loop:

o Outer loop runs from i = 0 to i < 3 (for rows).

o Inner loop runs from j = 0 to j < 3 (for

columns).
o Print ltm[i][j] followed by spaces.

o Print a newline after each row.

Step 6: End
Class--SaddlePnt
Algorithm for main() function
Step 1: Start
Step 2: Accept the size of the square matrix
 Create a Scanner object sc for user input.

 Display "Enter the size of Matrix" and read the integer n.

 Declare a n × n integer matrix mat.

Step 3: Accept elements into the matrix


 Use a nested loop:

o Outer loop runs from i = 0 to i < n (for rows).

o Inner loop runs from j = 0 to j < n (for columns).

o Read input values from sc and store them in mat[i][j].

Step 4: Display the original matrix


 Print "ORIGINAL MATRIX:".

 Use a nested loop to print the matrix:

o Outer loop runs from i = 0 to i < n.

o Inner loop runs from j = 0 to j < n.

o Print mat[i][j] followed by spaces.

Step 5: Find and count saddle points


 Declare an integer c = 0 to count saddle points.

 Use a nested loop to check each element:

o If mat[i][j] == rowmin(mat, i) and mat[i][j] == colmax(mat, j),

then it is a saddle point.


o Increment c.

Step 6: Display the result


 If c == 0, print "No Saddle Point".

 Else:

o Print "Saddle Points:".


o Reiterate through the matrix and print all saddle points using
the same condition.
Step 7: Define helper functions
 rowmin(matrix, i):

o Initialize min = matrix[i][0].

o Iterate through row i and update min with the smallest value.

o Return min.

 colmax(matrix, j):

o Initialize max = matrix[0][j].

o Iterate through column j and update max with the largest

value.
o Return max.

Step 8: End
Class--MirrorMat
Algorithm for main() function
Step 1: Start
Step 2: Accept the size of the square matrix
 Create a Scanner object sc for user input.
 Declare an integer n to store the size of the matrix.
 Display "Enter the size of Matrix" and read the value of n.
 Check if n is greater than 3 and less than 10.
o If valid, proceed to matrix input.
o Otherwise, print "Invalid Input" and exit.
Step 3: Accept elements into the matrix
 Declare a n × n integer matrix mat.
 Use a nested loop:
o Outer loop runs from i = 0 to i < n (for rows).
o Inner loop runs from j = 0 to j < n (for columns).
o Read input values from sc and store them in mat[i][j].
Step 4: Display the original matrix
 Print "ORIGINAL MATRIX:".
 Use a nested loop to print the matrix:
o Outer loop runs from i = 0 to i < n.
o Inner loop runs from j = 0 to j < n.
o Print mat[i][j] followed by spaces.
o Print a newline after each row.
Step 5: Display the mirror image of the matrix
 Print "REARRANGED MATRIX:".
 Use a nested loop to print the mirror image:
o Outer loop runs from i = 0 to i < n.
o Inner loop runs from j = 0 to j < n.
o Print mat[i][n - j - 1] (reverse columns).
o Print a newline after each row.
Step 6: End
Class--Wondrous
Algorithm for main() function
Step 1: Start
Step 2: Accept the size of the matrix
 Create a Scanner object sc for user input.
 Declare an integer n to store the size of the matrix.
 Display "Enter the size of Matrix" and read the value of n.
 Check if n is between 3 and 10 (both inclusive).
o If valid, proceed to matrix input.
o Otherwise, print "Invalid Input" and exit.
Step 3: Accept elements into the matrix
 Declare a n × n integer matrix mat.
 Use a nested loop:
o Outer loop runs from i = 0 to i < n (for rows).
o Inner loop runs from j = 0 to j < n (for columns).
o Read input values from sc and store them in mat[i][j].
Step 4: Check if the matrix satisfies Wondrous Square properties
 Property 1: It must contain numbers from 1 to n², each appearing exactly once.
o Use a helper function freq(matrix, x) that returns the count of a number x in the
matrix.
o Iterate through all elements of the matrix:
 If any value is out of range [1, n²] or appears more than once, set c = 1 and
break.
 Property 2: The sum of each row and column must be equal to 0.5 * n * (n² + 1).
o Compute the expected sum as sum_expected = 0.5 * n * (n² + 1).
o Iterate through rows and columns:
 Compute rowsum and colsum for each row/column.
 If rowsum or colsum does not match sum_expected, set c = 1 and break.
Step 5: Display the result
 If c == 0, print "Given Square is a Wondrous Matrix".
 Otherwise, print "Given Square is not a Wondrous Matrix".
Step 6: Define helper function freq(matrix, x)
 Initialize a counter c = 0.
 Iterate through the matrix:
o If matrix[i][j] == x, increment c.
 Return c.
Step 7: End
Class--MatRot
Algorithm for main() function
Step 1: Start
Step 2: Accept the matrix size
 Create a Scanner object sc for user input.
 Declare an integer n to store the size of the square matrix.
 Display "Enter the size of Matrix" and read the value of n.
 Validate n:
o It must be greater than 2 and less than 10.
o If invalid, print "Invalid Input" and exit.
Step 3: Accept the matrix elements
 Declare a n × n integer matrix mat.
 Use a nested loop:
o Outer loop runs from i = 0 to i < n (for rows).
o Inner loop runs from j = 0 to j < n (for columns).
o Read input values from sc and store them in mat[i][j].
Step 4: Display the original matrix
 Print "ORIGINAL MATRIX:".
 Use nested loops to print the elements row-wise.
Step 5: Rotate the matrix 90° clockwise
 Declare another n × n matrix rotmat to store the rotated matrix.
 Use nested loops to apply the rotation formula:
rotmat[i][j]=mat[n−j−1][i]

 This shifts the last row to the first column, second-last row to the second column, and so
on.
Step 6: Display the rotated matrix
 Print "ROTATED MATRIX:".
 Use nested loops to print the elements of rotmat row-wise.
Step 7: Compute the sum of the four corner elements
 Formula:
Sum=mat[0][0]+mat[0][n−1]+mat[n−1][0]+mat[n−1][n−1]
 Print the computed sum.
Step 8: End

You might also like