0% found this document useful (0 votes)
4 views

Algorithm (2)

The document outlines various Java programs focusing on matrix operations, including creating diagonal matrices, reversing matrix elements, checking matrix equality, and performing calculations on matrices. Each program includes a detailed algorithm and class structure, specifying data members and member functions. The document serves as a guide for implementing these matrix-related functionalities in Java.

Uploaded by

alexheathcote237
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithm (2)

The document outlines various Java programs focusing on matrix operations, including creating diagonal matrices, reversing matrix elements, checking matrix equality, and performing calculations on matrices. Each program includes a detailed algorithm and class structure, specifying data members and member functions. The document serves as a guide for implementing these matrix-related functionalities in Java.

Uploaded by

alexheathcote237
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Program-26

Write a program in java to print diagonal matrix.

Algorithm

 Step 1: Start.
 Step 2: Accept the value of n and create the array a [] of size n and b [] of
size n*n.
 Step 3: Enter element in the array a [].
 Step4: From i=0 to n-1 repeat step 5 to step 6.
 Step 5: From j=i+1 to j<n repeat step 6.
 Step 6: If a[j]>a [j+1] then interchange the element of a[j] with a [j+1].
 Step 7: Execute a for loop from i=n -1, r=0 up to i=0.
 Step 8: Execute an inner for loop for j=0 to j<=I and copy the element of
array a [] into b[r][j]=a[j].
 Step 9: Print the filled matrix.
 Step 10= Stop.

Program-27

Design a class matrev to reverse each element in matrix

Class name: MatRev

Data member/instance variables:

arr[][]: to store the integer elements.

m: to store the no.of rows.

n: to store the no.of columns.

Member function:

Matrev (int mm, int nn): parameterized constructor to initialize the data
member m=mm and n=nn.

voidfillarray (): to enter element in the array.

Int reverse (int x): returns the reverse of the number x.


Void revMat (MatRev t): reverse each element of the array of the parameterized
object and store it in the array of the current object.

Void shone (): display the array elements in matrix form.

Define the class MatRev giving details of the constructor(),void fillarray (),int
reverse (int),void revMat (MatRev)

And void shone().Define the main() fn to create objects and call the functions
accordingly.

Algorithm

 Step 1: Start.
 Step 2: Accept the number of row and column from the user and store it
to variable m and n respectively.
 Step 3: Create the array arr [m] [n] and fill the array with integer values.
 Step 4: Call the function RevMat() to reverse each element of the array .
 Step 5: Call the function show() to display the array element in the matrix
format.
 Step 6: Stop.

Program-28

Write a program in Java with the following details:

Class name : Composite

Data member :

arr[][] : to store composite number columnwise .

m : to store number of rows.

n : to store number of columns.

Member method :

Composite(int mm, int nn) : initialize the size of method m=mm,n=nn.


Int isComposite (int p) : return 1 if the number is composite ,otherwise 0.

Void fill() : fill the element in the array with first (m*n) composite number
column wise .

Void display(): display the array in matrix form .

Write main function and call the function accordingly .

Algorithm

 Step 1: Start.
 Step 2: Accept the no of row and column from this and store it in
variable r and c respectively.
 Step 3: Create object of class composite and all
all the function fill the matrix with composite numbers.
 Step 4: Create for loop and the function isComposite to fill each cell of the
matrix with composite number.
 Step 5: Display the matrix.
 Step 6: Stop.

Program-29

Write a program to declare a matrix of order m*n where m is the number of row and n is the
number of column . Such that value m>0 and m<10 and n>2 and n<6.

Allow the user to input digit (0-7) only at each location such that each row represent an octal
number . Perform the following task :

i) Display the original matrix :


ii) Calculate the decimal equivalent for each row.

Algorithm

 Step 1: Start.
 Step 2: Accept the size of row and column and store it in variable m and n
respectively.
 Step 3: If m<=0 and m>=10 and n<=2 or n>=6, then print out of range and
exit the program.
 Step 4: Create array a [] [] of size m*n.
 Step 5: Accept the element in the array.
 Step 6: Create for loop and calculate decimal equivalent of each row of
the entire matrix and display the result.
 Step 7: Stop.

Program-30
Design a class EqMat to check if two matrices are equal or not .Assume that the two matrices
have the same dimension .Some of the member of the class are given below :

Class name : EqMat

Data member :

a[][] : to store elements.

m: to store no.of rows.

n: to store no.of columns.

Member function :

EqMat (int mm, int nn) : parameterized constructor to initialize m=mm,n=nn.

