
Concept explainers
Write in C Language
Description
A sparse matrix is a matrix in which most of the elements are zero. Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, we can compress the sparse matrix by only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value).
0 0 0 0 0 0
0 3 0 0 0 0
0 0 0 6 0 0
0 0 9 0 0 0
0 0 0 0 12 0
Here is a 5X6 matrix, there are 4 element non-zero, we using three number represent it
5 6 4
After the three number are (Row, Column, value):
row col val
1 1 3
2 3 6
3 2 9
4 4 12
Write a
Input
First line consists 3 integers. First and second integers shows the dimensions of original matrix. Third integer k shows number of non-zero elements. Followed k lines are content of the sparse matrix. Each line consists 3 integers, row number, column number, and data.
Output
Output original matrix. Attach a white space behind each element.
Sample Input 1
5 6 4Sample Output 1
0 0 0 0 0 0Expert Answer (doesn't match the sample input. it must match the sample input and output)
The program is written in c
#include <stdio.h>
int main()
{
int a[20][20],n,i,j,k,l;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
a[i][j]=0;
}
}
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(k=0;k<n;k++)
{
scanf("%d",&i);
scanf("%d",&j);
scanf("%d",&a[i][j]);
}
for(k=0;k<=i;k++)
{
for(l=0;l<=j;l++)
{
printf("%d ",a[k][l]);
}
printf("\n");
}
return 0;
}

Step by stepSolved in 3 steps with 1 images

- python # Initialize a 6x6 matrix with all elements as 0 matrix = [[0 for _ in range(6)] for _ in range(6)] # Fill in the matrix for i in range(6): # Iterate over rows for j in range(6): # Iterate over columns if i > j: matrix[i][j] = 3*j - 4*i else: matrix[i][j] = 0 # Print the matrix for row in matrix: print(row)arrow_forwardIn pythion with numpy I have code in a for loop that creates a vecor of 12 elements:x = 1 2 3 4 5 6 7 8 9 10 11 12 The loop iterates 10 times. The second iteration generates a new vector x = 12 11 10 9 8 7 6 5 4 3 2 1 etc. At the end of the loop I need to have a 12 X 10 matrix x x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x x x x x I cannot figure out how to createa matrix where it takes the output and uses the output as rowarrow_forwardPlease written by computer sourcearrow_forward
- Using Java Modify the program 1 to compute the multiplication of two vectors with the sizes mx1 and 1xn. The result is a matrix mxn computed as follows: the element on the position r and c in the product will be equal with m1[r][0]*m2[0][c].arrow_forwardMATLAB. write code for all partsarrow_forwardWhat is Slow Insertion in an Ordered Array?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





