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;

{
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
{
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");
break;
default:
printf("\nWRONG CHOICE");
}
} while (choice != 0);

Freedom(a);
Freedom(b);
Freedom(sum);
return 0;
}```

2. Originally Posted by budala
Code:
```int main (int argc, const char * argv[])
{
int choice, column, row, value, number;
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