30
Apr

Here is my code.

#include

#include

#define MAX_ROWS 10

#define MAX_COLs 10

int main(void)

{

int i;

int j;

int addvalue [10][10];

printf(”\n***Add_table built***\n”);

for ( i = 0; i < 10; i )

for ( j = 0; j < 10; j )

if (i == j)

addvalue [i] [j] = i j;

else if (i > j)

addvalue [i][j] = i j;

else

addvalue [i][j] = i j;

for ( i = 0; i <10; i )

{

for ( j = 0; j <10; j )

printf(”=”, addvalue [i][j]);

printf(”\n”);

}

{

int i;

int j;

int mult_value [10][10];

printf(”\n***Mult-table built***\n”);

for ( i = 0; i < 10; i )

for ( j = 0; j < 10; j )

if (i == j)

mult_value [i] [j] = i*j;

else if (i > j)

mult_value [i][j] = i*j;

else

mult_value [i][j] = i*j;

for ( i = 0; i <10; i )

{

for ( j = 0; j <10; j )

printf(”=”, mult_value [i][j]);

printf(”\n”);

}

return 0;


Answer:
You should walk through your code and comprehend what it's doing, because it's not adding two matrices, and the second part is not close to mutiplying two matrices.

I just wrote the code below to show you a way to do it. Adding two matrices is simple, as you can see in the addIntMatrix function. You can write the multiply function yourself. It's quite a bit more complicated than matrix addition, and you should have some fun working it out.

Rather than just showing you the matrix addition code, I'm giving you the rest of it in the hope that you'll study it, and learn something about techniques you can use to write nice code in C.

#include

#include

#define ROWS 2

#define COLS 2

typedef struct {

unsigned _rows;

unsigned _cols;

int **_m;

} IntMatrix;

IntMatrix *newIntMatrix(const unsigned, const unsigned);

void delIntMatrix(IntMatrix *);

IntMatrix* addIntMatrix(const IntMatrix *m1,

const IntMatrix *m2);

void printIntMatrix(const char*, const IntMatrix*);

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

IntMatrix *A = newIntMatrix(ROWS,COLS);

IntMatrix *B = newIntMatrix(ROWS,COLS);

IntMatrix *C;

unsigned i,j, k = 0;

/* initialize for matrix add test */

for (i = 0; i < A->_rows; i ) {

for (j = 0; j < A->_cols; j ) {

A->_m[i][j] = k;

B->_m[i][j] = k (A->_rows * A->_cols);

}

}

printIntMatrix(”A:”,A);

printIntMatrix(”B:”,B);

/* compute C = A B, and print result */

C = addIntMatrix(A,B);

printIntMatrix(”A B:”,C);

delIntMatrix(A);

delIntMatrix(B);

delIntMatrix(C);

return 0;

}

IntMatrix *newIntMatrix(const unsigned rows, const unsigned cols) {

IntMatrix *m = (IntMatrix*)NULL;

unsigned i;

if ((rows > 0) && (cols > 0)) {

m = (IntMatrix*)calloc(sizeof(IntMatrix),1);

m->_rows = rows;

m->_cols = cols;

m->_m = (int**)calloc(m->_rows,sizeof(int*));

for (i = 0; i < m->_rows; i ) {

m->_m[i] = calloc(m->_cols,sizeof(int));

}

}

return m;

}

void delIntMatrix(IntMatrix *m) {

unsigned i;

if ((m != (IntMatrix*)NULL) && (m->_m != (int)NULL)) {

for (i = 0; i < m->_rows; i ) {

free(m->_m[i]);

}

free(m->_m);

}

}

IntMatrix *addIntMatrix(const IntMatrix *m1,

const IntMatrix *m2) {

IntMatrix *sum = (IntMatrix*)NULL;

unsigned i,j;

if ((m1 != (IntMatrix*)NULL) &&

(m2 != (IntMatrix*)NULL) &&

(m1->_rows == m2->_rows) &&

(m2->_cols == m2->_cols)) {

sum = newIntMatrix(m1->_rows,m1->_cols);

for (i = 0; i < m1->_rows; i ) {

for (j = 0; j < m1->_cols; j ) {

sum->_m[i][j] = m1->_m[i][j] m2->_m[i][j];

}

}

}

return sum;

}

void printIntMatrix(const char *s, const IntMatrix *m) {

unsigned i,j;

printf(”\n%s\n”,s);

if ((m != (IntMatrix*)NULL) &&

(m->_rows > 0) &&

(m->_cols > 0)) {

for (i = 0; i < m->_rows; i ) {

for (j = 0; j < m->_cols; j ) {

printf(”= “,m->_m[i][j]);

}

puts(”");

}

} else {

puts(”\nInvalid matrix”);

}

}

/*

Program output:

A:

1 2

3 4

B:

5 6

7 8

A B:

6 8

10 12

*/

Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkList

This entry was posted on Wednesday, April 30th, 2008 at 2:29 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or TrackBack URI from your own site.

Leave a reply

Name (*)
Mail (*)
URI
Comment