简体   繁体   English

C++中使用向量的帕斯卡三角矩阵

[英]Pascal triangle matrix using vectors in C++

I need make Pascal Triangle matrix using vectors and then print it.我需要使用向量制作 Pascal 三角矩阵,然后打印它。

This algorithm would work with arrays, but somehow it doesn't work with matrix using vectors.该算法适用于 arrays,但不知何故不适用于使用向量的矩阵。

#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?你能帮我修复这段代码吗?

Your code won't compile because you have numerous errors in PascalTriangle .您的代码无法编译,因为您在PascalTriangle中有很多错误。

For one, you initialize a matrix with no elements.首先,您初始化一个没有元素的矩阵。 Additionally, you use matrix indices starting at 1 rather than 0.此外,您使用从 1 而不是 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;
}

The main problem is that in PascalTriangle , you are starting out with an empty Matrix in both the number of rows and columns.主要问题是,在PascalTriangle中,您从行数和列数的空Matrix开始。

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.由于我的评论提到了push_back ,如果您没有使用传入的元素数初始化Matrix ,这里是使用它的方法。

The other issue is that NumberOfColumns should specify the row, not just the matrix vector.另一个问题是NumberOfColumns应该指定行,而不仅仅是矩阵向量。

The final issue is that you should be passing the Matrix by const reference, not by value.最后一个问题是您应该通过 const 引用而不是值来传递Matrix

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 :然后在NumberOfColumns中:

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

And then, NumberOfRows :然后, NumberOfRows

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

And last, PrintMatrix :最后, 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这是一个现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM