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

p 7

The document outlines a program to represent and display a sparse matrix using C programming language. It includes source code for reading a matrix, creating its sparse representation, and printing the sparse form. Execution results demonstrate successful test cases with user inputs and corresponding outputs.

Uploaded by

lokeshjasti07
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)
2 views

p 7

The document outlines a program to represent and display a sparse matrix using C programming language. It includes source code for reading a matrix, creating its sparse representation, and printing the sparse form. Execution results demonstrate successful test cases with user inputs and corresponding outputs.

Uploaded by

lokeshjasti07
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/ 3

7/4/22, 6:34 AM https://kitsw.codetantra.com/secure/labs-q.jsp?

sNo=7&qId=619cc6ee8c026006a269ef05&bd=AMjc0X2N0X2No&lid=624d781…

S.No: 7 Exp. Name: Write a program to represent and display sparse matrix. Date: 2022-06-14

Aim:

Page No:
Write a program to represent and display sparse matrix.
Source Code:

ID: B21ME015
sparseMatrix.c

#include<stdio.h>
#define MAX 20
void read_matrix(int a[10][10],int row,int column);
void print_sparse(int b[MAX][3]);
void create_sparse(int a[10][10],int row, int column, int b[MAX][3]);
int main()
{
int a[10][10],b[MAX][3], rows, column;
printf("Enter the size of matrix (rows, columns): ");
scanf("%d%d",&rows,&column);
read_matrix(a,rows,column);
create_sparse(a,rows,column,b);
print_sparse(b);
return 0;
}
void read_matrix(int a[10][10],int row,int column)
{
int i,j;
printf("Enter elements of matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
printf("[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}

2021-2025-MECH-1
}
void create_sparse(int a[10][10],int row,int column,int b[MAX][3])
{
int i,j,k;
k=1;
b[0][0]=row;
Kakatiya Institute of Technology and Science

b[0][1]=column;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(a[i][j]!=0)
{
b[k][0]=i;
b[k][1]=j;
b[k][2]=a[i][j];
k++;
}
}

https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=7&qId=619cc6ee8c026006a269ef05&bd=AMjc0X2N0X2No&lid=624d7816232d4a06d607281… 1/3
7/4/22, 6:34 AM https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=7&qId=619cc6ee8c026006a269ef05&bd=AMjc0X2N0X2No&lid=624d781…
b[0][2]=k-1;
}
}
void print_sparse(int b[MAX][3])

Page No:
{
int i,column;
column=b[0][2];
printf("Sparse form - list of 3 triples\n");

ID: B21ME015
for(i=0;i<=column;i++)
{
printf("%d\t%d\t%d\n",b[i][0],b[i][1],b[i][2]);
}
}

2021-2025-MECH-1
Kakatiya Institute of Technology and Science

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the size of matrix (rows, columns): 2 2
Enter elements of matrix 0
[0][0]: 0
[0][1]: 0
https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=7&qId=619cc6ee8c026006a269ef05&bd=AMjc0X2N0X2No&lid=624d7816232d4a06d607281… 2/3
7/4/22, 6:34 AM https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=7&qId=619cc6ee8c026006a269ef05&bd=AMjc0X2N0X2No&lid=624d781…

Test Case - 1
[1][0]: 1
[1][1]: 2

Page No:
Sparse form - list of 3 triples
2 2 2
1 0 1
1 1 2

ID: B21ME015
Test Case - 2

User Output
Enter the size of matrix (rows, columns): 3 3
Enter elements of matrix 0
[0][0]: 0
[0][1]: 0
[0][2]: 3
[1][0]: 0
[1][1]: 4
[1][2]: 0
[2][0]: 0
[2][1]: 5
[2][2]: 7
Sparse form - list of 3 triples
3 3 4
0 2 3
1 1 4
2 1 5
2 2 7

2021-2025-MECH-1
Kakatiya Institute of Technology and Science

https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=7&qId=619cc6ee8c026006a269ef05&bd=AMjc0X2N0X2No&lid=624d7816232d4a06d607281… 3/3

You might also like