diff --git a/cpp/matrix/AdjacencyMatrix.cpp b/cpp/matrix/AdjacencyMatrix.cpp new file mode 100644 index 000000000..46707adaf --- /dev/null +++ b/cpp/matrix/AdjacencyMatrix.cpp @@ -0,0 +1,62 @@ +// Adjacency Matrix representation in C++ + +#include +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(); +} \ No newline at end of file diff --git a/cpp/matrix/MatrixAdd.cpp b/cpp/matrix/MatrixAdd.cpp new file mode 100644 index 000000000..b015f98c7 --- /dev/null +++ b/cpp/matrix/MatrixAdd.cpp @@ -0,0 +1,49 @@ +#include +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; +} \ No newline at end of file diff --git a/cpp/matrix/MatrixSub.cpp b/cpp/matrix/MatrixSub.cpp new file mode 100644 index 000000000..e2eb303ca --- /dev/null +++ b/cpp/matrix/MatrixSub.cpp @@ -0,0 +1,49 @@ +#include +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; +} \ No newline at end of file diff --git a/cpp/matrix/matrixMul.cpp b/cpp/matrix/matrixMul.cpp new file mode 100644 index 000000000..37b058c06 --- /dev/null +++ b/cpp/matrix/matrixMul.cpp @@ -0,0 +1,48 @@ +#include +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>a[i][j]; +} +} +cout<<"enter the second matrix element=\n"; +for(i=0;i>b[i][j]; +} +} +cout<<"multiply of the matrix=\n"; +for(i=0;i=left; j--, N++) + { + board[top][j] = N; + } + //fill from down to top + for(j=top-1; j>=left+1; j--, N++) + { + board[j][left] = N; + } + } + //print the pattern + for(i=0; i > v + = new ArrayList<>(Arrays.asList( + new ArrayList<>(Arrays.asList(5, 4, 7)), + new ArrayList<>(Arrays.asList(1, 3, 8)), + new ArrayList<>(Arrays.asList(2, 9, 6)))); + + int n = v.size(); + List x = new ArrayList<>(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + x.add(v.get(i).get(j)); + } + } + Collections.sort(x); + int k = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + v.get(i).set(j, x.get(k++)); + } + } + + System.out.println("Sorted Matrix Will be:"); + for (List row : v) { + for (int num : row) { + System.out.print(num + " "); + } + System.out.println(); + } + } +}