In: Computer Science
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and double pointers?
The assignment asks to
Write a program that displays a 2D multiplication table based on row and column value specified by the user. Perform a data validation to ensure the number of rows and columns given by the user exist on the interval [1, 10]. If possible, protect against inputs that contain symbols that would normally crash the program (i.e. letter, symbols, numbers containing a decimal point, etc..).
While completing it, you must show your understanding of double pointers (int**), and dynamic memory And also store the internal portion of the table (i.e. the numbers on the inside that represent the products) in a dynamic 2D array (also known as heap memory).
#include
#include
using namespace std;
int main() {
int row = 0;
int col = 0;
do {
cout << "Please enter the
number of rows on the interval [1, 10]: ";
cin >> row;
if (row < 1 || row > 10) cout
<< "\nThat is not within the range [1-10]. Please try
again...\n";
} while (row < 1 || row > 10);
do {
cout << "Please enter the
number of columns on the interval [1, 10]: ";
cin >> col;
if (col < 1 || col > 10) cout
<< "\nThat is not within the range [1-10]. Please try
again...\n";
} while (col < 1 || col > 10);
int** ppRootPointer = NULL;
ppRootPointer = new(int* [col]);
for (int i = 0; i < col; i++) {
ppRootPointer[i] = new(int
[row]);
}
int counter = 1;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++)
{
ppRootPointer[i][j] = counter++;
}
}
cout << left;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++)
{
cout <<
ppRootPointer[i][j];
}
cout << endl <<
endl;
cout <<
left;
for (int i = 0; i < col; i++)
{
for (int j = 0;
j < row; j++) {
cout << *(*(ppRootPointer + i) + j);
}
cout <<
endl;
}
cout << endl <<
endl;
for (int i = 0; i <
col; i++) {
delete[]
ppRootPointer[i];
}
delete[]
ppRootPointer;
}
for(int i = 0; i <= col; i++){
if(i == 0){
cout << " ";
continue;
}
cout << "|" << i;
}
for(int i = 1; i <= row; i++){
cout << "\n---";
for(int j = 1; j <= col; j++){
cout << "+---";
}
cout << "\n";
cout << i;
for(int j = 1; j <= col; j++){
cout << "|" << i * j;
}
}
cout << "\n";
}
// C++ program to print multiplication table using 2D array
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;
int main() {
int row,col;
int i,j;
int **mulTable = NULL; // define the multiplication table
// input of number of rows
cout << "Please enter the number of rows on the interval [1, 10]: ";
cin>>row;
// validate row and re-prompt until valid value is entered
while(cin.fail() || row < 1 || row > 10)
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // and remove the bad input
cout << "That is not within the range [1-10]. Please try again : ";
cin>>row;
}
// input of number of columns
cout<<"Please enter the number of columns on the interval [1, 10]: ";
cin>>col;
// validate col and re-prompt until valid value is entered
while(cin.fail() || col <1 || col > 10)
{
cin.clear(); // back in 'normal' operation mode
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // and remove the bad input
cout << "That is not within the range [1-10]. Please try again : ";
cin >> col;
}
// create multiplication table of rows row and columns col
mulTable = new int*[row];
for(i=0;i<row;i++)
{
mulTable[i] = new int[col];
for(j=0;j<col;j++)
mulTable[i][j] = (i+1)*(j+1);
}
// print the multiplication table
cout<<endl<<endl<<"Multiplication Table : "<<endl;
for(j=0;j<=col;j++)
{
cout<<"+-----";
}
cout<<"+"<<endl;
cout<<"|"<<left<<setw(5)<<"";
for(j=0;j<col;j++)
{
cout<<"|"<<left<<setw(5)<<(j+1);
}
cout<<"|"<<endl;
for(j=0;j<=col;j++)
{
cout<<"+-----";
}
cout<<"+";
for(i=0;i<row;i++)
{
cout<<endl<<"|"<<left<<setw(5)<<(i+1);
for(j=0;j<col;j++)
cout<<"|"<<left<<setw(5)<<mulTable[i][j];
cout<<"|"<<endl;
for(j=0;j<=col;j++)
{
cout<<"+-----";
}
cout<<"+";
}
cout<<endl;
// delete the memory allocated
for(int i=0;i<row;i++)
delete [] mulTable[i];
delete [] mulTable;
return 0;
}
//end of program
Output: