0% found this document useful (0 votes)
255 views6 pages

Java Program Add Sparse Matrices

This Java program defines a SparseMatrix class to represent sparse matrices and includes methods to: 1) Insert elements into sparse matrices by row, column, and value 2) Add two sparse matrices by iterating through their non-zero elements and inserting the summed values into a result matrix 3) Print the result matrix to output the addition of the two input sparse matrices

Uploaded by

RASHMI DABRE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
255 views6 pages

Java Program Add Sparse Matrices

This Java program defines a SparseMatrix class to represent sparse matrices and includes methods to: 1) Insert elements into sparse matrices by row, column, and value 2) Add two sparse matrices by iterating through their non-zero elements and inserting the summed values into a result matrix 3) Print the result matrix to output the addition of the two input sparse matrices

Uploaded by

RASHMI DABRE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

import

java.util.Scanner
;
public class SparseMatrixOperations {
private int row, col;
private static int A[][], B[][], C[][];
private Scanner get = new Scanner(System.in);
public SparseMatrixOperations() {
//get the matrix row and coloum size
getMatrixSize();
//set matrix row and coloum size
A = new int[row][col];
B = new int[row][col];
C = new int[row][col];
//set every element are zero in matrix A,B,C
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
A[i][j] = B[i][j] = C[i][j] = 0;
}
}
}
public void getMatrixSize() {
System.out.println("Enter the Matrix Row ,Coloum size : \nLike
row,coloum ");
String rowcol = get.next();
String[] parts = rowcol.split(",");
//check the string ,string array size >2 ,<1
if (parts.length != 2) {
System.out.println("Your entering address not valid !! ");
//recall the method
getMatrixSize();
} else {
//set row and coloum
row = Integer.parseInt(parts[0]);
col = Integer.parseInt(parts[1]);
}
}
public void setMatrix(int X[][]) {
String addres = get.next();
switch (addres) {
default:
String[] parts = addres.split(",");
String part1 = parts[0];
String part2 = parts[1];
int r = Integer.parseInt(part1);
int c = Integer.parseInt(part2);
if (r < row && c < col) {
X[r][c] = 1;
} else {
System.out.println("Entering address not inside the
matrix !!!! ");
}
setMatrix(X);
case "End":
break;
}
}
public void printMatrix(int X[][]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(X[i][j] + "\t");
}
System.out.println();
}
}
public void Operation(int X[][], int Y[][], int Z[][], int Action)
{
switch (Action) {
case 1:
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
Z[i][j] = X[i][j] ^ Y[i][j];
}
}
case 2:
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
Z[i][j] = X[i][j] & Y[i][j];
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SparseMatrixOperations s = new SparseMatrixOperations();
Scanner command = new Scanner(System.in);
System.out.println("------- Get Matrix A,B-------\nStop
entering type End command ");
System.out.println("------- Get Matrix A--------- \nEnter
Address Like Row,Coloum ");
s.setMatrix(A);
System.out.println("------- Get Matrix B--------- \nEnter
Address Like Row,Coloum ");
s.setMatrix(B);
System.out.println("+++++++ Select Perform operations ++++++
+\nSuch as\n1. OR\n2. AND
\nType the number ..... ");
int Action = command.nextInt();
switch (Action) {
case 1:
s.Operation(A, B, C, 1);
case 2:
s.Operation(A, B, C, 2);
}
System.out.println("===== Print Matrix A ======");
s.printMatrix(A);
System.out.println("===== Print Matrix B ======");
s.printMatrix(B);
System.out.println("===== Output Matrix B ======");
s.printMatrix(C);
}
}
Write a program in Java for the addition of two sparse matrices.
Answer : -

public class sparse_matrix


{
int data[][] = new int[100][3];
int row, col, len;

public sparse_matrix(int r, int c)


{
row = r;
col = c;
len = 0;
}

// insert elements into sparse matrix


public void insert(int r, int c, int val)
{
if (r > row || c > col)
{
System.out.println("Wrong Entry");
}
else
{
// insert row value
data[len][0] = r;
// insert col value
data[len][1] = c;
// insert element's value
data[len][2] = val;
// increment number of data in matrix
len++;
}
}
public void add(sparse_matrix b)
{
// if matrices don't have same dimensions
if (row != b.row || col != b.col)
{
System.out.println("Matrices can't be added");
}
else
{
int apos = 0, bpos = 0;
sparse_matrix result = new sparse_matrix(row, col);
while (apos < len && bpos < b.len)
{
// if b's row and col is smaller
if(data[apos][0] > b.data[bpos][0] || (data[apos][0] == b.data[b
{
// insert smaller value into result
result.insert(b.data[bpos][0], b.data[bpos][1], b.data[bpos][2])
bpos++;
}
// if a's row and col is smaller
else if(data[apos][0] < b.data[bpos][0] || (data[apos][0] == b.d
{
// insert smaller value into result
result.insert(data[apos][0], data[apos][1], data[apos][2]);
apos++;
}
else
{
// add the values as row and col is same
int value = data[apos][2] + b.data[bpos][2];
if (value != 0)
result.insert(data[apos][0], data[apos][1], value);
apos++;
bpos++;
}
}
// insert remaining elements
while (apos < len)
result.insert(data[apos][0], data[apos][1], data[apos++][2]);
while (bpos < b.len)
result.insert(b.data[bpos][0], b.data[bpos][1], b.data[bpos++][2
result.print();
}
}

// printing matrix
public void print()
{
System.out.println("Addition Reasult");
for (int i=0; i<="" span="" style="box-sizing: border-box;">
{
System.out.println(data[i][0] + " " + data[i][1] + " " + data[i]
}
}

public static void main(String args[])


{
// Create two sparse matrices and insert values
sparse_matrix a = new sparse_matrix(4, 4);
sparse_matrix b = new sparse_matrix(4, 4);
a.insert(1, 2, 10);
a.insert(1, 4, 12);
a.insert(3, 3, 5);
a.insert(4, 1, 15);
a.insert(4, 2, 12);
b.insert(1, 3, 8);
b.insert(2, 4, 23);
b.insert(3, 3, 9);
b.insert(4, 1, 20);
b.insert(4, 2, 25);
a.add(b);
}
}

You might also like