KEMBAR78
Array Programs | PDF | Matrix (Mathematics) | Integer (Computer Science)
0% found this document useful (0 votes)
20 views10 pages

Array Programs

Computer programs for 12th isc

Uploaded by

sanemi0911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Array Programs

Computer programs for 12th isc

Uploaded by

sanemi0911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DETERMINING THE GIVEN MATRIX IS SYMMETRIC OR NOT

Write a program to declare a square matrix A[][] of order M x M where M is the number of rows an of columns
such that M must be greater than 2 and less than 10. Accept the value of M as user input. Display an appropriate
message for an invalid input. Allow the user to input integers into this matrix. Perform the following tasks.
(a) Display the original matrix.
(b) Check if the given matrix is symmetric matrix or not.
A matrix is said to be symmetric if the element of the ith row and jth column is equal to the ith column.
Test your program for the following data and some random data:
Example 1:
INPUT:
M= 3
1 2 3
2 4 5
3 5 6
OUTPUT:
ORIGINAL MATRIX:
1 2 3
2 4 5
3 5 6
THE GIVEN MATRIX IS SYMMETRIC
The sum of left diagonal 11

The sum of right diagonal 10

Program
import java.util.Scanner;
class matrix
{
public static void main(String args[])
{
// Program to check a matrix is Symmetric or not.
// Also to find the sum of left and right diagonal elements.
int m.i.j.f-1.s1-0,s2-0
Scanner in new Scanner(System.in);
System.out.println("Enter the value of m");
m-in.nextInt();
if(m>2 && m<10)
{
// Check for validity of rows and columns
int a[][]= new int[m][m];
System.out.println("Enter matrix elements");
// Accept matrix elements
for(i=0;i<m:i++)
{
for(j=0;j<m;j++)
a[i][j]=in.nextInt();
}
System.out.println("ORIGINAL MATRIX");

// Displaying matrix elements

for(i=0;i<m:i++)
{
for(j=0;j<m.j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
for(i=0;i<mi++)
{
for(j=0;j<m.j++)
{ // Checking for symmetric
if( a[i][j] !=a[j][i])
f=0
if(i==j) // Sum of left diagonal elements
s1=s1+a[il[j]
f( i + j ==m-1) // Sum of right diagonal elements
s 2=s2+a[i][j];
}
}
if(f = 1)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC):
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
System.out.println("The sum of the left diagonal =” + s 1) :
System.out.println(" The sum of the right diagonal= ”+s2);
}
else
System.out.println(THE MATRIX SIZE IS OUT OF RANGE")
}
}

Algorithm of checking for Symmetric matrix


[ A is a square matrix having M number of rows and columns. The value of M must be in such a way that
2<M<10. It is to be checked whether the matrix is Symmetric Matrix or not. It is also to be found the sum of left
and right diagonals respectively.

STEPS DESCRIPTION
1. Set the flag variable]
F1

2. [Check for symmetry of the matrix)


I0
Repeat while(I <M)
J0
Repeat while(JM)
if(A[1][j] ne A[j][1] )
F 0
3. [Add left diagonal elements]
if (I=J)
S1=S1+a [I][I]]

4. [Add right diagonal elements]


if(1 + j = M - 1)
S1=S2+ A[I][J]
JJ+1
Endwhile
I I+1
Endwhile
5. [ Print the matrix is symmetric or not along with sum of left and right Diagonal
elements]
if (F=1)
Display("Matrix is Symmetric")
Else
Display("Matrix is not a Symmetric matrix")
Display("Sum of left Diagonal elements S1)
Display("Sum of right Diagonal elements" S2)
6. STOP

Variable Description

Variables Data types Description


a[][] int Double dimensional array.
M int Number of rows and columns
f int Flag variable
c1,sl int Accumulators to store sum of left and right diagonals
respectively

DISPLAYING MIRROR IMAGE OF THE GIVEN MATRIX


Write a program to declare a square matrixA] |[] of order (MM) where 'M' is the number of rows and the
number of columns such that M must be greater than 2 and less than 20. Allow the user to input integers into
this matrix. Display appropriate error message for an invalid input. Perform the following tasks
(a) Display the input matris.
(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.
Test your program for the following data and some random data.
Example 1:
Input: M=3
4 16 12
8 2 14
6 1 3
Output: Original Matrix
4 16 12
8 2 14
6 1 3
Mirror Image Matrix:
12 16 4
14 2 8
3 1 6

// A program to display Mirror Matrix

import java.util.*;

public class Mirror


{
public static void main(String args[])
{
Scanner in new Scanner(System.in);
int M,I,j:
// Enter matrix dimension
System.out.println("M should be greater than 2 and less than 20");
System.out.println("Enter the value of M");
M=in.nextInt();
// Verify range
if(M>2 && M<20)
{
int A[][]=new int[M][M];
// Entering Matrix element
System.out.println("Enter Matrix Elements");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
A[i][j]=in.nextInt();
}
}
// Displaying Original Matrix
System.out.println("Original Matrix");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
//Displaying Mirror Matrix
System.out.println("Mirror Matrix");
for(i=0;i<M;i++)
{
for(j=M-1,j>=0;j-)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
Else
System.out.println("Size is out of range!!!"); 1
}
}

Algorithm of Mirror Matrix

A[M] [M] is a double dimensional array having dimension Mie., M number of rows and M number of columns.
Mirror matrix is to be generated by exchanging columns.

STEPS DESCRIPTION
1. [Accepting elements in the original matrix]
I0
While(I<M)
J0
While(J<M)
Accept(A[1][1])
JJ+1
E
Endwhile
I I+1
Endwhile

2. [Displaying Original Matrix)


I0
While(I<M)
J0
While(J<M)
Display (A[I][J])
JJ+1
Endwhile
I I+1
Endwhile
3. [Creating Mirror Matrix]
I 0
While(I<M)
JM-1
While(1>=0)
Display (A[I][J])
J-J-1
Endwhile
11+1
Endwhile

4. STOP

Variable Description
Variable Data Type Description
A[][] int Array to contain matrix elements
M int Size of the array
I int Loop variable
J int Loop variable

Write a program to declare a matrix A [||| of order (M x N) where 'M' is the number of rows and 'N' is the
number of columns such that both M and N must be greater than 2 and less than 20. Allow the user to input
integers into this matrix. Perform the following tasks on the matrix:
Display the input matrix
Find the maximum and minimum value in the matrix and display them along with their position.
Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange them in
the matrix.
Output the rearranged matrix.
Sample input Output
Input:
M=3
N=4
Entered values: 8,7,9,3,-2,0,4,5,1,3,6-4
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: 4
Row-2
Column 3

Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8 9

// Program to perform matrix operation


import java.io.*;
class Matrix
{
InputStreamReader read-new InputStreamReader(System.in);
BufferedReader in new BufferedReader(read);
int arr[]]
int r.c.max.min.max1,max2,min1,min2,i,j,m,n,
public void matrix()throws IOException
{
//Check for the validity of dimension
boolean bn=true;
while(bn)
{
System.out.println("Enter the number of rows");
r-Integer.parseInt(in.readLine());
System.out.println("Enter the number of columns:");
c-Integer.parseInt(in.readLine());
if(r<2 || c<2 || r>20 || c>20)
bn=true:
else
bn=false;
}
arr[][]=new int[r][c];
//Enter array elements
for(i=0;i< r;i++)
for(j=0;j< cj++)
{
{
System.out.println("Enter Value:");
arr[i][j]=Integer.parseInt(in.readLine());
}
}
//Initialize the variables
max= arr[0][0];
min=arr[0][0];
max1=0;
min1=0;
max2=0;
min2=0;
//Finding maximum and minimum elements among the array elements along with row
//number as well as column number in which it contains
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
if(arr[i][j]>max)
{
max =arr[i][j]:
max1=i;
max2=j;
}
else if(arr[i][j]< min) 1
{
min1=i
min2=j;
min= arr[i][j]:

System.out.println("Original Array");
for(i=0;i< r;i++)

{
for(j=0;j< c;j++)
{System.out.print(arr[i][j]+" ");
}System.out.println();
}
System.out.println("Maximum Value="+max);
System.out.println("Row="+max1);
System.out.println("Column="+max2);
System.out.println("Minimum Value="+min);
System.out.println("Row="+minl);.
System.out.println("Column="+min2);
//Sort the array elements in ascending order
for(m=0;m r;m++)
{for(n=0;n< c;n++)
{for(i=0;i< r;i++)
{for(j=0;j< c;j++)
if(arr[m][n]< arr[i][j]) 1
{
min=arr[m][n];
arr[m][n]=arr[i][j];
arr[i][j]=min;

}
}

//Print the array elements in order


System.out.println("Sorted Array");
for(i=0;i< ri++)
{
for(j=0;j<cj++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
public static void main(String args[]) throws IOException
{
Matrix ob= new Matrix();
ob.matrix():
}
}

Algorithm matrix(ARR,.R.C)

[ARR is a double dimensional array containing R rows and C columns. Maximum and minimum of the
elements stored in the array is to be obtained and array elements are to be sorted in ascending order]
Steps Description
[Initialize the variables]
MAX=ARR[0][0]
MIN=ARR[0][0]
MAXI0
MAX20
MIN10
MIN2 0

[Find maximum and minimum]


Repeat for 1-0,1,2 R
Repeat for J-0,1,2 …….C
if(ARR[I][J]>MAX)
then
MAXARR[I][J]
MAX11
MAX2J
else
if(ARR[I][J]<MIN) Then
MINARR[I][J]
MINII
MIN2J
Endif
Endfor I
Endfor J
Display("Maximum", MAX)
Display("Maximum in row", MAX1)
Display("Maximum in col" MAX2)
Display("Minimum", MIN)
Display("Minimum in row", MINI)
Display("Minimum in col", MIN2)

[Sort the elements of the array]


Repeat for M-0,1,2.. R
Repeat for N-0,1.2. C
Repeat for 1-0,1,2 R
Repeat for J-0,1,2 C
if (ARR[M][N] < ARR[I][J]) then
MINARR[M][N]
ARR[M][N]ARR[I][J]
ARR[I][J]MIN
Endfor J
Endfor I
Endfor N
Endfor M
` [Display the array elements in order]
Repeat for 1-0,1,2, R
Repeat for J=0,1,2 C
Display (ARR[I][J])
Endfor J
Endfor I

Return

Variable Description:

Variable Type Description

Arr[][] int Array to contain numbers


max int To contain maximum element
max1 int To contain row number having maximum
max2 int To contain col number having minimum
min int To contain minimum number
minl int To contain row number having minimum
min2 int To contain col number having minimum
i, j, m, n int Loop variables

You might also like