# judenaveenraj/cs215 forked from siddhant3s/cs215

Fetching contributors…
Cannot retrieve contributors at this time
executable file 181 lines (174 sloc) 5.17 KB
 /* 4. Write a C program for i. Adding two sparse matrices and transposing a sparse matrix where a sparse matrix is implemented by an array of records. */ #include struct SparseNode { int data; size_t x; size_t y; }; /* A sparse matrix is represented as an array with all the Non-zero listed. Each element of the array is basically a SparseNode which contains the actual data and the position of the element as x and y. |---------------------------------+-----+-----+-----+------+-----+-----+-----| | Position in the Sparse Array => | 0 | 1 | 2 | 3 | 4 | 5 | 6 | |---------------------------------+-----+-----+-----+------+-----+-----+-----| | The actual data in the Matrix | 845 | 475 | 486 | 1475 | 518 | 654 | 843 | |---------------------------------+-----+-----+-----+------+-----+-----+-----| | X Position of the Data | 0 | 2 | 4 | 3 | 2 | 1 | 3 | |---------------------------------+-----+-----+-----+------+-----+-----+-----| | Y Position of the Data | 2 | 5 | 4 | 2 | 6 | 2 | 7 | |---------------------------------+-----+-----+-----+------+-----+-----+-----| Above is the Array representation of a sparse Matrix containing 7 non-zero elements */ struct SparseNode* sparse_node_at_xy(struct SparseNode A[], size_t size, size_t x, size_t y) { size_t i; for (i=0; i < size; i++) if( A[i].x==x && A[i].y==y) return A+i; return NULL; } size_t sparse_add(struct SparseNode A[], size_t sizeA, struct SparseNode B[], size_t sizeB, struct SparseNode result[]) { /* result[] is assumed to have a length at least sizeA+sizeB */ size_t i; for (i=0; i< sizeA; i++) result[i]=A[i]; size_t j, sizeC=sizeA; for (j=0; j < sizeB; j++) { struct SparseNode* currentnode= sparse_node_at_xy(result, sizeA, B[j].x, B[j].y); if (currentnode) currentnode->data += B[j].data; else { result[i+j]=B[j]; sizeC++; } } return sizeC; } void sparse_transpose(struct SparseNode A[], size_t size) { while(size--) { size_t t= A[size].x; A[size].x=A[size].y; A[size].y=t; } } size_t sparse_input(struct SparseNode A[], size_t max) { size_t size; printf("Enter the number of non-zero elements (Maximum non zero elements must not exceed %i)",max); scanf("%u",&size); // printf("You entered a size %u",size); if(size>max) { fprintf(stderr,"ERROR. The number of non-zero element exceeded %u. Can't input.",max); return 0; } size_t i; for(i=0; i < size; i++) { size_t x,y; int data; printf("Enter the Data:"); scanf(" %i", &data); // printf("You entered %i\n",data); printf("Enter its position in Matrix as x and y (zero-based) separated by space"); scanf("%u%u",&x,&y); struct SparseNode node={data,x,y}; A[i]=node; } return i; } void sparse_output(struct SparseNode A[],size_t size, size_t x, size_t y) { size_t i; for(i=0; idata : 0); } printf("\n"); } } void sparse_dump(struct SparseNode A[], size_t size) { size_t i; for(i=0; i