+1

multidimensional array

void build_pascal(int p[][SIZE], int n); void showmatrix(int p[][SIZE], int n); const int SIZE=100; int main() { int n; cout<<"Enter the number of rows for pascal triangle size: "; cin>>n } void build_pascal(int p[][SIZE], int n) { assert(n>0 && n<SIZE); for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) { if(i>n || j>i) { p[i][j] = 0; } else if(j==0 || j==i) { p[i] [j] =1; } else { p[i][j] = p[i-1][j-1]+p[i-1][j]; } } } showmatrix(p,n); } void showmatrix(int p[][SIZE], int n) { for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) { cout<<p[i][j]<<"\t"; } cout<<endl; } cout<<endl; } I've done the code up to this and it's not completed .How can I write the matrix and relate the integer to it?

4/26/2021 10:32:10 AM

Ramisa Fariha

+2

Ramisa, put your code in the code playground and provide a link. That way it is possible to refer to line numbers and error messages. The first problem is you are using the constant SIZE before you have even defined it. The second problem is your showmatrix function always dumps the entire matrix, not just the specified part. Don't call show from within build.

+1

https://code.sololearn.com/ca36A9a56A03 here is the code @Martin

+1

Ramisa, you need to learn how to read the error messages. The first error is... ./Playground/file0.cpp:4:27: error: 'SIZE' was not declared in this scope 4 | void build_pascal(int p[][SIZE], int n); as I mentioned in my previous post "you are using the constant SIZE before you have even defined it" You need to move line 6 up to line 4 so that size is declared before it is used. After doing that you will get an error ./Playground/file0.cpp: In function 'int main()': ./Playground/file0.cpp:11:11: error: expected ';' before '}' token 11 | cin>>n You forgot to end the line with a semi colon After fixing that you will get the error... ./Playground/file0.cpp: In function 'void build_pascal(int (*)[100], int)': ./Playground/file0.cpp:15:5: error: 'assert' was not declared in this scope 15 | assert(n>0 && n<SIZE); | ^~~~~~ ./Playground/file0.cpp:2:1: note: 'assert' is defined in header '<cassert>'; did you forget to '#include <cassert>'? 1 | #include <iostream> +++ |+#include <cassert> 2 | using namespace std; this is because you have not included the header <cassert> You need to correct those errors first in your code. The code will then run without errors but it won't do much.

+1

@Martin I' ve fixed those errors..Now what will be the next step?

+1

Add some blank lines between your functions. This helps to visually identify one function from another. Move the assert line out of the build_pascal() function and place it into the main() function after the line where you read the user input. The assert function will cause the program to exit if an invalid size is entered for the number of rows. So it makes more sense to check it at this point rather than later in the code. The assert function should also be altered to read... assert(n>0 && n<=SIZE); because it is legal to have a matrix that is equal to SIZE. Then delete the line showmatrix(p,n) from the build_pascal() function. The build_pascal() function should only build the Pascal Triangle, not display it. You now need to declare a 2D array to use as a matrix. This matrix should be created using the maximum size permitted, which is defined by the constant SIZE. int matrix[SIZE][SIZE]; Since a 100 x 100 matrix is somewhat larger than we can comfortably display on screen it would make more sense to redefine SIZE to 12. So change... const int SIZE=100; to const int SIZE=12; Now you can build your triangle by calling build_pascal() from main()... build_pascal(matrix, n); then display it... showmatrix(matrix, n); Which will show the problems with your showmatrix() function.

+1

Fixing the showmatrix() function. The showmatrix() function as written always shows the entire matrix no matter what size the user requests. This is because even though you pass the size n to the function it is not used. The two for-loops need to be adjusted to only display the requested number of rows and columns, not the total number of rows and columns in the matrix. Change the i<SIZE and j<SIZE to i<n and j<n so that only the cells used are printed. Try to correct your code to work correctly using the steps I have outlined above. This is my solution for you to use as a reference. https://code.sololearn.com/cA182A4A19a2/#cpp

+1

Thank you so much

0

the question was Write and test a function that creates Pascal’s Triangle in the square matrix that is passed to it. For example, if the two-dimensional array a and the integer 4 were passed to the function, then it would load the following into a: 10000 11000 12100 13310 14641