Void readarray():Enter element in the array int check (EqMat P, EqMat Q):
To check If the object P and Q are equal and return 0.

Void print(): display the array elements.

Algorithm

 Step 1: Start.
 Step 2: Accept the number of rows and column and store in variable m and n
respectively.
 Step 3: Create two objects of class EqMat ob1 and ob2.
 Step 4: Call the function read array() to accept element in the array.
 Step 5: Call the function check passing two object ob1 and ob2 and store the return
result in variable p.
 Step 6: If p=1 then display matrices are equal else display matrices are not equal.
 Step 7: Display both the matrices.
 Step 8: Stop.

Program-31

Design a class shift to shuffle the matrix of elements row wise.

Algorithm

 Step 1: Start.
 Step 2: Accept the number of rows and columns and store it in variable m and n
respectively.
 Step 3: Create the array mat[m] [n] and enter element in the array.
 Step 4: Call the function Cyclic() by passing object to shuffle the row of the matrix.
 Step 5: Display the array after shuffling.
 Step 6: Stop.

Program-32

Design a class disarium to check a number is disarium or not.

Class name : Disarium

Data member :

Int num : to store the number.

Int size : to store the size of the number.

Member function :

Disarium (int nn) : parameterizrd constructor to initialize

Void CountDigit(): count total no.of digit and assigned to size .


Int SumofDDigit(int n, int p): return sum of digit of the number (n) to the power of there
repective position (p) using recursive technique.

Void check() : check whether the number and display the result with a appropriate message.

Algorithm

 Step 1: Start.
 Step 2: Accept a number and store it in variable a.
 Step 3: Create object of the class and initialize data member of the class.
 Step 4: Call the function CountDigit() to count total number of digit in the number and
store it to variable size.
 Step 5: Call the function SumofDigit() to return the sum of digit to the power of their
respective position.
 Step 6: Check whether the return value from the function SumofDigit() is equal to the
input number the display ‘disarium number’ other wise display ‘not disarium number’.
 Step 7: Stop.

Program-33

A class palin has been defined to check whether a positive number is a palindrome number or
not.

The number ‘N’ is a palindrome if the original number and its reverse are same .

Class name : Palin

Data member :

num : integer to store the number.

revnum : integer to store the reverse of the number.

Methods :

Palin() : constructor to initialize data members with legal initial values.

Void accept() : to accept the number.


Int reverse(int y) : reverse the parameterized argument ‘y’ and stores it in ‘refnum’using
recursive technique.

Void check():checks weather the number is palindrome or not by invoiking the function
reverse()and display the result with an appropriate message

Algorithm

 Step 1: Start.
 Step 2: Accept a number and store it in num.
 Step 3: Call the function reverse passing the num as argument. The function will return
the reverse of the past value.
 Step 4: Call function check() to check whether the reverse number and original
number are same then display the message “palindrome number”.
 Step 5: Stop.

Program-34

A class designed to calculate the sum of following series

Some of the details of the class are given below:

Class name:series sum

Data members:x:to store an integer number

N:to store number of terms

Sum:double variable to store the sum of the series

Member function

Series sum(int xx,int nn):constructer to x=xx and n=nn


Double findfact(intm):to return the factorial of m using recuersive technique

Double findpower(intx,int y):to return x raise to the power of y using recursive technique

Void calculate():to calculate the sum of series by invoking the recursive functions

Void display():to display the sum of series.

Algorithm

 Step 1: Start.
 Step 2: Accept the value of x and n.
 Step 3: Call the function calculate() to calculate the sum of given series by invoking the
function findfact() and findpower().
 Step 4: Call the function display() to display the sum of the series.
 Step 5: Stop.

Program 35

Write a class program to perform the matrix operations as per the specifications shown
below

Class :matrixop

Data members

Arr[][]:double dimentional array with size

M,n;integer to store a value

Member methods:constructor to ititialise the data members

Void get():to store sum of squares of row number and coloumn number in each of the matrix

Void show():to print the matrix elements

Void cal():to print the sum of the prime numbers available on the left diagonal as well as sum
of the elements except boundary elements.

Define the main () functions ro make use of all other functions shown above.

Algorithm

 Step 1 : Start
 Step 2 : Create an array arr[20][20].
 Step 3 :Call the function get () to store the sum of square of row number and column
number in each of the corresponding call of the array.
 Step 4 : Call the function low() to display the element of the array.
 Step 5 : call the function cal () to display the sum of prime number on the left diagonal
as well as sum of elements except boundary elements.
 Step 6 : Stop.

You might also like