Pascal's Triangle function giving me an error

I am using Visual Studio , and have the following code for my Pascal's Triangle function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string> 
using namespace std;
  void printPascal(int n)
{
	const int row;
	row = n;
	const int col;
	col = n;
	int list2[row][col];
	for (int line = 0; line < n; line++)
	{ // number of integers is equal to line number
		for (int i = 0; i <= line; i++)
		{      
			if (line == i || i == 0)
				list2[line][i] = 1; //first and last value is 1
			else
				list2[line][i] = list2[line - 1][i - 1] + list2[line - 1][i];
			        cout << list2[line][i] << " ";
		}
		cout << "\n"; 
	}
} 

int main()
{
int n5;
n5 = 5;
printPascal(n5);
}


I am getting lots of errors for the declertation of list 2:
"Error (active) E0257 const variable "row" requires an initializer"
Same error for column and a few more related to the const variable.
Last edited on
Try writing it as
const int row = n;
const int col = n;

Initialisation means you declare and assign in the same statement.

Otherwise, what you wrote was an assignment (after the declaration).

You can only initialise constants.
It still does not work, I get the following error:
"array type 'int [row][col]' is not assignable "
You didn't make the array const as well did you?

Works for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$ cat foo.cpp
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string> 
using namespace std;

void printPascal(int n)
{
	const int row = n;
	const int col = n;
	int list2[row][col];
	for (int line = 0; line < n; line++)
	{ // number of integers is equal to line number
		for (int i = 0; i <= line; i++)
		{      
			if (line == i || i == 0)
				list2[line][i] = 1; //first and last value is 1
			else
				list2[line][i] = list2[line - 1][i - 1] + list2[line - 1][i];
			        cout << list2[line][i] << " ";
		}
		cout << "\n"; 
	}
} 

int main()
{
int n5;
n5 = 5;
printPascal(n5);
}
$ g++ foo.cpp
$ ./a.out 
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
@monicam..

You are trying to create an array during the program's run, which cannot be done that way.

Rows 10 and 12 don't need to be const either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string> 

using namespace std;

void printPascal(int n)
{
	int row = n;
	int col = n;
	int list2[5][5]; // Created an array size of int n5
// Increase the 5 if you increase n5

	for (int line = 0; line < n; line++)
	{ // number of integers is equal to line number

		string spaces(n-line,' '); // Added spaces to better represent a triangle on screen
		cout << spaces;
		for (int i = 0; i <= line; i++)
		{      
			if (line == i || i == 0)
				list2[line][i] = 1; //first and last value is 1
			else
				list2[line][i] = list2[line - 1][i - 1] + list2[line - 1][i];
			cout << list2[line][i] << " ";
		}
		cout << "\n"; 
	}
} 

int main()
{
	int n5 = 5;
	printPascal(n5);
}
You don't need a 2-d array to print Pascal's triangle: just a 1-d array big enough to hold the longest line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void printPascal( int n )
{
   int *L = new int[n]{};   L[0] = 1;
   for (int line = 0; line < n; line++ )
   {
      int Lim1 = 0;
      for (int i = 0; i <= line; i++ )
      {
         int temp = L[i];
         cout << ( L[i] += Lim1 ) << " ";
         Lim1 = temp;
      }
      cout << '\n'; 
   }  
   delete [] L;
} 

int main()
{
   printPascal( 5 );
}
Last edited on
up to a certain point, pow(11,row) works too :) where row = 0...4.
it stops working when the internals should be double digits (at 5) .. which is bigger than any polynomial I ever needed the coefficients for, so it more or less works for all practical use cases.
(Don't code it like this. Its just a fun math comment -- your professor wants to see the array addition logic).
Last edited on
Topic archived. No new replies allowed.