Hi All,

I have a program for a multiplication table that requires a user to indicate the table they want to see with an integer between 4 and 12 and I would like the final array (MultTable) to look like the following, is this possible?

   0   1   2   3   4   5
0  0   0   0   0   0   0 
1  0   1   2   3   4   5
2  0   2   4   6   8  10
3  0   3   6   9  12  15
4  0   4   8  12  16  20
5  0   5  10  15  20  25

I am able to make this work, but with one small issue. I am not able to get a null character, or blank space, in MultTable[0][0]. The output I am getting is:

0  0   1   2   3   4   5
0  0   0   0   0   0   0 
1  0   1   2   3   4   5
2  0   2   4   6   8  10
3  0   3   6   9  12  15
4  0   4   8  12  16  20
5  0   5  10  15  20  25

My code is posted below. Any help with this would be much appreciated.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int x;
    int rows;
    int columns;
    int ** MultTable;

    void ComputeTable(int **p, int rows, int cols);
    void PrintTable(int **p, int rows, int cols);

    // Prompt user for multiplication table integer value
    cout << "This program allows you to see multiplication tables between 4 and 12";
    cout << endl;
    cout << "Enter a integer x such that 4 <= x <= 12 that you would like to see: ";
    cin >> x;
    cout << endl;

    // Check that integer is valid
    while (!cin || x<4 || x>12)
    {
        cin.clear();              //clear error code for cin object
        cin.ignore(100, '\n');    //remove the illegal input
        cout << "Invalid Number Entered, Try Again: ";
        cin >> x;
        cout << endl;
    }

    rows = x+2;
    columns = x+2;

    //Create Rows of of Muliplication Table
    MultTable = new int* [rows];

    //Create Columns of Multiplication Table
    for (int row = 0; row < rows; row++)
        MultTable[row] = new int[columns];

    //Compute Multiplication Table
    ComputeTable(MultTable, rows, columns);

    //Print Multipication Table
    PrintTable(MultTable, rows, columns);

    system("pause");

    return 0;
}

void ComputeTable(int **p, int rows, int cols)
{
    p[0][0]=NULL; // have also tried '\0' and does not work...
    for (int row = 0; row < rows-1; row++)
    {
        p[0][row+1] = row;
        for(int col = 0; col < cols-1; col++)
            p[col+1][0] = col;
    }
    for(int row = 0; row < rows-1; row++)
    {
        for (int col = 0; col < cols-1; col++)
            p[row+1][col+1] = row*col;
    }
}

void PrintTable(int **p, int rows, int cols)
{

    for (int row = 0; row < rows; row++)
    {
        for (int col = 0; col < cols; col++)
            cout << setw(5) << p[row][col];
        cout << endl;
    }
}

Since the table is an array of integers, there is no such thing as null elements, every element has a value. NULL is defined as 0 in most operating systems and compilers, and that's why p[0[0]=NULL doesn't work the way you want it to work. A work-around is to set the element to some number that is not likely to be entered then test for that number. If all the valid numbers are poitive then you can set NULL elements to -1.

for (int col = 0; col < cols; col++)
{
   if( p[row][col] < 0)
      cout << setw(5) << " ";
    else
            cout << setw(5) << p[row][col];
}
This question has already been answered. Start a new discussion instead.