Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Program (Calculation of Matrix Product)

108 views
Skip to first unread message

pedrom...@gmail.com

unread,
Jan 5, 2007, 5:40:24 AM1/5/07
to
#include <stdio.h>
#define MAX 100

int main() {
int m, n, /* dimensoes da matriz A */
nb, p, /* dimensoes da matriz B */
i, j, k;
float A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];

printf("Digite as dimensoes (mXn) da matriz A: ");
scanf("%d", &m);
scanf("%d", &n);
printf("Digite a matriz A:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%f", &A[i][j]);

printf("Digite as dimensoes (nXp) da matriz B: ");
scanf("%d", &nb);
scanf("%d", &p);
if (nb != n)
printf("Nao existe o produto da matriz A por B!!");
else {
printf("Digite a matriz B:\n");
for (i = 0; i < n; i++)
for (j = 0; j < p; j++)
scanf("%f", &B[i][j]);

/* calculo do produto */
for (i = 0; i < m; i++)
for (j = 0; j < p; j++){
C[i][j] = 0;
for (k = 0; k < n; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}

/* impressao do resultado */
printf("Matriz A X B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < p; j++)
printf("%f ", C[i][j]);
printf("\n");
}
}

return 0;
}

user923005

unread,
Jan 5, 2007, 2:37:52 PM1/5/07
to
This is C++ but it should be easy enough to translate it to C:
http://cap.connx.com/chess-engines/new-approach/strassen.zip

Originally, I think it was C (some student project from the internet),
but I went and translated it.

Typical reasoning says that Strassen matrix multiplication only pays
off when the matrix is very large. That typical reasoning is wrong.

It was not clear to me from your post what you were wanting in reply.
Perhaps a code review? Comments for improvements? If you give some
additional feedback, I can probably say something that is actually
helpful.

vanders...@gmail.com

unread,
Jan 5, 2007, 3:33:04 PM1/5/07
to
look this example that uses dynamic memory llocation :
http://www.vandersongold.com.br/downloads/mul_matriz.c

user923005

unread,
Jan 5, 2007, 4:00:00 PM1/5/07
to

vanders...@gmail.com wrote:
> look this example that uses dynamic memory llocation :
> http://www.vandersongold.com.br/downloads/mul_matriz.c

/* aloca as linhas da matriz */

v = calloc (m, sizeof(float *)); /* Um vetor de m ponteiros para float
*/
if (v == NULL) {

printf ("** Erro: Memoria Insuficiente **");

return (NULL);

}

/* aloca as colunas da matriz */

for ( i = 0; i < m; i++ ) {

v[i] = calloc (n, sizeof(float)); /* m vetores de n floats */

if (v[i] == NULL) {

printf ("** Erro: Memoria Insuficiente **");

/*
vector of float pointers v[] was never freed.
individual columns v[j] for j = 0 to i are not freed.
*/
return (NULL);

}

}

0 new messages