Multiplication Table

After figuring out how to create a multiplication table using arrays--thanks to a post online--I was able to create a table myself; however, I need the table to display like:
1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16

The table I have displays:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

Also, is there another way of displaying the table instead of having the "\t"; and "\n"; in the code--the book I am using did not go over this and I am not sure if there is another way of displaying the table.

Thanks!

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
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

int main()
{
	//declare variables
	int rows = 10;
	int cols = 10;
	int table[rows][cols] = { 0 };
	int firstNum = 0;
	int secondNum = 0;

	//calculate values in table
	for (int x = 0; x < rows; x += 1)
	{
		for (int y = 0 ; y < cols; y += 1)
		table[x][y] = x * y;
	}

	//display the table
	for (int x = 1; x < rows; x += 1)
	{
		for (int y = 1; y < cols; y += 1)
		cout << table[x][y] << "\t";
		cout << "\n"; 
	}
	
	//request two nubers from 1 - 9
	cout << "Enter a number between 1 and 9" << endl;
	cin >> firstNum;
	cout << "Enter another number between 1 and 9" << endl;
	cin >> secondNum;
	
	//find the product of the two numbers by searching in the array
	if (firstNum > 0 && firstNum < 10 && secondNum > 0 && secondNum < 10)
	{
		cout << "The Product of the two numbers is: " << table[firstNum][secondNum] << endl;
	}
	else 
		cout << "Error: Please enter a number between 1 and 9" << endl;
	return 0;
}
is there another way of displaying the table instead of having the "\t"; and "\n"


So \t is tab and \n is new line

instead of tab you can use setw
instead of new line you can use endl

As far as your output goes, you'll want to add some cout statements to figure out why it's not displaying like you wish.
That's my first time hearing about setw, thank you for informing me about it. I tried it out and worked well. Furthermore, with the output, I am not sure as to what I could include to fix this issue since I don't know how to add the extra row and column to the table; maybe it was an error in the Professor's directions or an error with my calculation. Either way, thank you very much for the help!
Last edited on
Topic archived. No new replies allowed.