Create a multiplication table program

Click For Summary
SUMMARY

The forum discussion centers on creating a multiplication table program in C++ using a 2-dimensional vector of integers. The program prompts the user for a number between 1 and 9, generates the multiplication table, and allows for repeated execution until the user opts to exit. Key features include input validation to restrict numbers to the specified range and the use of setw(n) for formatting output. The discussion also highlights the importance of understanding 0-based indexing in C++.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of 2-dimensional vectors in C++
  • Input validation techniques in C++
  • Basic knowledge of output formatting using setw(n)
NEXT STEPS
  • Implement error handling for user input in C++
  • Explore advanced formatting techniques in C++ for console output
  • Learn about dynamic memory allocation for 2D arrays in C++
  • Study algorithms for optimizing matrix operations in C++
USEFUL FOR

Beginner to intermediate C++ programmers, educators teaching programming concepts, and anyone interested in mastering 2D arrays and user input handling in C++.

carl123
Messages
55
Reaction score
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
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.
 
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
 

Similar threads

  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K