Question

In: Computer Science

First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and...

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";
}

Solutions

Expert Solution

// 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:


Related Solutions

Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Need this C++ code to be modified to work in C, still using 2d arrays... #include...
Need this C++ code to be modified to work in C, still using 2d arrays... #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; //Implementation of main function int main() { srand(time(NULL)); //Declaration of output,result,i,j,k ,figure as integer type and //assign flog with 0 int output[5][5], result[5], i, j, k, figure = 0; //Display statement cout << "The classic BINGO cards contains 25 squares arranged in five vertical" << endl; cout << "columns and five side to side rows. Each...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an...
Skills needed to complete this assignment: dynamic arrays, classes. PROMPT: In mathematics, a polynomial is an expression consisting of cariables and coefficients which involves only the operation of addition, subtraction, multiplication, and non-negative integer exponents of variables. A polynomial in a single variable can always be written in the form: anxn + an-1xn-1+ ....... + a2x2 + a1x + a0 Where a0 ....., an are coefficients and x is the variable. In this assignment, you will complete a polynomial class...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output) In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores,...
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
I have this program in C that takes three char arrays that each have a first...
I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers. #include <stdio.h> void upper_string(char []); int main() { char name1[100]="John Smith"; char name2[100]="Mary Cohen"; char name3[100]="Carl Williams"; upper_string(name1);// calling...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT