Pascal在C奇怪输出中的三角形

时间:2017-11-05 16:46:40

标签: c pascals-triangle

我正在尝试编写一个程序来显示Pascal的三角形,直到用户输入的级别数。我们不允许使用阶乘方法。我目前的代码是:

#include <stdio.h>

void trianglePrint(const int numLevels);

int main() {
  int numLevels;

  printf("Please enter how many levels of Pascal's Triangle you would 
  like to see: ");
  scanf("%d", &numLevels);

  trianglePrint(numLevels);


  return 0;
}

void trianglePrint(const int numLevels) {
  int pascalTriangle[28][28];
  int i, j;

  for (i = 0; i < numLevels; ++i) {
    for (j = 0; j <= i; ++j) {
      if (i == 0 || i == 1 || j == 0 || j == numLevels) {
        pascalTriangle[i][j] = 1;
        printf("%d ", pascalTriangle[i][j]);
      }
      else {
        pascalTriangle[i][j] = pascalTriangle[i - 1][j - 1] + 
        pascalTriangle[i - 1][j];
        printf("%d ", pascalTriangle[i][j]);
      }
    }
    printf("\n");
  }

}

我们应该能够达到28级,这就是我在两个维度上使用28号阵列的原因。 这适用于三角形的大约6个级别,但是对于更大的级别,它给出了非常大的整数。我认为这是由于未初始化的数组,但我不确定。有谁知道错误在哪里?

3 个答案:

答案 0 :(得分:1)

如果你改变了

public class MainActivity extends AppCompatActivity {
    private RelativeLayout mRelativeLayout;
    private int mCoordinatorHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mRelativeLayout = findViewById(R.id.relativeLayout);
        final AppBarLayout appBarLayout = findViewById(R.id.appBar);

        final CoordinatorLayout coordinatorLayout =
            (CoordinatorLayout) findViewById(R.id.coordinatorLayoutProfile);
        // Wait for just before drawing the view to get its height.
        coordinatorLayout.getViewTreeObserver()
            .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    // Remove the listener so we don't go into an infinite loop.
                    coordinatorLayout.getViewTreeObserver().removeOnPreDrawListener(this);
                    mCoordinatorHeight = coordinatorLayout.getHeight();
                    adjustRelLayoutHeight(mRelativeLayout, mCoordinatorHeight - appBarLayout.getBottom());
                    return false; // abort drawing
                }
            });

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                adjustRelLayoutHeight(mRelativeLayout, mCoordinatorHeight - appBarLayout.getBottom());
            }
        });
    }

    private void adjustRelLayoutHeight(RelativeLayout layout, int newHeight) {
        if (newHeight <= 0) {
            // no-op if height is invalid
            return;
        }
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layout.getLayoutParams();
        if (lp.height != newHeight) {
            lp.height = newHeight;
            layout.setLayoutParams(lp);
        }
    }

    private static final String TAG = "MainActivity";
}

if (i == 0 || i == 1 || j == 0 || j == numLevels)

(感谢Melpomene),然后对你阵列的所有访问最终都会被提升为成员。

这解决了陌生的数字。

输出:

if (i == 0 || i == 1 || j == 0 || j == i)

注意:
初始化阵列也是明智的预防措施。您可以使用有助于查找错误的值进行初始化,而不是隐藏错误,例如42。

答案 1 :(得分:0)

问题是,在pascalTriangle[i - 1][j]子句中使用pascalTriangle[i][j]计算else之前,您尚未设置{{1}}。

答案 2 :(得分:0)

尝试代码

void Pascal(int n)
    {
      int arr[n][n]; 


      for (int i = 0; i < n; i++)
      {

        for (int j = 0; j <= i; j++)
        {
          // First and last values in every row are 1
          if (i == j || j == 0)
               arr[i][j] = 1;
          else // Other values are sum of values just above and left of above
               arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
          printf("%d ", arr[i][j]);
        }
        printf("\n");
      }
    }