Sup dude.I have a C++ problem. I'm learning about pointers atm and I did this:
Code:int Table(int a, int b){ int **p_p_table; p_p_table = new int*[a]; for(int i=0; i<a; i++){ p_p_table[a] = new int[b]; } for(int i=0; i<a; i++){ for(int j=0; j<b; j++){ p_p_table[i][j] = i*j; } } for(int i=0; i<1; i++){ for(int j=0; j<b; j++){ cout << p_p_table[i][j] << " "; } cout << " " << endl; } } int main () { int height; int width; cout << "Enter the Dimensions of the table: "; cin >> height; cin >> width; Table(height,width); }
Basically, It's suppossed to be a function that let's the user create a multiplication table of their desired dimensions but whenever I run it and entert he dimension the program crashes because I'm not allocatting the memory correctly, How exactly do I do that?
@StrikerX ........ Batao mujhe :crazy:
Well I'm sure entirely sure what you're trying to do, but first of all, I don't think you need a 2D array for a multiplication table, a 1D array is suitable. Secondly, since your function is only printing and not returning anything, so it should be void Table(int a, int b).
Also, it's a good idea to control your coding style now, don't leave meaningless spaces if you can help it, better to make this a habit early, secondly give the variables a better name especially for functions. Here's a modified version of your code with one-dimensional array if you want.
Code:
#include<iostream>
using namespace std;
void Table(int a, int b)
{
int *p_p_table = new int[b+1]; //To go uptil 10
for(int j=0; j<(b+1); j++){
p_p_table[j] = a*j;
}
for(int j=1; j<(b+1); j++){
cout <<a <<" x " <<j <<" = " <<p_p_table[j] <<endl;
}
}
int main ()
{
int height;
int width;
cout << "Enter the Dimensions of the table: ";
cin >> height;
cin >> width;
Table(height,width);
}
Last edited: