Thread: adding sparse matrices

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    adding sparse matrices

    me again

    this time i want to add 2 sparse matrices. I define the matrix to have 3 rows and arbitrarily many columns. each row is a linked list and the rows are arranged in an array, so this is sort of a hash table.

    the insert method adds elements in each row but does it in a way that all of the elements are sorted. i sort the elements by their column values.

    i am having problems adding though. and not always which is puzzling. the program breaks if I have elements in the same position. but also not always. if A has elements in positions (2,2) and B in (2,2) the Add() works great. but if there is more than one element in a row, it breaks,

    really frustrating.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct node
    	{
    		int column;
    		int value;
    		int row;
    		struct node *next;
    	} element;
    
    /* MENU */
    void Menu()
    {
    	printf("\n***\tADDING SPARSE MATRICES\t***\n");
    	printf("\n 1.) Insert in A");
    	printf("\n 2.) Insert in B");
    	printf("\n 3.) Printout both");
    	printf("\n 4.) A + B = ");
    	printf("\n 0.) EXIT");
    	printf("\nChoose ---------> ");	
    }
    
    /*	Initialize*/
    void Init (element *x[])
    {
    	int i;
    	for (i=0; i<3; i++) x[i] = NULL;
    }
    
    /*	Printout  */
    void Printout (element *x[])
    {
    	int i, test = 0;
    	element *temp;
    	
    	for (i=0; i<3; i++) 
    	{
    		temp = x[i];
    		while (temp != NULL)
    		{
    			printf("Element position (%d,%d) = %d\n", i, temp->column, temp->value);
    			test = 1;
    			temp = temp->next;
    		}
    	}
    	
    	if (test == 0) printf("This matrix is empty!!\n");
    	
    }
    
    
    /*	INSERT	*/
    void Insert(element *x[], int row, int column, int value)
    {
    	int r = row;
    	element *p;
    	
    	element *new = malloc(sizeof(element));
    	new->row = row;
    	new->column = column;
    	new->value = value;
    	
    	if (x[r] == NULL) 
    	{
    		x[r] = new;
    		new->next = NULL;
    	}
    	else
    	{
    		p = x[r];
    		if (new->column < p->column)
    		{
    			new->next = p;
    			x[r] = new;
    		}
    		else if (new->column > p->column)
    		{
    			while (p->next != NULL && p->next->column < new->column)
    			{
    				p = p->next;
    			}
    			new->next = p->next;
    			p->next = new;
    		}
    		else printf("An element already exists there!!\n");
    	}	
    }
    
    
    /*	A  +  B  */
    void Add (element *x[],element *y[],element *z[])
    {
    	int i;
    	element *p, *q;
    	
    	for (i=0; i<3; i++)
    	{
    		/*row of x empty, row of y not*/
    		if (x[i] == NULL && y[i] != NULL)
    		{
    			q = y[i];
    			while (q != NULL)
    			{
    				Insert(z, i, q->column, q->value);
    				q = q->next;
    			}
    		}
    		/*row of y empty, row of x not*/
    		if (x[i] != NULL && y[i] == NULL)
    		{
    			q = x[i];
    			while (q != NULL)
    			{
    				Insert(z, i, q->column, q->value);
    				q = q->next;
    			}
    		}
    		/*row of x and y not empty*/		
    		if (x[i] != NULL && y[i] != NULL)
    		{
    			p = x[i];
    			q = y[i];
    			
    			/*stay in while loop until one row runs out*/
    			while (p != NULL || q != NULL)
    			{
    				/*columns match*/
    				if (p->column == q->column && p != NULL && q != NULL)
    				{
    					Insert(z, i, p->column , p->value + q->value);
    					p = p->next;
    					q = q->next;
    				}
    				/*column of x is smaller than column of y*/
    				else if (p->column < q->column)
    				{
    					Insert(z, i, p->column , p->value);
    					p = p->next;
    				}
    				/*column of x is bigger than column of y*/
    				else
    				{
    					Insert(z, i, q->column , q->value);
    					q = q->next;
    				}
    			}
    			
    			/*if row of x had more elements and didn't run out*/
    			if (p != NULL)
    			{
    				while (p != NULL)
    				{
    					Insert(z, i, p->column , p->value);
    					p = p->next;
    				}
    			}
    			/*if row of x had more elements and didn't run out*/
    			if (q != NULL)
    			{
    				while (q != NULL)
    				{
    					Insert(z, i, q->column , q->value);
    					q = q->next;
    				}
    			}
    		}
    	}
    	
    	Printout(z);
    }
    
    /*  FREE MEMORY*/
    void Freedom (element *x[])
    {
    	int i;
    	element *p;
    	for (i=0; i<3; i++)
    	{
    		if (x[i] != NULL)
    		{
    		 p = x[i];
    		 x[i] = x[i]->next;
    		 free(p);
    		}
    	}
    }
    
    /*  MAIN  */
    int main (int argc, const char * argv[]) 
    {
    	int choice, column, row, value, number;
    	element *a[3], *b[3], *sum[3];
    	Init(a);	Init(b);	Init(sum);		
    	do
    	{
    		Menu();
    		scanf("%d",&choice);
    		switch (choice)
    		{
    			case 1:		/*Insert in A */
    				do 
    				{
    					printf("Enter row -> ");
    					scanf("%d",&row);
    				} while (row < 0 || row > 3);	
    			
    				do 
    				{
    					printf("Enter column -> ");
    					scanf("%d",&column);
    				} while (column < 0);	
    				
    				printf("Enter value -> ");
    				scanf("%d",&value);
    				
    				Insert(a,row,column,value);
    				
    				break;
    			case 2:		/*Insert in B */
    				do 
    				{
    					printf("Enter row -> ");
    					scanf("%d",&row);
    				} while (row < 0 || row > 2);	
    				
    				do 
    				{
    					printf("Enter column -> ");
    					scanf("%d",&column);
    				} while (column < 0);	
    				
    				printf("Enter value -> ");
    				scanf("%d",&value);
    				
    				Insert(b,row,column,value);
    				
    				break;
    			case 3:		/* Printout A & B */
    				printf("\n::::::: MATRIX A :> \n\n");
    				Printout(a);
    				printf("\n::::::: MATRIX B :> \n\n");				
    				Printout(b);
    				break;
    			case 4:		/* A + B */
    				Init(sum);
    				printf("\n::::::: MATRIX A + B :> \n\n");
    				Add(a,b,sum);
    				break;
    			default:
    				printf("\nWRONG CHOICE");
    		}
    	} while (choice != 0);
    	
    	
    
    	 Freedom(a);
    	 Freedom(b);
    	 Freedom(sum);
    	return 0;
    }
    Last edited by budala; 08-21-2009 at 09:52 AM.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by budala View Post
    Code:
    int main (int argc, const char * argv[]) 
    {
    	int choice, column, row, value, number;
    	element *a[3], *b[3], *Add[3];
    	Init(a);	Init(b);	Init(sum);		
    	do
    	{
    
    
    }
    I havent gone through the whole program. But what is the statement

    Init(sum); imply

    I cant see any variable sum there

    are you instead trying to do

    Init(Add);

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    you're right. misprint. corrected it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  4. Adding Arrays and Matrices
    By dlf in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2005, 02:36 PM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM