Create a multiplication table program

In summary: This way each row and column is computed once, and the result is stored in an array.def multi_table(n): A = [[0 for i in xrange(0, n)] for j in xrange(0,n)] for i in xrange(0, n): A[i][j] = (i + 1) * (j + 1) for j in xrange(i, n): A[j][i] = A[i][j
  • #1
carl123
56
0
Create a program in C++ that makes a multiplication table. Ask the user how many numbers should be in the table.

Requirements
Store all of the data in a 2-dimensional vector of ints.
Allow the program to run repeatedly until the user is finished.
Do not allow inputs outside the range of 1-9

Sample Run
Enter the numbers for multiplication table (1-9): -1Enter the numbers for multiplication table (1-9): 10Enter the numbers for multiplication table (1-9): 4 | 1 2 3 4
- | - - - -
1 | 1 2 3 4
2 | 2 4 6 8
3 | 3 6 9 12
4 | 4 8 12 16

| 4 3 2 1
- | - - - -
4 | 16 12 8 4
3 | 12 9 6 3
2 | 8 6 4 2
1 | 4 3 2 1

Do you want to do another? (y/n) y

Enter the numbers for multiplication table (1-9): 3 | 1 2 3
- | - - -
1 | 1 2 3
2 | 2 4 6
3 | 3 6 9

| 3 2 1
- | - - -
3 | 9 6 3
2 | 6 4 2
1 | 3 2 1

Do you want to do another? (y/n) n

Things to Consider

You can use setw(n) before any number and it will add blank spaces to show the number in n columns. That is how to line up your columns.
Don't forget you have 0 based indexes and numbers that go from 1 to 9.
 
Technology news on Phys.org
  • #2
Please read rule #11 (click on "Expand" button in the beginning of the page) and Mark's remark in https://driven2services.com/staging/mh/index.php?posts/75931/. To demonstrate your effort, you could describe what you know and don't know how to do in this problem.
 
  • #3
carl123 said:
Create a program in C++ that makes a multiplication table. Ask the user how many numbers should be in the table.

This is a simple problem to test your understanding of 2D arrays.

You could approach it in the standard way, like this (porting the code from Python to C++ is left as an exercise to the reader ;) ):
Code:
def multi_table_(n):
    A = [[0 for i in xrange(0, n)] for j in xrange(0,n)]
    for i in xrange(0, n):
        for j in xrange(0, n):
            A[i][j] = (i + 1) * (j + 1);    print A

But you'd be wasting some computing power. You could recognise the symmetry in the matrix and approach it like this instead:
Code:
def multi_table(n):
    A = [[0 for i in xrange(0, n)] for j in xrange(0,n)]
    for i in xrange(0, n):
        for j in xrange(i, n):
            A[i][j] = (i + 1) * (j + 1);
            A[j][i] = A[i][j]    print A
 

FAQ: Create a multiplication table program

What is a multiplication table program?

A multiplication table program is a computer program designed to display a table of numbers, usually from 1 to 10, and their corresponding products when multiplied together. This allows users to quickly and easily find the product of any two numbers without having to manually calculate it.

Why would I need a multiplication table program?

A multiplication table program can be useful for many reasons. It can help with learning and practicing multiplication, especially for young students. It can also save time and effort when needing to find the product of two numbers. Additionally, it can be used in various fields of science and mathematics to quickly perform calculations.

How does a multiplication table program work?

A multiplication table program works by using a loop to iterate through the numbers 1 to 10, multiplying each number by the other numbers in the range and displaying the products in a table format. This process is repeated for each number in the range, resulting in a complete multiplication table.

Can I customize the multiplication table in the program?

Yes, most multiplication table programs allow for customization. Users can choose the range of numbers to be displayed, the number of rows and columns in the table, and even the appearance of the table. Some programs may also allow for the selection of specific numbers to be included or excluded in the table.

Is a multiplication table program accurate?

Yes, a multiplication table program is accurate as it uses a mathematical calculation to determine the products of numbers. However, it is always important to double check the results, as with any calculation, to ensure accuracy.

Similar threads

Replies
27
Views
2K
Replies
3
Views
886
Replies
9
Views
2K
Replies
34
Views
3K
Replies
17
Views
1K
Replies
1
Views
1K
Replies
23
Views
2K
Replies
3
Views
1K
Back
Top