I'm trying to multiply two matrix in c++ but result not correct!
Could you please let me know what's wrong with ,my code?!
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
//more detail
using namespace std;
#define WIDTH1 3
#define HEIGHT1 3
#define WIDTH2 3
#define HEIGHT2 3
#define WIDTH3 3
#define HEIGHT3 3
int A[HEIGHT1][WIDTH1];
int B[HEIGHT2][WIDTH2];
int C[HEIGHT3][WIDTH3];
int n, m;
int y = 0;
//more detail :)
int main() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
cin >> m;
A[i][j] = m;
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
cin >> n;
B[i][j] = n;
}
//more detail :)
for (int i = 0; i < 3; i++)
{
int k = 0;
int sum = 0;
int sum2 = 0;
int sum3 = 0;
for (int j = 0; j < 3; j++)
{
int sum = A[i][j] * B[j][i];
sum += sum;
}
B[i][y] = sum;
y = y + 1;
for (int j = 0; j < 3; j++)
{
int sum2 = A[i][j] * B[j][i];
sum2 += sum2;
}
B[i][y] = sum2;
y = y + 1;
//more detail :)
for (int j = 0; j < 3; j++)
{
int sum3 = A[i][j] * B[j][i];
sum3 += sum3;
}
B[i][y] = sum3;
y = y + 1;
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 1; j++)
{
cout << B[i][j] <<"\t" << B[i][j+1] << "\t" << B[i][j+2] << "\n" ;
}
return 0;
//more detail :)
}
What do you think this does:
for (int j = 0; j < 3; j++)
{
int sum = A[i][j] * B[j][i];
sum += sum;
}
in relation with your original sum
? It doesn't do anything.
Don't redeclare the same variable.
for (int j = 0; j < 3; j++)
{
sum += A[i][j] * B[j][i];
}