answersLogoWhite

0

C program to find sum of two sparse matrices?

Updated: 8/18/2019
User Avatar

Wiki User

11y ago

Best Answer

#include <stdio.h> #include <stdlib.h> typedef struct node { int column; int value; int row; struct node *next; } element; /* MENU */ void Menu() { printf("\n***\tADDING SPARSE MATRICES\t***\n"); printf("\n 1.) Insert in A"); printf("\n 2.) Insert in B"); printf("\n 3.) Printout both"); printf("\n 4.) A + B = "); printf("\n 0.) EXIT"); printf("\nChoose ---------> "); } /* Initialize*/ void Init (element *x[]) { int i; for (i=0; i<3; i++) x[i] = NULL; } /* Printout */ void Printout (element *x[]) { int i, test = 0; element *temp; for (i=0; i<3; i++) { temp = x[i]; while (temp != NULL) { printf("Element position (%d,%d) = %d\n", i, temp->column, temp->value); test = 1; temp = temp->next; } } if (test == 0) printf("This matrix is empty!!\n"); } /* INSERT */ void Insert(element *x[], int row, int column, int value) { int r = row; element *p; element *new = malloc(sizeof(element)); new->row = row; new->column = column; new->value = value; if (x[r] == NULL) { x[r] = new; new->next = NULL; } else { p = x[r]; if (new->column < p->column) { new->next = p; x[r] = new; } else if (new->column > p->column) { while (p->next != NULL && p->next->column < new->column) { p = p->next; } new->next = p->next; p->next = new; } else printf("An element already exists there!!\n"); } } /* A + B */ void Add (element *x[],element *y[],element *z[]) { int i; element *p, *q; for (i=0; i<3; i++) { /*row of x empty, row of y not*/ if (x[i] == NULL && y[i] != NULL) { q = y[i]; while (q != NULL) { Insert(z, i, q->column, q->value); q = q->next; } } /*row of y empty, row of x not*/ if (x[i] != NULL && y[i] == NULL) { q = x[i]; while (q != NULL) { Insert(z, i, q->column, q->value); q = q->next; } } /*row of x and y not empty*/ if (x[i] != NULL && y[i] != NULL) { p = x[i]; q = y[i]; /*stay in while loop until one row runs out*/ while (p != NULL q != NULL) { /*columns match*/ if (p->column == q->column && p != NULL && q != NULL) { Insert(z, i, p->column , p->value + q->value); p = p->next; q = q->next; } /*column of x is smaller than column of y*/ else if (p->column < q->column) { Insert(z, i, p->column , p->value); p = p->next; } /*column of x is bigger than column of y*/ else { Insert(z, i, q->column , q->value); q = q->next; } } /*if row of x had more elements and didn't run out*/ if (p != NULL) { while (p != NULL) { Insert(z, i, p->column , p->value); p = p->next; } } /*if row of x had more elements and didn't run out*/ if (q != NULL) { while (q != NULL) { Insert(z, i, q->column , q->value); q = q->next; } } } } Printout(z); } /* FREE MEMORY*/ void Freedom (element *x[]) { int i; element *p; for (i=0; i<3; i++) { if (x[i] != NULL) { p = x[i]; x[i] = x[i]->next; free(p); } } } /* MAIN */ int main (int argc, const char * argv[]) { int choice, column, row, value, number; element *a[3], *b[3], *sum[3]; Init(a); Init(b); Init(sum); do { Menu(); scanf("%d",&choice); switch (choice) { case 1: /*Insert in A */ do { printf("Enter row -> "); scanf("%d",&row); } while (row < 0 row > 3); do { printf("Enter column -> "); scanf("%d",&column); } while (column < 0); printf("Enter value -> "); scanf("%d",&value); Insert(a,row,column,value); break; case 2: /*Insert in B */ do { printf("Enter row -> "); scanf("%d",&row); } while (row < 0 row > 2); do { printf("Enter column -> "); scanf("%d",&column); } while (column < 0); printf("Enter value -> "); scanf("%d",&value); Insert(b,row,column,value); break; case 3: /* Printout A & B */ printf("\n::::::: MATRIX A :> \n\n"); Printout(a); printf("\n::::::: MATRIX B :> \n\n"); Printout(b); break; case 4: /* A + B */ Init(sum); printf("\n::::::: MATRIX A + B :> \n\n"); Add(a,b,sum); break; default: printf("\nWRONG CHOICE"); } } while (choice != 0); Freedom(a); Freedom(b); Freedom(sum); return 0;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program to find sum of two sparse matrices?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a C program to find sum of 3 matrices?

matrix


Write a program to print the sum of a sparse matrix?

This is a directive, not a question.


Write a c program using dynamic memory allocation function calloc to find sum of two matrices and also print the resultant matrix?

printf("%s",per&gt;50?:"pass",per&lt;50?:"fail");


What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Write a program to find the sum of all positive number and terminate the program when sum exceed 999?

int sum = 0; int n = 0; while( sum &lt;= 999 ) { sum += (++n); }


When adding or subtracting matrices do the dimensions of the sum or differences always match the original matrices?

Yes, because otherwise addition and subtraction are not defined.


Writes a c program to find the sum of all integers between 1 and n?

Write a program to find the number and sum of all integers from 100 to 300 that are divisible by 11


Java program to find sum on even numbers from 12-45?

sum = 0; for (int i = 12; i


How do you add matrices?

You add matrices by adding their respective terms - e.g. the element in the first row and sixth column of the sum is the sum of the elements in the addends' first rows and sixth columns. Wikipedia has a nice example of matrix addition that I linked below.


Write a C program using dynamic memory allocation to find the sum of elements of a matrix?

Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.


What is multiwavelet?

A generalization of wavelets that uses vector valued functions as scaling and wavelet functions. The two scale equation contains a sum of matrices instead of sum of scalars.