title: My attempt at Row-major order of array is showing correct values but indexing incorrect values
body: <p>I have made a class called matrix that stores values in a 1D array, but outputs it as a 2D array. I have included print statements to show the exact values supposedly being put into the array however when I use the print function to index it, it shows incorrect value on the second row last index. not entirely sure what I'm doing wrong.</p>
  <pre><code>#include &lt;iostream&gt;
  
  class Matrix
  {
      private:
          int rows{}, cols{};
          double *newArr;
      public:
          Matrix(int row, int col)
          {
              rows = row;
              cols = col;
              newArr = new double[rows * cols];
          }
          void setValue(int row, int col, double value)
          {
              std::cout &lt;&lt; value &lt;&lt; std::endl;
              newArr[row * row + col] = value;                
          }
          double getValue(int row, int col)
          {
              return newArr[row * row + col];                 
          }
          int getRows()
          {
              return rows;
          }
          int getCols()
          {
              return cols;
          }
          void print()
          {
              for (int i{}; i &lt; rows; ++i){
                  for (int j{}; j &lt; cols; ++j){
                      std::cout &lt;&lt; newArr[i * i + j] &lt;&lt; &quot; &quot;;
                  }
                  std::cout &lt;&lt; std::endl;
              }
          }
          ~Matrix()
          {
              delete[] newArr;
          }
  };
  
  int main()
  {
      Matrix x(3, 4);
  
      for (int i{}; i &lt; x.getRows(); i++){
          for (int j{}; j &lt; x.getCols(); j++){
              x.setValue(i, j, (j+i)/2.0);                     
          }
          std::cout &lt;&lt; std::endl;
      }
      std::cout &lt;&lt; std::endl;
  
      x.print();
  
      return 0;
  }
  </code></pre>