Skip to content
Merged
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
83 changes: 83 additions & 0 deletions DSA/Matrix/DiagonalMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// C++ pgm to print diagonal matrix.
#include <iostream>
using namespace std;

class DiagonalMatrix
{
private:
int *A, n;

public:
DiagonalMatrix(int n)
{
this->n = n;
A = new int(n);
};
void set(int i, int j, int x);
int get(int i, int j);
void display();

~DiagonalMatrix();
};

// Here, i & j denotes the position where element should get inserted!
void DiagonalMatrix::set(int i, int j, int x)
{
// condition to fill elements in diagonal matrix.
if (i == j)
{
// inserting at proper index.
A[j - 1] = x;
}
}

// function used to fetch elements at specific index.
int DiagonalMatrix::get(int i, int j)
{
if (i == j)
{
return A[j - 1];
}
return 0;
}

// displays diagonal matrix.
void DiagonalMatrix::display()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
cout << A[i] << " ";
}
else
{
cout << "0 ";
}
}
cout << endl;
}
}

// destructor
DiagonalMatrix::~DiagonalMatrix()
{
delete[] A;
}

int main()
{
// pass dimansions
DiagonalMatrix DM(4);
// pass the values according to dimensions.
DM.set(1, 1, 5);
DM.set(2, 2, 6);
DM.set(3, 3, 7);
DM.set(4, 4, 8);
DM.display();
//cout<<DM.get(4,4);

return 0;
}
137 changes: 137 additions & 0 deletions DSA/Matrix/LowerTriMat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// C++ pgm to display lower tri. matrix in row & column major both the approches.
#include <iostream>
using namespace std;

class LTMatrix
{
private:
int n;
int *A;

public:
LTMatrix(int n)
{
this->n = n;
A = new int[n * (n + 1) / 2];
}
~LTMatrix() { delete[] A; }
void Display(bool row = true);
void setRowMajor(int i, int j, int x);
void setColMajor(int i, int j, int x);
int getRowMajor(int i, int j);
int getColMajor(int i, int j);
int getN() { return n; }
};

// set the elements in proper posn for matrix for row major approach!
void LTMatrix::setRowMajor(int i, int j, int x)
{
if (i >= j)
{
int index = ((i * (i - 1)) / 2) + j - 1;
A[index] = x;
}
}

// set the elements in proper posn for matrix for column major approach!
void LTMatrix::setColMajor(int i, int j, int x)
{
if (i >= j)
{
int index = (n * (j - 1) - (((j - 2) * (j - 1)) / 2)) + (i - j);
A[index] = x;
}
}

// get the elements from matrix for row major approach!
int LTMatrix::getRowMajor(int i, int j)
{
if (i >= j)
{
int index = ((i * (i - 1)) / 2) + j - 1;
return A[index];
}
else
{
return 0;
}
}

// get the elements from matrix for column major approach!
int LTMatrix::getColMajor(int i, int j)
{
if (i >= j)
{
int index = (n * (j - 1) - (((j - 2) * (j - 1)) / 2)) + (i - j);
return A[index];
}
else
{
return 0;
}
}

// displays the lower tri. matrix.
void LTMatrix::Display(bool row)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i >= j)
{
if (row)
{
cout << getRowMajor(i, j) << " ";
}
else
{
cout << getColMajor(i, j) << " ";
}
}
else
{
cout << 0 << " ";
}
}
cout << endl;
}
}

int main()
{

// can edit these values acc. to dimensions!
cout << "Matrix by row major approach: " << endl;
LTMatrix rm(4);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(2, 1, 2);
rm.setRowMajor(2, 2, 3);
rm.setRowMajor(3, 1, 4);
rm.setRowMajor(3, 2, 5);
rm.setRowMajor(3, 3, 6);
rm.setRowMajor(4, 1, 7);
rm.setRowMajor(4, 2, 8);
rm.setRowMajor(4, 3, 9);
rm.setRowMajor(4, 4, 10);

rm.Display();
cout << endl;

cout << "Matrix by column major approach: " << endl;
LTMatrix cm(4);
cm.setColMajor(1, 1, 1);
cm.setColMajor(2, 1, 2);
cm.setColMajor(2, 2, 3);
cm.setColMajor(3, 1, 4);
cm.setColMajor(3, 2, 5);
cm.setColMajor(3, 3, 6);
cm.setColMajor(4, 1, 7);
cm.setColMajor(4, 2, 8);
cm.setColMajor(4, 3, 9);
cm.setColMajor(4, 4, 10);

cm.Display(false);

return 0;
}
109 changes: 109 additions & 0 deletions DSA/Matrix/SymmetricMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// C++ pgm displays symmetric matrix given any elements in an array.
#include <iostream>
using namespace std;

class Symmetric
{
private:
int *A, n, x;

public:
Symmetric(int n);
void create(int n);
void set(int i, int j, int x);
int get(int i, int j);
void display();
~Symmetric();
};

// creates the mat. acc. to dimensions.
Symmetric::Symmetric(int n)
{
this->n = n;
A = new int[n * (n + 1) / 2];
}

// get the elements by user.
void Symmetric::create(int n)
{
this->n = n;
cout << "Enter the elements for " << n << "x" << n << " matrix: " << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> x;
set(i, j, x);
}
}
}

// set the elements in matrix at proper index.
void Symmetric::set(int i, int j, int x)
{
if (i >= j)
{
// row major mapping
A[i * (i - 1) / 2 + j - 1] = x;
}
else
// column major mapping
A[j * (j - 1) / 2 + i - 1] = x;
}

int Symmetric::get(int i, int j)
{
if (i >= j)
{
// row major mapping
return A[i * (i - 1) / 2 + j - 1];
}
else
{
// column major mapping
return A[j * (j - 1) / 2 + i - 1];
}
}

// display the matrix elements in proper manner.
void Symmetric::display()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i >= j)
{
cout << A[(i * (i - 1)) / 2 + (j - 1)] << " ";
}
else
{
cout << A[j * (j - 1) / 2 + i - 1] << " ";
}
}
cout << endl;
}
}

// destructor
Symmetric::~Symmetric()
{
delete[] A;
}

int main()
{
int n;
cout << "Enter the dimension for matrix: ";
cin >> n;

Symmetric S1(n);
S1.create(n);
S1.display();
// get the elements at specific index function;
// for 3x3 matrix below, for others u can edit!
cout << "The element at row 3 & column 2 is: " << S1.get(3, 2) << endl;
cout << "The element at row 2 & column 1 is: " << S1.get(2, 1) << endl;

return 0;
}