I need to declare a vector which will contain 3 x 3 matrix. And Need to pass that vector to a function and need to perform a multiplication with the same vector. I know, I am not going in a proper way. But I am not able to figure out the problem. I am hoping some direction about 2d Matrix vector Multiplication.
#include <iostream>
#include <vector>
using namespace std;
void display(vector<vector<int>> &vec)
{
for (int i = 0; vec.size(); i )
{
for (int j = 0; j < vec.size(); j )
{
cout << vec[i][j] << " ";
}
cout << endl;
}
}
vector<vector<int>> matMultiply(vector<vector<int>> &vec)
{
for (int i = 0; vec.size(); i )
{
for (int j = 0; j < vec.size(); j )
vec[i][j] = 0;
{
for (int k = 0; k < vec.size(); i )
{
vec[i][j] *= vec[i][k] * vec[k][j];
cout << vec[i][j] << " ";
}
}
cout << endl;
return vec;
}
}
int main()
{
vector<vector<int>> matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
display(matrix);
matMultiply(matrix);
return 0;
}
CodePudding user response:
Because matrix multiplication requires multiple matrix values to compute the value at a single index, you need some way to store the intermediate result. probably in another vector
. You need to create a new std::vector<std::vector<int>>
inside the matMultiply
function. It should look something like this:
std::vector<std::vector<int>> matMultiply(const std::vector<std::vector<int>> &vec){
std::vector<std::vector<int>> result(3, std::vector<int>(3));
for(int i = 0; i < result.size(); i ){
for (int j = 0; j < result.size(); j ) {
for (int k = 0; k < result.size(); k ){
result[i][j] = vec[i][k] * vec[k][j];
}
}
}
return result;
}
Since you are not modifying the parameter(s) of the function, you should add const
to have extra compile time checking in case you accidentally modify it in the function body.
You should remove the cout
statements because they do not belong to the matMultiply
function. The job of display the result belongs to the display
function, which means you should pass the result of matMultiply
as an argument to the display function in your main
function.
Your main
function should now look something like this:
int main()
{
std::vector<std::vector<int>> matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
display(matrix);
// Now you'll pass the result of `matMultiply` which is the
// `result` matrix to the display function to display the result
display(matMultiply(matrix));
return 0;
}
CodePudding user response:
@Taimoor Zaeem. Thank you again. I did the code as you suggested. But the result is coming wrong. Beside the display function is not working, may be I did wrong.. For the viewing the result, I have add std :: cout<< mat[i][j]<<" ";
in the matMultiply
function
The result is coming
1 9 30
2 12 36
3 15 42
4 24 66
8 33 81
12 42 96
7 39 102
14 54 126
21 69 150
The result will be
30 36 42
66 81 96
102 126 150
#include<iostream>
#include<vector>
void display (std::vector<std::vector<int>> mat){
for(int i = 0; mat.size(); i ){
for (int j = 0; j < mat.size(); j ) {
std :: cout<< mat[i][j]<<" ";
}
std :: cout<< std ::endl;
}
}
std::vector<std::vector<int>> matMultiply(const std::vector<std::vector<int>> &vec){
std::vector<std::vector<int>> result(3, std::vector<int>(3));
for(int i = 0; result.size(); i ){
for (int j = 0; j < result.size(); j ) {
for (int k = 0; k < result.size(); k ){
result[i][j] = vec[i][k] * vec[k][j];
std :: cout<< result[i][j] <<" ";
}
std :: cout<< std:: endl;
}
}
return result;
}
int main(){
std :: vector < std :: vector< int > > matrix = {{1,2,3},{4,5,6},{7,8,9}};
//display(matrix);
matMultiply(matrix);
display(matMultiply(matrix));
return 0;
}