# Write a C program to convert a matrix to a sparse matrix and perform addition on two sparse matrices.

If most of the value of a matrix is 0, then it is called a sparse matrix. There are lesser non-zero elements thus lesser memory is utilised and we can save computational time by logically designing a data structure that only traverses non-zero elements.

``````#include <stdio.h>
void addition(int ktemp, int ltemp, int k, int l);
int main()
{
int a, ktemp, n, m, count = 0, i, j, k = 0, b, ltemp, l = 0, p, q;

printf("Enter no of rows of first matrix");
scanf("%d", &n);
printf("Enter no of columns of first matrix");
scanf("%d", &m);
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
printf("Enter %d%d element", i, j);
scanf("%d", &a[i][j]);
if (a[i][j] != 0)
{
ktemp[k] = i;
ktemp[k] = j;
ktemp[k] = a[i][j];
k++;
}
}
}
printf("\n Sparse matrix is \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < k; j++)
{
printf("%d ", ktemp[i][j]);
}
printf("\n");
}

printf("Enter no of rows of second matrix");
scanf("%d", &p);
printf("Enter no of columns of second matrix");
scanf("%d", &q);
if (p != m || q != n)
{
printf("\n Addtition can be performed on matrix of same order");
}
else
{
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
printf("Enter %d%d element", i, j);
scanf("%d", &b[i][j]);
if (b[i][j] != 0)
{
ltemp[l] = i;
ltemp[l] = j;
ltemp[l] = b[i][j];
l++;
}
}
}

printf("\n Sparse matrix is \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < l; j++)
{
printf("%d ", ltemp[i][j]);
}
printf("\n");
}

return 0;
}
}

void addition(int ktemp, int ltemp, int k, int l)
{
int i = 0, j = 0, sparse, x = 0;

while (i < k && j < l)
{
if ((ktemp[i] == ltemp[j]) && (ltemp[j] == ktemp[i]))
{
printf("Entered this");
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
sparse[x] = ltemp[j] + ktemp[i];
x++;
i++;
j++;
}
else
{
if (ktemp[i] < ltemp[j])
{
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
x++;
i++;
}

else
{
if ((ktemp[i] == ltemp[j]) && (ktemp[i] < ltemp[j]))
{
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
x++;
i++;
}
else
{
sparse[x] = ktemp[j];
sparse[x] = ktemp[j];
sparse[x] = ktemp[j];
x++;
j++;
}
}
}
}

while (i < k)
{
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
sparse[x] = ktemp[i];
x++;
i++;
}
while (j < l)
{
sparse[x] = ktemp[j];
sparse[x] = ktemp[j];
sparse[x] = ktemp[j];
x++;
j++;
}

for (i = 0; i < 3; i++)
{
for (j = 0; j < x; j++)
{
printf("%d", sparse[i][j]);
}
printf("\n");
}
}

``````

Learning –

Time complexity traversing simple matrix – O(n^2)

Time complexity traversing sparse matrix – O(n)

1. CBD for Sale

I do consider all of the concepts you have presented in your post.
They’re very convincing and can certainly work.
Nonetheless, the posts are very quick for beginners. May you please lengthen them a little from
next time? Thanks for the post.

2. Kevin

This is a topic that’s close to my heart… Many thanks!
Exactly where are your contact details though?

3. It’s amazing to visit this web page and reading the views of all mates regarding
this piece of writing, while I am also zealous of getting know-how.

4. www.magcloud.com

I am sure this paragraph has touched all the internet viewers, its really really fastidious piece of
writing on building up new weblog.