c program to add sparse matrices

list of programs below:  c program to add sparse matrices i.e. c program to add sparse two matrices, c program to add three sparse matrices

A sparse matrix is a matrix having maximum no, of elements as zero

Example of sparse matrix

1 0 0 0
0 0 2 0
0 0 0 0

Since sparse matrices have many zero’s for storage efficiency sparse matrices are represented in other way using a two dimentional array( three tuple format ) i.e row column and element. In this repesentation only non zero elements are taken. example the above sparse matrix is represented in 3-tuple format as

r  c  element
0 0 1
1 2 2

r represents row no. c represents column number and element is value at  that row and column.

c program to add sparse matrices using functions, c program to add three sparse matrices using functions
c program to add sparse matrices using functions

write a C program to add sparse matrices using functions

write a C program to add sparse matrices using functions

#include < stdio.h >
#include < conio.h >
void main()
{
int sp1[2][3],sp2[2][3],sp3[2][3];
clrscr();
printf(“\n OUTPUT:C program to add two sparse matrices \n”);
printf(“\n Enter first sparse matrix”);
read_sparse(sp1);
printf(“\n Enter second sparse matrix”);
read_sparse(sp2);
printf(“\n First sparse matrix  in 3-tuple format is”);
print_sparse(sp1);
printf(“\n Second sparse matrix in 3-tuple format is “);
print_sparse(sp2);
printf(“\n addition of two sparse matrices in 3-tuple format is\n”, addition_sparse(sp1,sp2));
} // end of main

// read sparse matrix
int read_sparse(int sp[2][3]){
int i,j,k,t;
printf(“\nEnter the elements of sparse matrix \n”);
k=1;
for(i=0;i < 2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d \t”,&t);

if( t != 0 )
{
sp[i][j] = t;

} // if
j++;
printf(“\n”);
}; // for
i++;
}
} // end of read sparse matrix

// print sparse matrix
int print_sparse(int sp[2][3]){
printf(“\n I \t c \t element”)
int i=0,j=0;
for(i=0;i < 2;i++)
{
for(j=0;j<3;j++)
{
if (sp[i][j] !=0)
{
printf(“\n %d \t %d \t %d \n” i, j , sp[i][j]);

} // end of print sparse

// add sparse matrices

int addition_sparse(sp1,sp2)
{
int SPARSE_RESULT[2][3];
int i,j;
for(i=0;i < 2;i++)
{
for(j=0;j<3;j++)
{
SPARSE_RESULT[i][j]= sp1[i][j] + sp2[i][j] ;
}
}
print_sparse(SPARSE_RESULT);
// end of sparse addition
}

output:

OUTPUT:C program to add two sparse matrices

Enter first sparse matrix
Enter the elements of sparse matrix
1 0
0 0
0 1
Enter second sparse matrix
Enter the elements of sparse matrix
0 0
0 1
0 3

First sparse matrix in 3-tuple format is:
r c element
0 0 1
2 1 1

Second sparse matrix in 3-tuple format is:
r  c element
1 1 1
2 1 3

addition of two sparse matrices in 3-tuple format is
r c element
0 0 1
1 1 1
2 1 4

write a C program to add three sparse matrices using functions

write a C program to add sparse matrices using functions

#include < stdio.h >
#include < conio.h >
void main()
{
int sp1[2][3],sp2[2][3],sp3[2][3];
clrscr();
printf(“\n OUTPUT: c program to add three sparse matrices \n”);
printf(“\nEnter first sparse matrix”);
read_sparse(sp1);
printf(“\nEnter second sparse matrix”);
read_sparse(sp2);
printf(“\nEnter third sparse matrix”);
read_sparse(sp3); xzc
printf(“\nFirst sparse matrix in 3-tuple format is”);
print_sparse(sp1);
printf(“\nSecond sparse matrix in 3-tuple formatis”);
print_sparse(sp2);
printf(“\nThird sparse matrix in 3-tuple format is”);
print_sparse(sp3);

printf(“\n addition of three sparse matrices in 3-tuple format is\n”, addition_sparse(sp1,sp2,sp3));
} // end of main

// read sparse matrix
int read_sparse(int sp[2][3]){
int i,j,k,t;
printf(“\nEnter the elements of sparse matrix \n”);
k=1;
for(i=0;i < 2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d\t”,&t);

if( t != 0 )
{
sp[i][j] = t;

} // if
j++
printf(“\n”);
}; // for
i++;
}
} // end of read sparse matrix

// print sparse matrix
int print_sparse(int sp[2][3]){
printf(“\n r \t c \t element”)
int i=0,j=0;
for(i=0;i < 2;i++)
{
for(j=0;j<3;j++)
{
if (sp[i][j] !=0)
{
printf(“\n %d \t %d \t %d \n” i, j , sp[i][j]);

} // end of print sparse

// add sparse matrices

int addition_sparse(sp1,sp2,sp3)
{
int SPARSE_RESULT[2][3];
int i,j;
for(i=0;i < 2;i++)
{
for(j=0;j<3;j++)
{
SPARSE_RESULT[i][j]= sp1[i][j] + sp2[i][j] + sp3[i][j]
}
}
print_sparse(SPARSE_RESULT);
// end of sparse addition
}

output:

OUTPUT: c program to add three sparse matrices

Enter first sparse matrix
Enter the elements of sparse matrix
o 1
2 0
0 1
Enter second sparse matrix
Enter the elements of sparse matrix
0 0
1  1
1  0
Enter third sparse matrix
Enter the elements of sparse matrix
0 0
0  1
0 0

First sparse matrix in 3-tuple format is:
r c element
0 1 1
1 0 2
2 1 1

Second sparse matrix in 3-tuple format is:
r c element
1 0 1
1 1  1
2 0 1

Third sparse matrix in 3-tuple format is:
r c element
1 1 1

addition of two sparse matrices in 3-tuple format is
r c element
0 1 1
1 0 3
1 1 2
2 0 1
2 1  1

Other programs:

Static variable scope
Register variable scope
External variable scope
Automatic variable scope
amicable number
Prime number
Perfect number
Palindrome number
Strong number
Find Factorial 
Biggest Number
Average of n numbers
decimal number to octal
convert decimal number to hexadecimal
decimal number to binary
simple interest
compound interest
Add sparce matrices
Add matrices
 file copying
Palindrome string
Reverse string
String length
Replace characters in string
substring or not using arrays
First occurrence of character in string
Last occurrence of character in string
Convert to uppercase
Convert to lowercase
Concatenate strings 
Compare strings
Generate Employee payslips
Count students scoring marks>50%
convert weight into grams

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *