I am supposed to write a program to output Pascal’s Triangle. Ask the user to input the number of rows of the triangle to display. Also output for each row, the sum of the elements of the row, and show that it is equal to 2n, where n is the row number (with 0 being the first row).

This is what i have so far and my program compile but it just crashes before running. Can anyone help me please

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{

     cout << "Please enter the number of rows of the triangle to display cn: ";
     int cn;
   cin>> cn;
   int M [cn][cn];
   M [0][0] = 1;
   for (int i = 1; i < cn+1; i++) M [0] [i] = 0;
   M [1] [0] = 1;
   M [1] [1] = 1;
   for (int j = 2; j < cn+1; j++) M [1] [j] = 0;
   for (int k = 2; k < cn+1; k++){
   for (int l = 1; l < cn+1; l++)
   { 
       int n = 1; 
       if(l == 0) M[k][l]= 1;
       else M[k][l]= M[k-1][l]+M[k-1][l-1];
}}   
for (int p = 0; p < cn+1; p++) {
for (int q = 0; q < cn+1; q++)
cout << M [p][q]<< endl;}
system("Pause");
}

Recommended Answers

All 10 Replies

cin>> cn;
   int M [cn][cn];

Don't do that, ever. Unfortunately it may work - but in fact you cannot initialize an array of unknown size like that.

Even if the above worked, you are using

cn+1

as loop limit. That is, the last time around, the loop counter is cn. Remember, given an array foo[10], the maximal legal index is 9.

Finally, you are not calculating a triangle... but rather a square. But for now it is least of the worries.

Thanks But how can i fix those so that i am calculating a triangle?

First of all, replace line 12 with the proper set of new statements.
Then, replace all cn+1 to cn.
See if it is still crashes.

After that we may continue on the triangle matter.

Thanks i made those changed. The program does run now but the output is very funky :)

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{

     cout << "Please enter the number of rows of the triangle to display cn: ";
     int cn;
   cin>> cn;
   int M [cn][cn];
   M [0][0] = 1;
   for (int i = 1; i < cn; i++) M [0] [i] = 0;
   M [1] [0] = 1;
   M [1] [1] = 1;
   for (int j = 2; j < cn; j++) M [1] [j] = 0;
   for (int k = 2; k < cn; k++){
   for (int l = 1; l < cn; l++)
   { 
       int n = 1; 
       if(l == 0) M[k][l]= 1;
       else M[k][l]= M[k-1][l]+M[k-1][l-1];
}}   
for (int p = 0; p < cn; p++) {
for (int q = 0; q < cn; q++)
cout << M [p][q]<< endl;}
system("Pause");
}

Does line 22 ever execute?

how do i check ?

Put a printf before that statement

> how do i check ?

A preferred way is to set a breakpoint there.

Yea line 22 does work ! now what do i need to do please because the output is still not a triangle. Thanks

Do a google search for pascals triangle c . You will get lots of examples

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.