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

Adding Two Sparse Matrices-1

Uploaded by

Impana
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)
38 views

Adding Two Sparse Matrices-1

Uploaded by

Impana
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/ 5

4.

) Develop a C program to perform the following operations on sparse matrix using arrays and
functions

i.) To add 2 sparse matrices

#include<stdio.h>

#define MAX_TERMS 100

typedef struct { int row; int col; int value; }

SparseTerm;

void inputSparseMatrix(SparseTerm matrix[], int *terms, int *rows, int *cols);

void printSparseMatrix(SparseTerm matrix[], int terms);

void addSparseMatrices(SparseTerm A[], int termsA, SparseTerm B[], int termsB, SparseTerm C[], int
*termsC);

int main()

SparseTerm A[MAX_TERMS], B[MAX_TERMS], C[MAX_TERMS];

int termsA, termsB, termsC;

int rowsA, colsA, rowsB, colsB;

printf("Input matrix A:\n");

inputSparseMatrix(A, &termsA, &rowsA, &colsA);

printf("Input matrix B:\n");

inputSparseMatrix(B, &termsB, &rowsB, &colsB);

if (rowsA != rowsB || colsA != colsB) {

printf("Matrices cannot be added (dimensions do not match).\n");

else {

addSparseMatrices(A, termsA, B, termsB, C, &termsC);

printf("Sum of matrices A and B:\n");

printSparseMatrix(C, termsC);

SparseTerm transposed[MAX_TERMS];

int termsTransposed;

printSparseMatrix(transposed, termsTransposed);

return 0;
}

void inputSparseMatrix(SparseTerm matrix[], int *terms, int *rows, int *cols)

printf("Enter the number of rows and columns: ");

scanf("%d %d", rows, cols);

printf("Enter the number of non-zero terms: ");

scanf("%d", terms);

for (int i = 0; i < *terms; i++) {

printf("Enter row, column, and value for term %d: ", i + 1);

scanf("%d %d %d", &matrix[i].row, &matrix[i].col, &matrix[i].value);

void printSparseMatrix(SparseTerm matrix[], int terms){

for (int i = 0; i < terms; i++) {

printf("Row: %d, Col: %d, Value: %d\n", matrix[i].row, matrix[i].col, matrix[i].value);

void addSparseMatrices(SparseTerm A[], int termsA, SparseTerm B[], int termsB, SparseTerm C[], int
*termsC) {

int i = 0, j = 0, k = 0;

*termsC = 0;

while (i < termsA && j < termsB) {

if (A[i].row < B[j].row || (A[i].row == B[j].row && A[i].col < B[j].col)) {

C[k++] = A[i++];

else if (A[i].row > B[j].row || (A[i].row == B[j].row && A[i].col > B[j].col)) {

C[k++] = B[j++];

else {

C[k].row = A[i].row;

C[k].col = A[i].col;
C[k++].value = A[i].value + B[j].value; i++; j++;

while (i < termsA) {

C[k++] = A[i++];

while (j < termsB) {

C[k++] = B[j++];

*termsC = k;

void fastTransposeSparseMatrix(SparseTerm A[], int termsA, SparseTerm B[], int *termsB) {

int rowTerms[MAX_TERMS] = {0};

int position[MAX_TERMS];

*termsB = 0;

for (int i = 0; i < termsA; i++) {

rowTerms[A[i].col]++;

position[0] = 0;

for (int i = 1; i < MAX_TERMS; i++) {

position[i] = position[i - 1] + rowTerms[i - 1];

for (int i = 0; i < termsA; i++) {

int col = A[i].col;

int pos = position[col]++;

B[pos].row = A[i].col;

B[pos].col = A[i].row;

B[pos].value = A[i].value;

for (int i = 0; i < MAX_TERMS; i++) {

if (rowTerms[i] > 0) {
(*termsB)++;

OUTPUT:

ALGORITHM:

Step 1:start

Step 2: Initialization:

Let A[] and B[] be the two sparse matrices in triplet form.

Let m be the number of non-zero elements in matrix A, and n be the number of non-zero elements
in matrix B.

Step 3:Create an empty array C[] to store the result.

Step 4:Initialize three pointers:

i = 0, j = 0, and k = 0. The pointer i will traverse matrix A, j will traverse matrix B, and k will store the
position in result matrix C.

Step 5: Traverse both matrices:


While i < m and j < n (i.e., there are elements left in both A and B):

Step 6:Compare the current triplet in A[i] and B[j].

Case 1: If A[i] is before B[j] (either row or column is smaller), then: Copy A[i] to C[k].
Increment i and k.

Case 2: If B[j] is before A[i] (either row or column is smaller), then: Copy B[j] to C[k].
Increment j and k.

Case 3: If A[i] and B[j] are in the same position (same row and column): Add the values from
A[i] and B[j] and store the result in C[k]. Increment i, j, and k.

Step 7: Handle remaining elements:

If any elements are left in A (i.e., i < m), copy all the remaining elements from A to C[]. If any
elements are left in B (i.e., j < n), copy all the remaining elements from B to C[].

Step 8: Return the result:

The resulting matrix C[] now contains the sum of A and B in triplet form. Return C[] and the number
of non-zero elements in C (k).

Step 9:stop.

You might also like