bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_2 / assignments / assignment_01.cpp

assignment_01.cpp (2917B)


      1 /* Write a program to implement a Diagonal Matrix a Lower Triangular Matrix, an Upper Triangular Matrix, and a
      2 Symmetric Matrix using a one-dimensional array. */
      3 
      4 #include <iostream>
      5 
      6 using namespace std;
      7 
      8 class Matrix
      9 {
     10 private:
     11   int n, *arr;
     12 
     13 public:
     14   Matrix(int size)
     15   {
     16     n = size;
     17     arr = new int[n * (n + 1) / 2];
     18   }
     19 
     20   void setDiagonal(int i, int j, int x)
     21   {
     22     if (i == j)
     23       arr[i] = x;
     24   }
     25   void setLowerTri(int i, int j, int x)
     26   {
     27     if (i >= j)
     28       arr[i * (i + 1) / 2 + j] = x;
     29   }
     30   void setUpperTri(int i, int j, int x)
     31   {
     32     if (i <= j)
     33       arr[n * i - (i * (i - 1) / 2) + (j - i)] = x;
     34   }
     35   void setSymmetric(int i, int j, int x)
     36   {
     37     (i >= j) ? arr[i * (i + 1) / 2 + j] = x : arr[j * (j + 1) / 2 + i] = x;
     38   }
     39 
     40   int getDiagonal(int i, int j)
     41   {
     42     return (i == j) ? arr[i] : 0;
     43   }
     44   int getLowerTri(int i, int j)
     45   {
     46     return (i >= j) ? arr[i * (i + 1) / 2 + j] : 0;
     47   }
     48   int getUpperTri(int i, int j)
     49   {
     50     return (i <= j) ? arr[n * i - i * (i - 1) / 2 + (j - i)] : 0;
     51   }
     52   int getSymmetric(int i, int j)
     53   {
     54     return (i >= j) ? arr[i * (i + 1) / 2 + j] : arr[j * (j + 1) / 2 + i];
     55   }
     56 
     57   void display(int type)
     58   {
     59     cout << endl
     60          << "Matrix:" << endl;
     61     for (int i = 0; i < n; i++)
     62     {
     63       for (int j = 0; j < n; j++)
     64       {
     65         if (type == 1)
     66           cout << getDiagonal(i, j) << " ";
     67         else if (type == 2)
     68           cout << getLowerTri(i, j) << " ";
     69         else if (type == 3)
     70           cout << getUpperTri(i, j) << " ";
     71         else if (type == 4)
     72           cout << getSymmetric(i, j) << " ";
     73       }
     74       cout << endl;
     75     }
     76   }
     77 
     78   ~Matrix()
     79   {
     80     delete[] arr;
     81   }
     82 };
     83 
     84 int main()
     85 {
     86   int n, choice, x;
     87   cout << "Enter the order of the matrix : ";
     88   cin >> n;
     89   Matrix M(n);
     90 
     91   cout << "\n1. Diagonal Matrix.";
     92   cout << "\n2. Lower Triangular Matrix.";
     93   cout << "\n3. Upper Triangular Matrix.";
     94   cout << "\n4. Symmetric Matrix.";
     95   cout << "\nEnter your choice : ";
     96   cin >> choice;
     97 
     98   switch (choice)
     99   {
    100   case 1:
    101     cout << "\nEnter the diagonal elements : ";
    102     for (int i = 0; i < n; i++)
    103     {
    104       cin >> x;
    105       M.setDiagonal(i, i, x);
    106     }
    107     M.display(1);
    108     break;
    109 
    110   case 2:
    111     cout << "\nEnter the elements : ";
    112     for (int i = 0; i < n; i++)
    113     {
    114       for (int j = 0; j <= i; j++)
    115       {
    116         cin >> x;
    117         M.setLowerTri(i, j, x);
    118       }
    119     }
    120     M.display(2);
    121     break;
    122 
    123   case 3:
    124     cout << "\nEnter the elements : ";
    125     for (int i = 0; i < n; i++)
    126     {
    127       for (int j = i; j < n; j++)
    128       {
    129         cin >> x;
    130         M.setUpperTri(i, j, x);
    131       }
    132     }
    133     M.display(3);
    134     break;
    135 
    136   case 4:
    137     cout << "\nEnter the elements : ";
    138     for (int i = 0; i < n; i++)
    139     {
    140       for (int j = 0; j <= i; j++)
    141       {
    142         cin >> x;
    143         M.setSymmetric(i, j, x);
    144       }
    145     }
    146     M.display(4);
    147     break;
    148 
    149   default:
    150     cout << "Invalid Choice.";
    151   }
    152   return 0;
    153 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror