title: Gaussian elimination code
body: <p>I have tried to write code that reads in values from an nxn matrix and then print those values out. I think I have succeeded in doing this. The next step I have tried is to solve the matrix using Gaussian elimination but I can't get the code to print out the values. Could someone please have a look at this and tell me where I'm going wrong?</p>
  
  <pre><code>  #include &lt;cstdlib&gt;
    #include &lt;cmath&gt;
    #include &lt;vector&gt;
    #include &lt;iostream&gt;
    #include &lt;fstream&gt;
    #include &lt;string&gt;
  
    using namespace std;
  
  
     int main(){
     //create a 2D vector of doubles 
     vector&lt; vector&lt; double &gt; &gt; matrix; 
  
  
      int num_lines = 0;
      double temporary, r;
      int i, j, k, dimension, temp; /* counter variables for loops */
  
  
  
  
      // Open a file containing the matrix data
      ifstream myFile("test_data2.txt");
      // Check if file is open
      if(myFile.is_open()){
      // First step is to detect matrix size by assuming matrix is
      // square and counting number of columns.
  
      // Tempoary variable to hold current line to process
      string line;
  
      // Tempoary variable to hold number of rows and cols in square matrix
      int num_lines = 0;
  
      // Count rows / cols
      while(getline(myFile, line)){
          num_lines ++;
      }
  
      // Reset EOF flag
      myFile.clear();
  
      // Reset to start of file
      myFile.seekg(0, myFile.beg);
  
      // Second step is to grab lines from the file and process them
      // splitting each line into substrings and converting them into
      // doubles
  
      // For each line
      for(int l = 0; l &lt; num_lines; l ++){
          // Process line by line
          getline(myFile, line);
  
          // Tempoary vector to hold rows of matrix data
          vector&lt;double&gt; temp;
  
          // Tempoary variable to hold search positions
          int start = 0;
          int end;
          int length;
  
          // Get value by value
          for(int n = 0; n &lt; num_lines; n ++){
              // Break line down by finding commas
              end = line.find(',', start);
              length = end - start;
  
              // Extract substring
              string tempstr = line.substr(start, length);
  
              // Set next value of start
              start = end + 1;
  
              // Convert to double
              temp.push_back(atof(tempstr.c_str()));
          }
  
          // Add row to matrix
          matrix.push_back(temp);
      }
      // Close input file
      myFile.close();
  } else {
      // If input file failed to open, print an error
      cout &lt;&lt; "Error opening input file" &lt;&lt; endl;
  }
  // Print out the matrix (will do nothing if input file open failed)
  cout &lt;&lt; "Print out input file." &lt;&lt; endl;
  
  for(int i = 0; i &lt; matrix.size(); i ++)
  {
      for(int j = 0; j &lt; matrix[i].size(); j ++)
      {
          cout &lt;&lt; matrix[i][j];
          // Add commas and new lines when required
          // Remember not to add commas after rightmost values, or
          // a new line at the end of the last value
          if(j &lt; matrix[i].size() - 1)
          {
              cout &lt;&lt; ' '; // Character, not string
          }
          else
          {
              if(i &lt; matrix[i].size() - 1)
              {
                  cout &lt;&lt; '\n'; // Character, not string
              }
          }
      }
  }
  
  
  
  
  
  
   for (i = 0; i &lt; num_lines; i++)
      for (j = num_lines; j &lt; 2 * num_lines; j++)
          if (i == j % num_lines)
              matrix[i][j] = 1;
          else
              matrix[i][j] = 0;
  
    /* using gauss-jordan elimination */
    for (j = 0; j &lt; num_lines; j++) {
      temp = j;
  
      /* finding maximum jth column element in last (dimension-j) rows */
      for (i = j + 1; i &lt; num_lines; i++)
          if (matrix[i][j] &gt; matrix[temp][j])
              temp = i;
  
  
  
      /* swapping row which has maximum jth column element */
      if (temp != j)
          for (k = 0; k &lt; 2 * num_lines; k++) {
              temporary = matrix[j][k];
              matrix[j][k] = matrix[temp][k];
              matrix[temp][k] = temporary;
          }
  
        /* performing row operations to form required identity matrix out of the input              matrix */
      for (i = 0; i &lt; num_lines; i++)
          if (i != j) {
              r = matrix[i][j];
              for (k = 0; k &lt; 2 * num_lines; k++)
                  matrix[i][k] -= matrix[j][k] * r / matrix[j][j];
          } else {
              r = matrix[i][j];
              for (k = 0; k &lt; 2 * dimension; k++)
                  matrix[i][k] /= r;
          }
     }
  
     /* Display augmented matrix */
     printf("\n After Gauss-Jordan elimination, augmented matrix is : \n\n");
  
    for (i = 0; i &lt; num_lines; i++) {
       for (j = 0; j &lt; 2 * num_lines; j++)
          printf("  %4.2f", matrix[i][j]);
        printf("\n");
     }
  
  
     /* displaying inverse of the non-singular matrix */
     printf("\n\n\n The inverse of the entered non-singular matrix is : \n\n");
  
      for (i = 0; i &lt; num_lines; i++) {
      for (j = num_lines; j &lt; 2 * num_lines; j++)
          printf("  %.5f", matrix[i][j]);
      printf("\n");
    }
  
  
   return 0;
    }
  
      // EXAMPLE FILE
      //1,2,3
      //4.5,6.7,8.8
      //-110,-55.3,+53.723
      // END OF EXAMPLE FILE REMOVE COMMENTS AT TOP AND BOTTOM AND "//" characters before    use. Save as "matrix.csv".
  </code></pre>