Program (Calculation of Matrix Product)

P

pedromoises48

#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[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[j]);

/* calculo do produto */
for (i = 0; i < m; i++)
for (j = 0; j < p; j++){
C[j] = 0;
for (k = 0; k < n; k++)
C[j] = C[j] + A[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[j]);
printf("\n");
}
}

return 0;
}
 
U

user923005

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.
 
U

user923005

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 = calloc (n, sizeof(float)); /* m vetores de n floats */

if (v == 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);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top