Question

6.18.1 Consider the sparse matrix X below and write C code that would store this code...

6.18.1

Consider the sparse matrix X below and write C code that would store this code in Yale Sparse Matrix Format.

Row 1 [1, 2, 0, 0, 0, 0]

Row 2 [0, 0, 1, 1, 0, 0]

Row 3 [0, 0, 0, 0, 9, 0]

Row 4 [2, 0, 0, 0, 0, 2]

Row 5 [0, 0, 3, 3, 0, 7]

Row 6 [1, 3, 0, 0, 0, 1]

6.18.2

In terms of storage space, assuming that each element in matrix X is single-precision floating point, compute the amount of storage used to store the matrix above in Yale Sparse Matrix Format.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Include necessary header files
#include <stdio.h>

int main()
{
//Define the sparse matrix X
int X[6][6] = {1, 2, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0,
0, 0, 0, 0, 9, 0,
2, 0, 0, 0, 0, 2,
0, 0, 3, 3, 0, 7,
1, 3, 0, 0, 0, 1};
int A[6*6];
int IA[6];
int JA[6*6];

//Display the matrix in sparse atrix format
printf("Sparse Matrix X");
for(int i=0; i<6; i++)
{
printf(" ");
for(int j=0; j<6; j++)
printf("%d ", X[i][j]);
}
  
//Read all non zero elements of matrix X and store in array A
int k = 0, l = 0;
for(int i=0; i<6; i++)
{
//IA array represents the matrix X into row splits
IA[l++] = k;
for(int j=0; j<6; j++)
if(X[i][j] != 0)
{
  
//Store the element in A and then increment the index k
A[k] = X[i][j];
  
//Store the column index in array JA
JA[k++] = j;
}
}
  
  
  
printf(" Yale Sparse Matrix Format A = ");
for(int i=0; i<k; i++)
printf("%d ", A[i]);

printf(" IA = ");
for(int i=0; i<l; i++)
printf("%d ", IA[i]);
  
printf(" JA = ");
for(int i=0; i<k; i++)
printf("%d ", JA[i]);
return 0;
}

Screenshot of Code Screen:

Screenshot of Output:

Add a comment
Know the answer?
Add Answer to:
6.18.1 Consider the sparse matrix X below and write C code that would store this code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT