+ 1
Need help with a matrix task for my school project
Hello guys, I am currently working on a project for my school. Our task is to make a matrix 4x4 multiplied with a vector 4x1. I am sure my code is right but the result keeps beeing false. https://code.sololearn.com/cuhR7NA0426e/#cpp help is appreciated
10 Answers
+ 2
I think if you have to multiply a matrix with a vector your result will be another matrix. Each line of the new matrix is = each line of old matrix * vector.
But in your code you use 2 vector and a matrix and in the function mul you attempt to save the result in the second vector
+ 2
Yes...give me some time please
+ 1
something like this, without scanf 4 semplicity
#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef double vec[4];
typedef double mat[4] [4];
void mul(mat m_res, mat m, vec v)
{
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
m_res[i][j]= m[i][j] * v[i];
}
int main()
{
vec v;
mat m, m_res;
int i, j;
for (i = 0; i<4; i++)
v[i] = i;
for (i = 0; i < 4; i++)
for( j = 0; j < 4; j++)
m[i][j] = 10*i + j;
mul(m_res, m, v);
for (i = 0; i < 4; i++)
{
cout << endl;
for( j = 0; j < 4; j++)
cout << m_res[i][j] << " ";
}
return 0;
}
+ 1
maybe you have to adjust the formula but now it work
+ 1
yes, the m_res matrix
+ 1
v is 0 1 2 3
m is 0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33
so if u multiply each line with v
first line 0*0 0*1 0*2 0*3
second line 1*10 1*11 1*12 1*13
third line 2*20 2* 21 2*22 2*23
last line 3*30 3*31 3*32 3*33
+ 1
try also
m_res[i][j] = m[i][j] * v[j]
for a different moltiplication
+ 1
maybe the second is what you need
0
do you have any idea how to change my code to do that?
0
the output is just
0 0 0 0
10 11 12 13
40 42 44 46
90 93 96 99