### 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

 ``123456`` ``````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``````

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include #include 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
@thiskid

 ``123456789101112131415`` ``````for(bstart=0;bstart

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.
 ``123456789101112131415`` ``````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.
 ``12345678910111213`` ``````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.

 ``1234567`` ``````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.