This is my program to read in values from a sparse matrix and store them in a linked list. Something is seriously wrong with something very basic I think!
Ok, please someone help me. Something is wrong with my addnode function I THINK, because as you can see there is a test to see if anything is in position 0,0.
I would really appreciate any help at all, anything!
I hope this code formatting thing works!
xx
Code:
#include <stdio.h>
int size, n, temprow, tempcol, g, size1, rowcount, colcount, counter;
float tempval, q, red;
struct Node {
int row;
int column;
float value;
Node *next;
};
void readmatrix(void);
void insertmatrix(Node *listpointer, int w, int x, float y);
float findvalue(Node *listpointer, int r, int co);
Node *A, *B, *C;
int main() {
A = 0;
B = 0;
C = 0;
g = 0;
printf("Please enter the no. of rows in the matrix\n");
scanf("%i", &size);
readmatrix();
//this prints the first matrix
rowcount = 0;
colcount = 0;
while(counter < size){
while(rowcount < size){
q = findvalue(A, rowcount, colcount);
printf("%f ", q);
rowcount++;
}
printf("\n");
colcount++;
rowcount = 0;
counter++;
}
red = findvalue(A, 0, 0);
printf("%f", red);
}
void readmatrix(void) {
n = 0;
size1 = size * size;
while(n < size1){
printf("Enter the row, column and value seperated by a single space.\n");
scanf("%i %i %f", &temprow, &tempcol, &tempval);
if(tempval != '0'){
insertmatrix(A, temprow, tempcol, tempval);
}
n++;
}
}
void insertmatrix(Node *listpointer, int w, int x, float y) {
Node *temp;
temp = new Node;
temp -> row = w;
temp -> column = x;
temp -> value = y;
temp -> next = listpointer;
listpointer = temp;
}
float findvalue(Node *listpointer, int r, int co) {
Node *current;
current = listpointer;
while(current != NULL){
if((current->row == r)&&(current->column == co)){
return current->value;
}
current = current -> next;
}
return 0;
}