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.
#include<iostream>
usingnamespace 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;
}
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!