Make a C++ program that will print the multiplication table using nested loop.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int rows, columns, count, count_row, count_column;
bool r = 0, c = 0; // flags when determining the correct input
// of the number of rows and columns
// input rows & colomns and validation
while (!r) // the number of rows is correct?
{
cout << "Please enter a number of rows on the interval [1, 10]: ";
cin >> rows;
if (cin.fail() || rows<1 || rows>10) {
cout << "\nThat is not within the interval [1, 10]. Pleace try again... " << endl;
cin.clear(); // clears the error flag
cin.ignore(24, '\n'); // Extracts characters from the input sequence and discards them
}
else {
r = 1; // number of rows is correct
while (!c) { // the number of columns is correct?
cout << "Please enter a number of columns on the interval [1, 10]: ";
cin >> columns;
if (cin.fail() || columns<1 || columns>10) {
cout << "\nThat is not within the interval [1, 10]. Pleace try again... " << endl;
cin.clear(); // clears the error flag
cin.ignore(24, '\n'); // Extracts characters from the input sequence and discards them
}
else
c = 1; // number of columnsis correct
}
}
}
// The declaration of a two-dimensional dynamic array:
int **ptrarray = new int*[rows];
for (count = 0; count < rows; count++) // rows in array
ptrarray[count] = new int[columns]; //columns
// ptrarray – array ptr
// recorded data in a two-dimensional dynamic array
for (count_row = 0; count_row < rows; count_row++)
for (count_column = 0; count_column < columns; count_column++)
ptrarray[count_row][count_column] = (count_column + 1)*(count_row + 1); // // recorded data in two-dimensional dynamic array
// output on display
cout << endl;
cout << " |";
for (count_column = 0; count_column < columns; count_column++)
cout << setw(3) << count_column + 1 << "|"; // number of columns
cout << endl;
for (count_column = 0; count_column <= columns; count_column++)
cout << "---+";
cout << endl;
count_column = 0;
for (count_row = 0; count_row < rows; count_row++) { // output number of each row
cout << setw(3) << count_row + 1 << "|"; // number of rows
while (count_column<columns) {
cout << setw(3) << ptrarray[count_row][count_column] << "|"; // output rezult of multiplication
count_column++; // next value in row
}
count_column = 0; // before the next row, zero the column number
cout << endl;
for (count = 0; count <= columns; count++) // output of the dividing line in the table
cout << "---+";
cout << endl;
}
// Deleting a two-dimensional dynamic array
for (count = 0; count < rows; count++)
delete[]ptrarray[count];
cout << endl;
return 0;
}
Comments
Leave a comment