Trouble displaying a Multiplicaiton Table in c++

I'm having trouble generating a two dimensional array as a multiplication table. Any help would be greatly appreciated.

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
43
  #include <iostream>
  # include <iomanip>
  using namespace std;

  void lookup ();

   int main ()
   {
    int TimesTable[9][9];
    int i = 0;
    int j = 0;
    int product = 0;
    
    for (i = 1; i < 10; j++)
    {
        TimesTable [i-1][j-1] = i * j;
        
        cout << "Multiplication Table";
        for (j = 0; j < 9; j++)
        {
            for(i = 0; i < 9; i += 1)
            {
                  cout << setw(3) << TimesTable[i][j] << " ";
                  cout << endl;
             }
             lookup();
             return 0;
         }
       }

   system("pause");
   return 0;
   } //end of main function
      
       //**function definition**
       void lookup()
       {
            int x, a, b, c, i, j, TimesTable[9][9];
            cout << "Enter any two values: ";
            cin >> a >> b;
            c = a * b;
            cout << "Product = " << c << endl;
         } //end of void function 
I'm having trouble

What kind of trouble? How can we help, if you won't tell us what the problem is?

What's the point of the lookup() function?

You seem to create a table in your main function, and then completely ignore it, instead performing the multiplication again in lookup().
The table doesn't display properly. I'm supposed to generate the table and then use the lookup function to do the arithmetic.
Just to give you an idea...
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <iomanip> 
#include <cctype> // For toupper function.

void displayMultiplicationTable(int[][13]);
void findProduct(int&, int&);
void validateArrayBounds(int&);

int main()
{
    int table[13][13];
    int firstNum{ 0 }, secondNum{ 0 };
    char tryAgain{ ' ' };

    displayMultiplicationTable(table); 

    do
    {

	   findProduct(firstNum, secondNum);

	   std::cout << "The product is: " << table[firstNum][secondNum] << std::endl;

	   std::cout << "Would you like to try again?(Y/N)?";
	   std::cin >> tryAgain;

    } while (toupper(tryAgain) == 'Y');

    return 0;
}

void displayMultiplicationTable(int multTbl[][13])
{
    for (int countRows = 0; countRows < 13; countRows++)
    {
	   for (int countCols = 0; countCols < 13; countCols++)
	   {
		  multTbl[countRows][countCols] = countRows * countCols;
		  std::cout << std::setw(6) << multTbl[countRows][countCols];
	   }

	   std::cout << std::endl;
    }
}

void findProduct(int& num1, int& num2)
{
    std::cout << "Enter the first Number: ";
    validateArrayBounds(num1);

    std::cout << "Enter the second Number: ";
    validateArrayBounds(num2);
}

void validateArrayBounds(int& input)
{
    std::cin >> input;
    while (input < 0 || input > 12)
    {
	   std::cout << "The min/max input for the "
		  << "multiplication table is 0 - 12\n";
	   std::cout << "Please, try again.\n";
	   std::cout << ">>>";
	   std::cin >> input;
    }
}
Last edited on
Kmscott09 wrote:
The table doesn't display properly.

Look at where you're putting your endline. Is that really where you want it to be?

I'm supposed to generate the table and then use the lookup function to do the arithmetic.

Are you sure? Are you sure you're not supposed to use the lookup() function to look up the result in the table?

I mean, it would be weird to write a function to perform a calculation, and call it lookup(). It would be considerably less weird to write a function to look something up, and call it lookup().

Also, it would be weird to create a table of calculation results, and then ignore it and do the calculation over again anyway.

Wouldn't it?
Last edited on
Topic archived. No new replies allowed.