Skip to content
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

Transpose #582

Open
wants to merge 14 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
62 changes: 62 additions & 0 deletions cpp/matrix/AdjacencyMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Adjacency Matrix representation in C++

#include <iostream>
using namespace std;

class Graph {
private:
bool** adjMatrix;
int numVertices;

public:
// Initialize the matrix to zero
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new bool*[numVertices];
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = false;
}
}

// Add edges
void addEdge(int i, int j) {
adjMatrix[i][j] = true;
adjMatrix[j][i] = true;
}

// Remove edges
void removeEdge(int i, int j) {
adjMatrix[i][j] = false;
adjMatrix[j][i] = false;
}

// Print the martix
void toString() {
for (int i = 0; i < numVertices; i++) {
cout << i << " : ";
for (int j = 0; j < numVertices; j++)
cout << adjMatrix[i][j] << " ";
cout << "\n";
}
}

~Graph() {
for (int i = 0; i < numVertices; i++)
delete[] adjMatrix[i];
delete[] adjMatrix;
}
};

int main() {
Graph g(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);

g.toString();
}
49 changes: 49 additions & 0 deletions cpp/matrix/MatrixAdd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
using namespace std;

int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;

cout << "Enter number of rows (between 1 and 100): ";
cin >> r;

cout << "Enter number of columns (between 1 and 100): ";
cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

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

// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];

// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}

return 0;
}
49 changes: 49 additions & 0 deletions cpp/matrix/MatrixSub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
using namespace std;

int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;

cout << "Enter number of rows (between 1 and 100): ";
cin >> r;

cout << "Enter number of columns (between 1 and 100): ";
cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

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

// Subtracting Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] - b[i][j];

// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}

return 0;
}
48 changes: 48 additions & 0 deletions cpp/matrix/matrixMul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
46 changes: 46 additions & 0 deletions cpp/matrix/matrixTranspose.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
using namespace std;

int main() {
int a[10][10], transpose[10][10], row, column, i, j;

cout << "Enter rows and columns of matrix: ";
cin >> row >> column;

cout << "\nEnter elements of matrix: " << endl;

// Storing matrix elements
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}

// Printing the a matrix
cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}

// Computing transpose of the matrix
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}

// Printing the transpose
cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}

return 0;
}
59 changes: 59 additions & 0 deletions cpp/matrix/spiralMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// C++ program for the above approach

#include <bits/stdc++.h>
using namespace std;

vector<int> spiralOrder(vector<vector<int> >& matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<int> ans;

if (m == 0)
return ans;

vector<vector<bool> > seen(m, vector<bool>(n, false));
int dr[] = { 0, 1, 0, -1 };
int dc[] = { 1, 0, -1, 0 };

int x = 0, y = 0, di = 0;

// Iterate from 0 to m * n - 1
for (int i = 0; i < m * n; i++) {
ans.push_back(matrix[x][y]);
// on normal geeksforgeeks ui page it is showing
// 'ans.push_back(matrix[x])' which gets copied as
// this only and gives error on compilation,
seen[x][y] = true;
int newX = x + dr[di];
int newY = y + dc[di];

if (0 <= newX && newX < m && 0 <= newY && newY < n
&& !seen[newX][newY]) {
x = newX;
y = newY;
}
else {
di = (di + 1) % 4;
x += dr[di];
y += dc[di];
}
}
return ans;
}

// Driver code
int main()
{
vector<vector<int> > a{ { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };

// Function call
for (int x : spiralOrder(a)) {
cout << x << " ";
}
return 0;
}

// This code is contributed by Yashvendra Singh
49 changes: 49 additions & 0 deletions java/matrix/AdjMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Adjacency Matrix representation in Java

public class Graph {
private boolean adjMatrix[][];
private int numVertices;

// Initialize the matrix
public Graph(int numVertices) {
this.numVertices = numVertices;
adjMatrix = new boolean[numVertices][numVertices];
}

// Add edges
public void addEdge(int i, int j) {
adjMatrix[i][j] = true;
adjMatrix[j][i] = true;
}

// Remove edges
public void removeEdge(int i, int j) {
adjMatrix[i][j] = false;
adjMatrix[j][i] = false;
}

// Print the matrix
public String toString() {
StringBuilder s = new StringBuilder();
for (int i = 0; i < numVertices; i++) {
s.append(i + ": ");
for (boolean j : adjMatrix[i]) {
s.append((j ? 1 : 0) + " ");
}
s.append("\n");
}
return s.toString();
}

public static void main(String args[]) {
Graph g = new Graph(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);

System.out.print(g.toString());
}
}
Loading