Thread: Little help? Matrices to linked list? Thanks

  1. #1
    lost blonde girl
    Join Date
    Mar 2005
    Location
    new zealand!!
    Posts
    3

    Little help? Matrices to linked list? Thanks

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To update the value of something outside the function, inside the function, what do you do?

    You pass a pointer to it.

    To update what a the value of a pointer which is outside the function, inside the function, what do you do?

    You pass a pointer to it.

    You need a pointer to a pointer for your argument to your insertion function. Otherwise the change is lost when the function ends, just like any other variable:
    Code:
    void foo( int bar )
    {
        bar = 5; /* This is lost at the end of the function. */
    }
    Code:
    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; /* so is this... */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    lost blonde girl
    Join Date
    Mar 2005
    Location
    new zealand!!
    Posts
    3
    omg, please ignore this post, turn back before you think I am a complete moron!! I forgot the & before the *listpointer so I actually wasnt doing anything, sorry to waste your time
    sincerely

    lost blonde girl :P

  4. #4
    lost blonde girl
    Join Date
    Mar 2005
    Location
    new zealand!!
    Posts
    3
    ohh thanks Quzah I hadnt noticed your reply when I posted. noted
    sorry for waste of time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM