0% found this document useful (0 votes)
48 views2 pages

Sparse Matrix Addition C

The document contains a C program for adding two sparse matrices. It defines a SparseMatrix structure, includes functions to read, print, and add sparse matrices, and checks for compatibility before addition. The main function orchestrates the input and output of the matrices and displays the result.

Uploaded by

koena.sr
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)
48 views2 pages

Sparse Matrix Addition C

The document contains a C program for adding two sparse matrices. It defines a SparseMatrix structure, includes functions to read, print, and add sparse matrices, and checks for compatibility before addition. The main function orchestrates the input and output of the matrices and displays the result.

Uploaded by

koena.sr
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/ 2

Sparse Matrix Addition in C

#include <stdio.h>

#define MAX 100

typedef struct {
int row;
int col;
int val;
} SparseMatrix;

void readSparseMatrix(SparseMatrix sm[], int *size) {


int rows, cols, nonZero, i;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
printf("Enter number of non-zero elements: ");
scanf("%d", &nonZero);

sm[0].row = rows;
sm[0].col = cols;
sm[0].val = nonZero;

for (i = 1; i <= nonZero; i++) {


printf("Enter row, column, and value: ");
scanf("%d %d %d", &sm[i].row, &sm[i].col, &sm[i].val);
}
*size = nonZero + 1;
}

void printSparseMatrix(SparseMatrix sm[]) {


int i, n = sm[0].val + 1;
printf("RowColValue\n");
for (i = 0; i < n; i++) {
printf("%d%d%d\n", sm[i].row, sm[i].col, sm[i].val);
}
}

void addSparseMatrices(SparseMatrix sm1[], SparseMatrix sm2[], SparseMatrix result[]) {


if (sm1[0].row != sm2[0].row || sm1[0].col != sm2[0].col) {
printf("Matrices cannot be added.\n");
return;
}

int i = 1, j = 1, k = 1;
result[0].row = sm1[0].row;
result[0].col = sm1[0].col;
result[0].val = 0;

while (i <= sm1[0].val && j <= sm2[0].val) {


if (sm1[i].row < sm2[j].row || (sm1[i].row == sm2[j].row && sm1[i].col <
sm2[j].col)) {
result[k++] = sm1[i++];
} else if (sm1[i].row > sm2[j].row || (sm1[i].row == sm2[j].row && sm1[i].col >
sm2[j].col)) {
result[k++] = sm2[j++];
} else {
result[k] = sm1[i];
result[k++].val = sm1[i++].val + sm2[j++].val;
}
}

while (i <= sm1[0].val) result[k++] = sm1[i++];


while (j <= sm2[0].val) result[k++] = sm2[j++];

result[0].val = k - 1;
}

int main() {
SparseMatrix sm1[MAX], sm2[MAX], result[MAX];
int size1, size2;

printf("Enter first sparse matrix:\n");


readSparseMatrix(sm1, &size1);
printf("Enter second sparse matrix:\n");
readSparseMatrix(sm2, &size2);

addSparseMatrices(sm1, sm2, result);

printf("First Matrix:\n");
printSparseMatrix(sm1);
printf("Second Matrix:\n");
printSparseMatrix(sm2);
printf("Resultant Matrix after Addition:\n");
printSparseMatrix(result);

return 0;
}

You might also like