Using C programming A sparse matrix is a two-dimensional array in which a large
ID: 3920407 • Letter: U
Question
Using C programming
A sparse matrix is a two-dimensional array in which a large number of the elements are zero. A concise text file representation of a sparse matrix needs to store only the array dimensions on the first line and the number of nonzero elements on the second. Each of the remaining lines should contain three numbers—row subscript, column subscript, and value of one nonzero entry. Write a program that converts a text file containing a traditional matrix representation to a text file containing a compressed sparse matrix. The program should open the file containing the traditional representation (a line with dimensions followed by matrix contents a row at a time), call a function scan_matrix to input the matrix, open the output file, and call a function write_sparse to store the compressed representation. Write a second program that reverses the process—doing input of a sparse matrix file and creating a file containing the traditional representation. For the second program, write functions scan_sparse and write_matrix.
(Note: the matrix txt file to be referenced has about 2500 elements, roughly split evenly between 1 and 0)
Explanation / Answer
Given below is the code for the question. You can use your input file provided by instructor
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Program 1: write_compressed.c
--------------
//program to read a standard matrix representation and generate a compressed sparse matrix representation file
#include <stdio.h>
#include <stdlib.h>
void scan_matrix(FILE *fp, int mat[][100], int rows, int cols);
void write_sparse(FILE *fp, int mat[][100], int rows, int cols);
int main(){
char inFilename[20], outFilename[20] = "compressed.txt";
FILE *in, *out;
int rows, cols;
int mat[100][100];
printf("Enter input file name for standard matrix representation: ");
scanf("%s", inFilename);
in = fopen(inFilename, "r");
if(in == NULL){
printf("ERROR: could not open input file %s ", inFilename);
exit(1);
}
out = fopen(inFilename, "w");
if(out == NULL){
printf("ERROR: could not open output file %s ", outFilename);
exit(1);
}
fscanf(in, "%d %d", &rows, &cols);
scan_matrix(in, mat, rows, cols);
printf("Writing to file %s ", outFilename);
write_sparse(out, mat, rows, cols);
}
void scan_matrix(FILE *fp, int mat[][100], int rows, int cols){
int i, j;
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
fscanf(fp, "%d", &mat[i][j]);
fclose(fp);
}
void write_sparse(FILE *fp, int mat[][100], int rows, int cols){
int count = 0;
int i, j;
//first count how many are non zero elements
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
{
if(mat[i][j] != 0)
count++;
}
//now write to file
fprintf(fp, "%d %d ", rows, cols);
fprintf(fp, "%d ", count);
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
if(mat[i][j] != 0)
fprintf(fp, "%d %d %d ", i, j, mat[i][j]);
}
}
fclose(fp);
printf("Compressed sparse matrix representation generated ");
}
---------------
Program 2: read_compressed.c
-------------
//program to read a compressed sparse matrix representation and generate a traditional matrix representation file
#include <stdio.h>
#include <stdlib.h>
void scan_sparse(FILE *fp, int mat[][100], int rows, int cols);
void write_matrix(FILE *fp, int mat[][100], int rows, int cols);
int main(){
char inFilename[20], outFilename[20] = "matrix-out.txt";
FILE *in, *out;
int rows, cols;
int mat[100][100];
printf("Enter input file name containing compressed sparse matrix: ");
scanf("%s", inFilename);
in = fopen(inFilename, "r");
if(in == NULL){
printf("ERROR: could not open input file %s ", inFilename);
exit(1);
}
out = fopen(inFilename, "w");
if(out == NULL){
printf("ERROR: could not open output file %s ", outFilename);
exit(1);
}
fscanf(in, "%d %d", &rows, &cols);
scan_sparse(in, mat, rows, cols);
printf("Writing to file %s ", outFilename);
write_matrix(out, mat, rows, cols);
}
void scan_sparse(FILE *fp, int mat[][100], int rows, int cols){
int i, r, c, count;
fscanf(fp, "%d", &count);
for(i = 0; i < count; i++)
{
fscanf(fp, "%d %d %d", &r, &c, &mat[r][c]);
}
fclose(fp);
}
void write_matrix(FILE *fp, int mat[][100], int rows, int cols){
int i, j;
fprintf(fp, "%d %d ", rows, cols);
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++)
{
fprintf(fp, "%4d", mat[i][j]);
}
fprintf(fp, " ");
}
fclose(fp);
printf("Traditional matrix representation generated ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.