title: My c++ program that reads in a matrix and prints out the non-zero numbers is generating a runtime error
body: <p>This is a simple program that reads in a matrix from the command line. The first 2 numbers it reads in represent the rows and columns and then it reads in a matrix of floating point numbers containing 0's. It reads these in line by line and stores the line temporarily in an array of floats. While it reads in the line it checks for non zero numbers and increments nz which stores the number of non zero numbers for each row and nztotal which is the total number of non zero numbers. It then goes back through this array and prints out the index and value of each non zero number. It has to back through the array so that it can print out nz first. Here is the code:</p>
  
  <pre><code>     #include &lt;iostream&gt;
          #include &lt;stdlib.h&gt;
          using namespace std;
  
          int main(int argc, char* argv[]) {
              //puting the rows and columns into ints
              int ar = atoi(argv[1]);
              int ac = atoi(argv[2]);
              //printing out the rows;
              cout&lt;&lt; ar;
              int nz = 0;
              int nztotal = 0;
              //creating an array of floats
              float* arr = new float[ac];
              //reading through line by line
              for(int i = 0; i &lt; ar;i++)
              {
                  cout &lt;&lt; endl;
                  nz = 0;
                  //reading through number by number
                  for(int j = 0;j &lt; ac; j++)
                  {
                      //storing each number in the array
                      cin&gt;&gt; arr[j];
                      //checking if the number is non-zero and incrementing nz and nztotal
                      if(arr[j] != 0)
                      {
                          nz++;
                          nztotal++;
                      }
                  }
                  //printing out nz
                  cout&lt;&lt; nz;
                  //reading through the array and printing out the values and index of each non-zero number
                  for(int j = 0;j &lt; ac; j++)
                  {
                      if(arr[j] != 0)
                      {
                          int temp = j + 1;
                          cout&lt;&lt; temp &lt;&lt; arr[j];
                      }
                  }
              }
              cout&lt;&lt; nztotal;
          }
  </code></pre>
  
  <p>This is a sample input:</p>
  
  <pre><code>4 4 2 0 3 0 2 0 3 0 2 0 3 0 2 0 3 0
  </code></pre>
  
  <p>This should produce this as an output:</p>
  
  <pre><code>4
  2 1 2 3 3
  2 1 2 3 3
  2 1 2 3 3
  2 1 2 3 3
  8
  </code></pre>
  
  <p>But instead it is generating a runtime error and nothing is getting printed out. I am pretty sure it just me doing something stupid</p>