Multiplication Table

Hello, everyone. I am in a c++ class and I have to create a multiplication table for an assignment. We have to use <iomanip> to format the cells so that they line up. My first problem, I have to make the "Mult table" on the same row as the horizontal numbers being displayed and I didn't know how to make a for loop for that. My second problem, I am not sure how to multiple the numbers together. I should end up with something similar to this if I entered:

a=0
nrows=5
b=0
ncols=5
p=0

1
2
3
4
5
6
Mult table    b=0     b=1     b=2      b=3     b=4
       a=0     0       0       0        0       0
       a=1     0       1       2        3       4
       a=2     0       2       4        6       8
       a=3     0       3       6        9      12
       a=4     0       4       8       12      16


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
    int astart,nrows,bstart,ncol,p;
    
    cout << "Enter start value for base a: ";
    cin >> astart;
    cout << "Enter how many base values: ";
    cin >> nrows;
    cout << "Enter start value for b: ";
    cin >> bstart;
    cout << "Enter how many b values: ";
    cin >> ncol;
    cout << "Enter precision: ";
    cin >> p;
    cout << endl;
    cout << "Table starts at multiplier of b = " << bstart << " with base a = " << astart << endl;
    cout << setw(10) << "Mult Table" << " ";
    
    for(bstart=0;bstart<ncol;bstart++)
    {
        cout << setw(3+p) << "b = " << bstart << " ";
    }
    cout << endl;
    
    for(astart=0;astart<nrows;astart++)
    {
        cout << setw(9) << " a = " << astart << endl;
        
    }
}
@thiskid

Replace your for loops with..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(bstart=0;bstart<ncol;bstart++)
    {
        cout << setw(3+p) << "b=" << bstart << " "; // Removed the spaces to match your illustration
    }
    cout << endl;
    
    for(astart=0;astart<nrows;astart++) // Your original loop
    {
        cout << setw(9) << "a=" << astart; // Again, removed the spaces before and after the =
		 for(bstart=0;bstart<ncol;bstart++)
		{
			cout << setw(4+p) << astart*bstart << " "; // Multiplies the two numbers
		}
		cout << endl; // Newline, of course
    }


But..

You are not using the user's inputs. You're starting the loops with a zero, and going up to base value.
To use their inputs, use this section, instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int x = bstart;x < bstart + ncol;x++)
    {
        cout << setw(3+p) << "b=" << x << " ";
    }
    cout << endl;
    
    for(int y = astart;y < astart + nrows;y++)
    {
        cout << setw(9) << "a=" << y;
		 for(int x = bstart;x < bstart + ncol;x++)
		{
			cout << setw(4+p) << x*y << " ";
		}
		cout << endl;
    }
Last edited on
@whitenite1 The second code is what I wanted! Thank you! But I am confused why the "a" values are incrementing by 10 digits every pass. The b columns are perfect but i cant figure out how to fix the a rows.
1
2
3
4
5
6
7
8
9
10
11
12
13
const int ROWS = 5 , COLUMNS = 5;


std::cout << "Mul table\tb=0\tb=1\tb=2\tb=3\tb=4" << std::endl;
for( int i = 0; i < ROWS; ++i )
{
    std::cout << "\ta = " << i << '\t';
    for( int j = 0; j < COLUMNS; ++j )
    {
        std::cout << i * j << '\t';
    }
    std::cout << std::endl;
}


http://ideone.com/Ap0xkI
@thiskid

Could you explain a bit better? Everything looks correct for me. The a table will start with what you input as start value for base a, and continue for how many base values you requested. So, if you input 8 for base a, and 6 for base values, a will print out from 8 to 13, and those will multiply with whatever number you inputted for base b.

1
2
3
4
5
6
7
Mult table    b=4     b=5     b=6      b=7     b=8
       a=8     32       40       48        56       64
       a=9     36       45       54        63       72
       a=10   40       50       60        70       80
       a=11    44      55       66        77       88
       a=12    48      60       72        84       96
       a=13    52      65       78        91      104

@whitenite1 I apologize, I was tweaking your code a little bit to do what my program specifically said and I accidentally typed "a+10" and it didn't do what I wanted it to do. But thank you for the code. My program now works perfectly!!
Topic archived. No new replies allowed.