Skip to main content
1 of 5
user31782
  • 251
  • 2
  • 7

Different approaches to generate formatted mathematical multiplication tables in C/C++

I am self studying C++ from Robert Lafore's OOP in C++. I am solving the first exercise questions of chapter-3 loop and decisions. I've written three different codes using different approaches. I want to get feedback over my codes like a teacher is supposed to give feedback to his student. I want to get feedback over each code individually and then want to know which solution is overall better or more efficient.

The exercise is:

Assume that you want to generate a table of multiples of any given number. Write a program that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this (only the first three lines are shown):

    Enter a number: 7
    7     14     21     28     35     42     49     56     63     70
   77     84     91     98    105    112    119    126    133    140
  147    154    161    168    175    182    189    196    203    210

Using a for loop inside while loop: I've written solution for user-defined number of columns and rows. The most obvious solution was to keep track of current position in the table. The number at a particular position should original_num * pos. And after every maxcolth position print a newline.

#include<iostream>
#include<iomanip>    // For setw()
#include<conio.h>    // For getch()
using namespace std;
int main()
{
    int number;
    cout << "Enter the number whose table you want: ";
    cin >> number;
    int maxrows; int maxcols;
    cout << "\nEnter the number of rows you want: "; cin >> maxrows;
    cout << "\nEnter the number of columns : "; 
    cin >> maxcols;
    int pos=1; //starting position
    
    while( pos<=maxrows*maxcols )
    {
        for(int col=1; col<=maxcols; col++, pos++)
        {
            cout<< setw(6) << pos*number << "  ";
        }
        cout<<"\n";
    }
    getch();
}  
 

The inner for loop finishes after maxcol number of repeatitions. The outer while loop puts a newline and repeats again.

Using a single for loop with an if statement inside: Another idea was to force myself to use only one loop because, in principle, the second loop should be redundant as we need to increment only pos to find the value at that position. There must be some way to tell the computer to print a newline after every maxcol number of executions -- this is acheived by the if statement.

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
    int number;
    cout << "Enter the number whose table you want: ";
    cin >> number;
    int maxrows; int maxcols;
    cout << "\nEnter the number of rows you want: "; cin >> maxrows;
    cout << "\nEnter the number of columns : "; cin >> maxcols;
    int row; int col; //Current row and current col
    
    for(int pos=1; pos<=(maxrows*maxcols); pos++)
    {
        cout << setw(6) <<number*pos << "  ";
        if((pos%maxcols)==0)
        {
          cout << "\n"; 
        }
        
    }
    getch();    
}

The problem is the if statement doesn't get executed most of the times. For 10 maxcols if gets executed only 10% times. It mostly wastes cpu time. In the previous version every statement does some important function every single time it is executed.

Keep track of current row and column: The idea was to locate the current position by knowing the current row and column of that position. It is quite similar to the first solution but there is an important advantage that I'll mention after the code.

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
    int number;
    cout << "Enter the number whose table you want: ";
    cin >> number;
    int maxrows; int maxcols;
    cout << "\nEnter the number of rows you want: "; cin >> maxrows;
    cout << "\n Enter the number of columns : "; cin >> maxcols;
    
    for(int row=0; row<=(maxrows-1); row++)
    {
        for(int col=1; col<=maxcols; col++)
        {
            cout << setw(6) << number*(col + row*maxcols); // (col + row*maxcols) represent the current position
        }
        cout << "\n";
    }
    getch();
}

Variable row is not the current row but number of rows above the current row. row*maxcols is the position of first number of current row. col is simply the current column. The beauty of this approach is that the way current position is determined allows us to print the multiplication table in vertically increasing manner too. AS per the exercise the numbers 7, 14, 21 ... are printed horizontally -- what if I wanted to print the vertically and jump to next col after a particular number of rows? Here is the solution:

#include<iostream>
#include<iomanip>    // For setw()
#include<conio.h>    // For getch()
using namespace std;
int main()
{
    int number;
    cout << "Enter the number whose table you want: ";
    cin >> number;
    int maxrows; int maxcols;
    cout << "\nEnter the number of rows you want: "; cin >> maxrows;
    cout << "\n Enter the number of columns : "; 
    cin >> maxcols;
    
    
    
    for(int row=1; row<=maxrows; row++ )
    {       
        cout << "\n";
        for(int col=0; col<maxcols; col++)
        {
            cout << setw(6)<< number*(row + maxrows*col) << "  ";
        }   
        
        
    }
    getch();    
}

row is the current row now and col is the number of columns to the left of current position. (row + maxrows*col) represent the current position.


Question:

  • Please study every code individually and tell if the idea used could be implemented better. Could the number of variables used deceased further? Could the algorithm be improved further? Could the code be more efficient/optimised further?

  • In the second version I removed one loop and instead used an if statement? Which is better? Use two loops or one loop plus an if statement?

  • Overall which version is more professional/good/efficient? Which one would be faster at run time?

  • I am new to programming and this is the first which I've started reading seriously. Is the way I am solving exercises ok for a starter? I'm not asking for opinion based answer. Just a suggestion. This last point should be considered as a kinda P.S. Please don't close the question rather skip this P.S.

Thank you.

user31782
  • 251
  • 2
  • 7