Discuss C - Initializing a Sparse Matrix in the Programming Help forum on ASP Free. C - Initializing a Sparse Matrix Programming Help forum discussing programming solutions and code assistance related issues. Feel free to ask questions, or to offer your assistance.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 7
Time spent in forums: 3 h 16 m 3 sec
Reputation Power: 0
C - Initializing a Sparse Matrix and Adding Nodes
Hi all, so I'm trying to create a sparse matrix...ie a matrix where only space is allocated for cells (nodes) with values. There are two parts to this program. First part of the assignment is to build a structure to store a sparse matrix A. I must take the name of the file as a command line argument. Entries of the sparse matrix are given in the file in the following format. First line of the file is an integer that is the dimension of the matrix. You may assume that the matrix is square. That is, the row and column dimensions are the same. Each of the subsequent lines contain two integers, row-index, column-index and value of the matirx entry(double). An example of a text file will look like this:
Posts: 7
Time spent in forums: 3 h 16 m 3 sec
Reputation Power: 0
I have completely rewritten my insert function, which has removed all bus errors. I now however cannot tell if either my print function is wrong, or the insert function isnt completely right as printing prints 15.000000 twice and that's it....
Code:
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void initializeMatrix(Matrix** M, long* dimension, FILE* in) {
(*M)->rowList = (Node**)malloc(*dimension*sizeof(Node*));
(*M)->columnList = (Node**)malloc(*dimension*sizeof(Node*));
int i;
for (i=0; i<*dimension; i++) {
(*M)->rowList[i] = NULL;
(*M)->columnList[i] = NULL;
}
}
void insertNode(Matrix** M, long row_index, long column_index, double value) {
Node* ptr = (Node*)malloc(sizeof(Node));
ptr->value = value; /* initialize all properties of node */
ptr->rowIndex = row_index;
ptr->columnIndex = column_index;
if ((*M)->rowList[row_index] == NULL) { /* index is empty */
ptr->rowPtr = NULL;
(*M)->rowList[row_index] = ptr;
}
if ((*M)->columnList[column_index] == NULL) {
ptr->colPtr = NULL;
(*M)->columnList[column_index] = ptr;
}
if ((*M)->rowList[row_index]->columnIndex > column_index) { /* insert node to front */
ptr->colPtr = (*M)->rowList[row_index];
(*M)->rowList[row_index] = ptr;
}
if ((*M)->columnList[column_index]->rowIndex > row_index) {
ptr->rowPtr = (*M)->rowList[row_index];
(*M)->columnList[column_index] = ptr;
}
if ((*M)->rowList[row_index]->columnIndex < column_index) { /* insert node in order */
Node* rowptr = (Node*)malloc(sizeof(Node));
rowptr = (*M)->rowList[row_index];
while (rowptr->colPtr != NULL && rowptr->columnIndex < column_index) {
rowptr = rowptr->colPtr;
}
if (rowptr->colPtr == NULL) {
rowptr->colPtr = ptr;
free(rowptr);
}
else { /* node already exists */
printf("Node already exists!");
free(ptr);
free(rowptr);
}
}
if ((*M)->columnList[column_index]->rowIndex < row_index) {
Node * colptr = (Node*)malloc(sizeof(Node));
colptr = (*M)->columnList[column_index];
while (colptr->rowPtr != NULL && colptr->rowIndex < row_index) {
colptr = colptr->rowPtr;
}
if (colptr->rowPtr == NULL) {
colptr->rowPtr = ptr;
free(colptr);
}
else { /* Node already exists! */
printf("Node already exists!");
free(ptr);
free(colptr);
}
}
}
void printSubMatrix(Matrix* M, long beginrow, long endrow, long begincol, long endcol) {
long i = beginrow;
Node* ptr = (Node*)malloc(sizeof(Node));
ptr->columnIndex = begincol;
for (i; i<endrow; i++) {
printf("\n");
if (M->rowList[i] != NULL) {
ptr = M->rowList[i];
while (ptr->columnIndex <= endcol) {
printf("%lf ", ptr->value);
ptr = ptr->colPtr;
}
}
}
free(ptr);
}
Posts: 7
Time spent in forums: 3 h 16 m 3 sec
Reputation Power: 0
The code runs, etc. Therefore gdb isn't helpful...I came to the forums having done all the debugging I can and being stuck. None of the values have 15, so the printing makes no sense. I inserted print statements in add and it is adding correctly I believe. Therefore I feel as if I am not navigating through the matrix properly in print, but I don't see how that could be
Posts: 31,109
Time spent in forums: 3 Months 3 Weeks 2 Days 20 h 18 m 8 sec
Reputation Power: 2919
well, the print looks fine. the insert got some suspicious parts I would check
very good if it's really working by careful debug and adding lots of printf commands
inside the insert method.