# Matrix program using multi threading calculation

Hi, I was asked to write a program to calculate the matrix multiplication using the following pseudo codes
// global data declaration

int dim;

void *multiply(struct matrix *in)
{
// local data declaration

// multiplication process
...
...

}

int main(int argc, char *argv[])
{

// local data e.g. thread id
int i, j, no_elements;

if (argc != 2) {
// output error ... need to specify one argument !
return(1);
}

dim=atoi(argv[1]);

no_elements = 0;
for (i=0; i<dim; i++)
for (j=0; j<dim; j++){
// initialise the data
...
no_elements++;
}

for (m=0; m<no_elements; m++)

for (m=0; m<no_elements; m++)

// printing of result

}
when run:
\$./ matrix    3
"3" is the size of the matrix

The result output as below
180  84  168            118  102  135            60720  50448  67308
171  195  42     *        78  110  204   =       43620  44604  69333
71  164  59             196  136  154            32734  33306  52127

From the structure of the code above, what sort of variables should i store in the stuct matrix??   And maybe the algorithm on writing the multiply function() using struct as parameter. Thanks
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
>> what sort of variables should i store in the stuct matrix??

At the very least, you'll need the size (rows and cols) of the matrix, and the actual data elements.

>> And maybe the algorithm on writing the multiply function() using struct as parameter. Thanks

Here's the algorithm for matrix multiplication easily explained :

http://en.wikipedia.org/wiki/Matrix_multiplication
0
Author Commented:
i understand the algorithm for the matrix, but is there any tutorial/sample code to see how to do the matrix multiplication?
0
Commented:
The code will be exactly the same as the algorithm ... Just multiply-add each row of the first matrix with each column of the second matrix ... Use a nested loop for that ...
0
Author Commented:
i actually weak in pointers
I gt error  "request for member 'row' in something not a struct or union"
"request for member 'row' in something not a struct or union"
in line   "total += in.row[l] * in.col[l];"

/* this is only in the form of pseudo codes but NOT C/C++ program
you need to fill in the necessary detail
*/

int dim;
//global data declaration
struct matrix
{

int row[9];
int col[9];
};

void *multiply(struct matrix *in)
{
int total = 0;
int l;
// local data declaration
for (l = 0; l < dim; l++)
{

err            total += in.row[l] * in.col[l];

}

}

int main(int argc, char *argv[])
{

// local data e.g. thread id
int i, j,k,x,m, no_elements;
if (argc != 2) {
// output error ... need to specify one argument !
return(1);
}

dim=atoi(argv[1]);
struct matrix a[dim*dim];

no_elements = 0;
k=0;
for (i=0; i<dim; i++) {
for (j=0; j<dim; j++){
k++;
no_elements++;
if (k % dim == 0)
k=0;
for (x=0; x<dim; x++){
a[(dim*i)+j].row[x] = (dim*i)+x+1;
a[(dim*i)+j].col[x] = k+(dim*x);

}

}

}

for (m=0; m<no_elements; m++)

for (m=0; m<no_elements; m++)

// printing of result

}

i think it happens while using the struct variables in the function..
0
Commented:
struct matrix
{

int row[9];
int col[9];
};

This does not represent a matrix ... It's just two arrays of 9 ints.

>> I gt error  "request for member 'row' in something not a struct or union"

That's because in is a pointer to struct matrix, not a struct matrix. For accessing fields you have to use -> instead of .

But, as I said : you haven't really defined a matrix ... Try either this :

int matrix[9][9];

or :

int **matrix;  /* <--- allocate memory !!! */

>> i actually weak in pointers

Then I suggest reading these tutorials :

arrays : http://www.cplusplus.com/doc/tutorial/arrays.html
pointers : http://www.cplusplus.com/doc/tutorial/pointers.html
dynamic memory : http://www.cplusplus.com/doc/tutorial/dynamic.html
0

Experts Exchange Solution brought to you by