title: Dynamic Multidimensional array in C++
body: <p>I made Matrix Multiplication program in c++ using dynamic Multidimensional arrays. 
  problem is when i enter test values 
  matrix A = row1{ 1 }  ,  row2 { 2 }  matrix B = row1 {1 ,2 , 3}  , it stops working at the loop where user 
  inputs values of first array , i found it using debugging. but program works fine when i enter
  matrix A = row1{ 1 , 2  }  ,  row2 { 3 , 4  }  matrix B = row1 {5 , 6 } , row2 {7 , 8}</p>
  
  <p>i want this program to be a general program that can multiply all matrixs</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Lab_02
  {
  public:
      void Product(){
      int a1Rows, a1Columns;
      int a2Rows, a2Columns;
      cout &lt;&lt; "Plz Enter the no. of rows for Array 1      :";
      cin &gt;&gt; a1Rows;
  
      cout &lt;&lt; "Plz Enter the no. of columns for Array 1   :";
      cin &gt;&gt; a1Columns;
  
      cout &lt;&lt; "Plz Enter the no. of rows for Array 2      :";
      cin &gt;&gt; a2Rows;
  
      cout &lt;&lt; "Plz Enter the no. of columns for Array 2   :";
      cin &gt;&gt; a2Columns;
  
      int **dynamicArray = 0;
      int **dynamicArray2 = 0;
      int **dynamicArray3 = 0;
      cout &lt;&lt; endl;
  
  
      for (int i = 0; i &lt; a1Rows; i++)
      {
          dynamicArray3 = new int *[a1Rows];
      }
      for (int i = 0; i &lt; a2Columns; i++)
      {
          dynamicArray3[i] = new int[a2Columns];
      }
  
  
  
      // memory allocated for elements of rows. 
      for (int i = 0; i &lt; a1Rows; i++)
      {
          dynamicArray = new int *[a1Rows];
      }
      // memory allocated for  elements of each column.  
      for (int i = 0; i &lt; a1Columns; i++)
      {
          dynamicArray[i] = new int[a1Columns];
      }
  
  
      // memory allocated for elements of rows. 
      for (int i = 0; i &lt; a2Rows; i++)
      {
          dynamicArray2 = new int *[a2Rows];
      }
      // memory allocated for  elements of each column.  
      for (int i = 0; i &lt; a2Columns; i++)
      {
          dynamicArray2[i] = new int[a2Columns];
      }
  
  
      cout &lt;&lt; "enter the values or array 1 \n";
      for (int i = 0; i &lt; a1Rows; i++)
      {
          for (int j = 0; j &lt; a1Columns; j++)
          {
              cout &lt;&lt; "array[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "]\t";
              cin &gt;&gt; dynamicArray[i][j];
  
          }
  
      }
  
      cout &lt;&lt; "enter the values or array 2 :\n";
  
      for (int i = 0; i &lt; a2Rows; i++)
      {
          for (int j = 0; j &lt; a2Columns; j++)
          {
              cout &lt;&lt; "array[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "]\t"; 
              cin &gt;&gt; dynamicArray2[i][j];
  
          }
      }
  
  
      int sum; 
  
  
      for (int i = 0; i &lt; a1Rows; i++)
      {
          for (int j = 0; j &lt; a1Columns ; j++)
          {
              sum = 0;
              for (int k = 0; k &lt;  a2Columns ; k++)
              {
                  sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
              }
              dynamicArray3[i][j] = sum;
          }
  
      }
  
  
          cout &lt;&lt;"Result" &lt;&lt; endl &lt;&lt; endl;
          for (int i = 0; i &lt; a1Rows; i++)
          {
              for (int j = 0; j &lt; a2Columns; j++)
              {
                  cout &lt;&lt; dynamicArray3[i][j] &lt;&lt; "\t";
  
              }
              cout &lt;&lt; endl;
          }
  
  
      }
     };
  
  
          void main(void)
          {
  
           Lab_02 object;   
             object.Product();
  
            }
  </code></pre>