Making a simple multiplication table with four variables.

Hey everyone,

I have a question regarding how to create a multiplication table. I'm trying to have the user input 4 integers (R1, R2, C1, C2) and use these variables to create a table that shows the multiplication from R1 and R2 and C1 and C2.
I want it to look like this (but with the numbers in the middle of each space). I'm having trouble with adding the proper spacing with the dashes and lines.
1 2 3 4 5 6 7 8 9 10
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
1 1 2 3 4 5 6 7 8 9 10
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
2 2 4 6 8 10 12 14 16 18 20
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
3 3 6 9 12 15 18 21 24 27 30
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
4 4 8 12 16 20 24 28 32 36 40
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
5 5 10 15 20 25 30 35 40 45 50
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
6 6 12 18 24 30 36 42 48 54 60
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
7 7 14 21 28 35 42 49 56 63 70
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
8 8 16 24 32 40 48 56 64 72 80
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
9 9 18 27 36 45 54 63 72 81 90
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
10 10 20 30 40 50 60 70 80 90 100
-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|


this is all i have so far.

#include <cmath>
#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
int r1, r2, c1, c2;
string line = "---|";
int line_num;

cin >> r1 >> r2 >> c1 >> c2;

int delta_r = 1;
if(r2 < r1)
{
delta_r = -1;
}
int delta_c = 1;

if(c2 < c1)
{
delta_c = -1;
}
for(int r = 0; r <= abs(r1 - r2); r++)
{
int r_formula = (r1 + r * delta_r);

cout << r_formula << endl;

for(int c = 0; c <= abs(c1 - c2); c++)
{
cout << setw(6) << r_formula * (c1 + c * delta_c);

}

cout << endl;
}

}
Last edited on
I think the best way to make proper spacing for your output would be to play around with how it looks and try tabs [/t] and spaces [/s]. This can be a bit time consuming, though.

Perhaps a better way to do that kind of spacing, is to learn C++ window creation tool on Visual Studios. Although, learning that tool will be even more time consuming. XD

Tutorial for C++ windows creation tool:
https://msdn.microsoft.com/en-us/library/bb384843.aspx

Also, please remember to use code tags!

I hope this helps,

~Hirokachi
Last edited on
The main thing I am having trouble with is how to add that weird ----| under each number.
Topic archived. No new replies allowed.