Skip to content

Matrix Multiplication using new method #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions days.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution
{
public:
string longestPrefix(string s)
{
// approach1
int n = s.length();
string prefix = s.substr(0, n - 1);
string suffix = s.substr(1, n - 1);
while (prefix.length())
{
if (prefix == suffix)
{
return prefix;
}
else
{
prefix.pop_back();
suffix = suffix.substr(1, suffix.size() - 1);
}
}
return "";
109 changes: 40 additions & 69 deletions matrix multiplication.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,41 @@
#include <iostream>
#include<iostream>
using namespace std;

int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;

// If column of first matrix in not equal to row of second matrix,
// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}

// Storing elements of first matrix.
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

// Storing elements of second matrix.
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}

// Initializing elements of matrix mult to 0.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}

// Multiplying matrix a and b and storing in array mult.
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}

// Displaying the multiplication of two matrix.
cout << endl << "Output Matrix: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}

return 0;
}
int main() {
int product[10][10], r1=3, c1=3, r2=3, c2=3, i, j, k;
int a[3][3] = { {2, 4, 1} , {2, 3, 9} , {3, 1, 8} };
int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 4, 7} };
if (c1 != r2) {
cout<<"Column of first matrix should be equal to row of second matrix";
} else {
cout<<"The first matrix is:"<<endl;
for(i=0; i<r1; ++i) {
for(j=0; j<c1; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"The second matrix is:"<<endl;
for(i=0; i<r2; ++i) {
for(j=0; j<c2; ++j)
cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
product[i][j] = 0;
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k) {
product[i][j]+=a[i][k]*b[k][j];
}
cout<<"Product of the two matrices is:"<<endl;
for(i=0; i<r1; ++i) {
for(j=0; j<c2; ++j)
cout<<product[i][j]<<" ";
cout<<endl;
}
}
return 0;
}