In C++, Step 1: Write a program that stores a multiplication table in a 9 -by- 9
ID: 3818972 • Letter: I
Question
In C++,
Step 1: Write a program that stores a multiplication table in a 9-by-9 two-dimensional array. Generate the multiplication table with two loops. (So you will have a nested loop that will iterate 9 times and fill the 9x9 array with the values.)
Step 2: Display the table for the user to see it.
Here is an example of a multiplication table that is 12x12. You will just go to 9.
Step 3: Create a function that returns the product of two numbers between 1 and 9 by looking up the product in the array. Example: if the user enters 9 and 2 the program will look up the answer in the two dimensional array and display 18.
Explanation / Answer
#include <iostream>
using namespace std;
int product(int a, int b) {
return a * b;
}
int main()
{
int table[9][9];
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
table[i][j] = product(i+1, j+1);
}
}
cout<<"Multiplecation table is "<<endl;
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
cout<<table[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Multiplecation table is
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81