Hi guys! I was beginning the practice problem in pointers. the first problem was:
"Write a function that builds the multiplication table of arbitrary dimensions"..
Since the chapter dealt with pointer, double pointer and dynamic allocation of memory, I want the program to let user enter number of rows and columns for multiplication table(arbitrary dimension), then display the table and finally delete the memory allocated.
MY program at present, whenever run, popups message: "the program stopped working". I think it might be a memory leak but can't seem to understand it.
Also, I wanted to pass dynamically allocated 2-D array into function and thus, instinctively used triple pointer(the book only talked about double pointer). So, is there any other way to do the same without triple pointer and is the triple pointer even considered a good practice?
Any help would be highly appreciated.
My code at present:
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 42 43 44
|
#include <iostream>
using namespace std;
void multiply(int ***p_last_array,int rows,int column)
{
for(int i=0;i<rows;i++) //for filling array with values
{
for(int j=0;j<column;j++)
{
*p_last_array[i][j]=i*j;
}
}
for(int i=0;i<rows;i++) // displaying values
{
for(int j=0;j<column;j++)
{
cout<<*p_last_array[i][j]<<"/t";
}
cout<<endl;
}
delete p_last_array;
}
int main()
{
int rows;
int column;
cout<<"Enter number of rows: ";
cin>>rows;
cout<<"Enter number of column: ";
cin>>column;
int **p_p_pointer=new int*[rows];
for(int i=0;i<column;i++)
{
p_p_pointer[i]=new int[column];
}
multiply(&p_p_pointer,rows,column);
for(int i=0;i<rows;i++) //check if works by directly deleting double pointer;
{
delete[] p_p_pointer[i];
}
delete[] p_p_pointer;
}
|