Home > Back-end >  Pascal triangle matrix using vectors in C
Pascal triangle matrix using vectors in C

Time:03-20

I need make Pascal Triangle matrix using vectors and then print it.

This algorithm would work with arrays, but somehow it doesn't work with matrix using vectors.

#include <iomanip>
#include <iostream>
#include <vector>

typedef std::vector<std::vector<int>> Matrix;

int NumberOfRows(Matrix m) { return m.size(); }
int NumberOfColumns(Matrix m) {
  if (m.size() != 0)
    return m[0].size();
  return 0;
}

Matrix PascalTriangle(int n) {
  Matrix mat;
  int a;
  for (int i = 1; i <= n; i  ) {
    a = 1;
    for (int j = 1; j <= i; j  ) {
      if (j == 1)
        mat.push_back(j);
      else
        mat.push_back(a);
      a = a * (i - j) / j;
    }
  }
  return mat;
}

void PrintMatrix(Matrix m, int width) {
  for (int i = 0; i < NumberOfRows(m); i  ) {
    for (int j = 0; j < NumberOfColumns(m); j  )
      std::cout << std::setw(width) << m[i][j];
    std::cout << std::endl;
  }
}

int main() {
  Matrix m = PascalTriangle(7);
  PrintMatrix(m, 10);

  return 0;
}

I get nothing on screen, and here's the same code just without matrix using vectors program (which works fine).

Could you help me fix this code?

CodePudding user response:

The main problem is that in PascalTriangle, you are starting out with an empty Matrix in both the number of rows and columns.

Since my comments mentioned push_back, here is the way to use it if you did not initialize the Matrix with the number of elements that are passed in.

The other issue is that NumberOfColumns should specify the row, not just the matrix vector.

The final issue is that you should be passing the Matrix by const reference, not by value.

Addressing all of these issues, results in this:

Matrix PascalTriangle(int n) 
{
    Matrix mat;
    for (int i = 0; i < n; i  )
    {
        mat.push_back({}); // creates a new empty row
        std::vector<int>& newRow = mat.back(); // get reference to this row
        int a = 1;
        for (int j = 0; j < i   1; j  ) 
        { 
            if (j == 0) 
                newRow.push_back(1);
            else
                newRow.push_back(a);
            a = a * (i - j) / (j   1); 
        }
    }
    return mat;
}

And then in NumberOfColumns:

int NumberOfColumns(const Matrix& m, int row) 
{
    if (!m.empty())
        return m[row].size();
    return 0;
}

And then, NumberOfRows:

int NumberOfRows(const Matrix& m) { return m.size(); }

And last, PrintMatrix:

void PrintMatrix(const Matrix& m, int width) 
{
    for (int i = 0; i < NumberOfRows(m); i  ) 
    {
        for (int j = 0; j < NumberOfColumns(m, i); j  )
            std::cout << std::setw(width) << m[i][j];
        std::cout << std::endl;
    }
}

Here is a live demo

CodePudding user response:

Your code won't compile because you have numerous errors in PascalTriangle.

For one, you initialize a matrix with no elements. Additionally, you use matrix indices starting at 1 rather than 0.

The following prints things for me:

Matrix PascalTriangle(int n) {
  Matrix mat(n, std::vector<int>(n, 0)); // Construct Matrix Properly
  int a;
  for (int i = 0; i < n; i  ) { // Start index at 0
    a = 1;
    for (int j = 0; j < i   1; j  ) { // Start index at 0
      if (j == 0) // Changed 1 to 0
        mat[i][j] = 1;
      else
        mat[i][j] = a;
      a = a * (i - j) / (j 1); // Changed j to j 1 since j starts at 0
    }
  }
  return mat;
}
  • Related