Pascal's triangle using C++ vectors This is my C++ program. I wanted to print out Pascal's triangle. I let the user decide how many rows to print out. The first row is considered as 0, and is just:


  1


I'm using vectors for this task. I think that I do not have to store the full triangle as that would take up too much extra space. I just store the previous row and the next one. I use the SetRow() function to determine the numbers in the next row based on the numbers in the previous row. Then each row is printed out. Please give me some feedback on my code. Is it an effective way to approach this problem? Does my code have any "bad programming practices"? What would you have done differently?

#include &lt;iostream&gt;
#include &lt;vector&gt;
using std::vector;
using std::cout;
using std::cin;
using std::endl;

void SetRow(vector&lt;int&gt; &amp;old_row, vector&lt;int&gt; &amp;new_row, int row);

int main() {
  int n_num;
  cout &lt;&lt; "What row of Pascal's triangle do you want to go up to?" &lt;&lt; endl;
  cin &gt;&gt; n_num;
  cout &lt;&lt; endl;

  vector &lt;int&gt; row_1 = {1};
  vector &lt;int&gt; row_2;
  vector &lt;int&gt; curr_row;

  cout &lt;&lt; row_1.at(0) &lt;&lt; endl;

  // loop through all the rows
  for (int i = 1; i &lt; (n_num + 1); i++) {
    if (i % 2 == 1) {
      SetRow(row_1, row_2, i);
      curr_row = row_2;
    } else if (i % 2 == 0) {
      SetRow(row_2, row_1, i);
      curr_row = row_1;
    }
    for (auto k: curr_row) {
      cout &lt;&lt; k &lt;&lt; ' ';
    }
    cout &lt;&lt; endl;
  }

  // This ends the program
  return 0;
}

void SetRow(vector&lt;int&gt; &amp;old_row, vector&lt;int&gt; &amp;new_row, int row) {
  new_row.clear();
  new_row.push_back(1);
  for (int i = 0; i &lt; (row - 1); i++) {
    new_row.push_back(old_row.at(i) + old_row.at(i + 1));
  }
  new_row.push_back(1);
}
The code seems all right; you might add the uniform variable initialization: int n_num {};. Furthermore, the way you read the number of rows is unstable, when you use std::cin to do it. Imagine you enter "b" instead of a number. Your program has no loop, that prevents that. You could use a function that only reads integers:

int readInt(std::istream&amp; stream) {
    /*
     * Function to get integers, that are entered by the user
     */
    int input;
    stream &gt;&gt; input;
    return input;
}


Then int num_n {readInt(std::cin)}; would be a possible way to do it.

Second off: why do you have the line else if (i % 2 == 0) ? I suppose you could just use else instead. If i % 2 == 1 returns false, it has to be 0. So there is no need to check again. It's like checking if your number is odd, and if it's not checking again if it's even.