### Help with recursive multiplication table

Hey there. I am a beginning c++ programmer and had a few questions on a program i wrote.
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include int mult(int n, int a, int b, int tab[5][5]){ if( a == 0 && b <= n) { tab[a][b] = b; std::cout<< tab[a][b]; return mult(n, a, b+1, tab); } else if( b == 0){ tab[a][b] = a; std::cout << tab[a][b]; return mult(n, a, b + 1, tab); } else if(b <= n){ tab[a][b] = a * b; std::cout<

The code complies and runs as it should but at the end it will tell me segmentation fault. I was wondering why. Also, how could I set the array to a user entered size? Thanks.
On lines 3, 14, and 19, you accidentally used `<= n` instead of `< n`.

For arrays of user-defined size, I recommend you look into std::vector. You will want a vector of vectors.
thanks! that worked perfectly. do you know of anyway to do it other than using vectors? My teacher is really picky on what we use and we can not use std::vector or std::string.
If you are required to do it the deprecated way, you can do it like this:
 ``1234567`` ``````unsigned xsize, ysize; std::cin >> xsize >> ysize; int **tab = new int*[xsize]; for(unsigned i = 0; i < xsize; ++i) { tab[i] = new int[ysize]; }``````
 ``1234`` ``````int mult(int n, int a, int b, int **tab) { //... }``````
Thanks!
Topic archived. No new replies allowed.