Skip to content

Instantly share code, notes, and snippets.

@Eldres
Last active September 19, 2015 23:21
Show Gist options
  • Save Eldres/13641df616c959f6a87d to your computer and use it in GitHub Desktop.
Save Eldres/13641df616c959f6a87d to your computer and use it in GitHub Desktop.
Multiply two user inputted vectors
Matrix multiplication: Write a C++ program to compute the product of two matrices.
You are required to use the template class vector to represent a matrix.
Specifically, your program will include the main( ) function and a second function multiply_matrices( ).
The main() function will:
* allow the user to choose the size of the matrices, and subsequently the content of A and B
* call the multiply_matrices() function to compute the product of A and B
* print out the multiplication result.
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
void showMatrix(vector <vector<int> > matrix)
{
for (int i = 0; i < matrix.size(); i++) // i is the row in the matrix
{
for (int j = 0; j < matrix[i].size(); j++) // j is the col in the matrix
{
cout << setw(3) << matrix[i][j];
}
cout << endl;
}
cout << endl;
}
vector <vector<int> > vectorMultiply(vector <vector<int> > &matrix1, vector<vector<int> > &matrix2)
{
vector<vector<int> > result;
int sum = 0;
for (int i = 0; i < matrix1.size(); i++)
{
vector<int> temp;
for (int j = 0; j < matrix1[i].size(); j++)
{
for (int k = 0; k < matrix1[i*j].size(); k++) //error
{
sum = sum + matrix1[i][k] * matrix2[k][j];
temp.push_back(sum);
}
sum = 0;
result.push_back(temp);
}
}
return result;
}
int main()
{
int inputSizeFMRow = 0; //input for size of first matrix row
int inputSizeFMCol = 0; //input for size of first matrix col
int inputSizeSMRow = 0; //input for size of second matrix row
int inputSizeSMCol = 0; //input for size of second matrix col
int userInput = 0; //elements provided by the user
vector <vector<int> > firstMatrix(inputSizeFMRow);
vector <vector<int> > secondMatrix(inputSizeSMRow);
cout << "Please enter the row size of the first matrix: ";
cin >> inputSizeFMRow;
cout << "Please enter the column size of the first matrix: ";
cin >> inputSizeFMCol;
cout << endl;
for (int i = 0; i < inputSizeFMRow; i++)
{
vector<int> temp;
for (int j = 0; j < inputSizeFMCol; j++)
{
cout << "Please enter values into the first matrix: ";
cin >> userInput;
temp.push_back(userInput);
}
firstMatrix.push_back(temp);
}
cout << "\nPlease enter the row size of the second matrix: ";
cin >> inputSizeSMRow;
cout << "Please enter the column size of the second matrix: ";
cin >> inputSizeSMCol;
cout << endl;
if (inputSizeFMCol != inputSizeSMRow)
{
cout << "These matrices cannot be multiplied together.\n";
}
else
{
for (int i = 0; i < inputSizeSMRow; i++)
{
vector<int> temp;
for (int j = 0; j < inputSizeSMCol; j++)
{
cout << "Now please enter the values for the second matrix: ";
cin >> userInput;
temp.push_back(userInput);
}
secondMatrix.push_back(temp);
}
vector <vector<int> > multiMatrix = vectorMultiply(firstMatrix, secondMatrix);
showMatrix(multiMatrix);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment