Creating a Multiplication Table

Page 1 of 1

3 Replies - 5934 Views - Last Post: 22 July 2009 - 06:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=116225&amp;s=e18d64963e687972467c5a5d7c0f7c44&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 NewbieDan

Reputation: 0
• Posts: 12
• Joined: 22-July 09

Creating a Multiplication Table

Posted 22 July 2009 - 10:10 AM

Hello all. I was looking for some help with an assignment I have in my C++ class. Im at my wits end with it.
So, to the point.
it is a console program.
The program has to take 2 numbers, one representing the starting number the other the ending number, and take those two numbers and output a multiplication table.
The area I'm lost on is the sum of the table. It is suppose to take all the numbers on the table and add them up, but I cant get it to do it. Every thin I've tried breaks the program.
The table Format is off, bit I may be able to figure that out on my own.

For the purposes of consistency, Im useing 2 for the first number and 5 for the second.
The output should look like this
X 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Rows printed = 4
Columns printed = 4
Sum: 196

But Im getting
X 1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
Rows printed: 2
Columns printed: 5
Sum of the Table: 9

```#include <iostream>

using namespace std;

int main()
{
int low = 0;
int high = 0;
int rows = 0;
int columns = 0;
int accumOne = 0;
int accumTwo = 0;
cout << "Enter first number: ";
cin >> rows;
cout << "Enter second numnber: ";
cin >> columns;
cout << endl;
cout << "THE MULTIPLICATION TABLE";
cout << endl;
for (high = 1; high <= columns; high++)
{
cout << "\t" << high;
}
cout << endl;
accumOne = accumOne + high;
for (low = 1; low <= rows; low++)
{
cout << low;
accumTwo = accumTwo + low;
for (high = 1; high <= columns; high++)
{
cout << "\t" << low * high;
}
cout << endl;
}
cout << "Rows printed: " << rows << endl;
cout << "Columns printed: " << columns << endl;
cout << "Sum of table: " << accumOne + accumTwo << endl;

return 0;
}

```

This post has been edited by NewbieDan: 22 July 2009 - 10:11 AM

Is This A Good Question/Topic? 0

Replies To: Creating a Multiplication Table

#2 eker676

• Software Engineer

Reputation: 379
• Posts: 1,833
• Joined: 18-April 09

Re: Creating a Multiplication Table

Posted 22 July 2009 - 10:40 AM

Since, I have a lot of time on my hands I debugged it and fixed it for you.

That practice is not approved of by DIC but, I was feeling nice and a genuine effort was made on your part.

In the future, debug all your code when you get logic errors like these. ( To debug your code download Visual C++ Express Edition 2008, place a breakpoint, and hit the play button )

```#include <iostream>

using namespace std;

int main()
{
int low = 0;
int high = 0;
int rows = 0;
int columns = 0;
int sum = 0;

cout << "Enter first number: ";
cin >> rows;
cout << "Enter second numnber: ";
cin >> columns;
cout << endl;
cout << "THE MULTIPLICATION TABLE";
cout << endl << endl;

// Print out top number row, THESE are NOT calculated values
for (int i = rows; i <= columns; i++)
{
cout << "\t" << i;
}
cout << endl;

// Print the inner rows, THESE ARE calculated values
for (int j = rows; j <= columns; j++)
{
cout << j; // Print the row #

for (int k = rows; k <= columns; k++)
{
cout << "\t" << j * k;
sum += j * k; // Add the number to the sum
}

cout << endl; // Next row
}

cout << "\nRows printed: " << columns << endl; // This needs to be columns, because the program prints out the same # of rows
// and columns
cout << "Columns printed: " << columns << endl;
cout << "Sum of table: " << sum << endl << endl;

return 0;
```

This post has been edited by eker676: 22 July 2009 - 10:41 AM

#3 NewbieDan

Reputation: 0
• Posts: 12
• Joined: 22-July 09

Re: Creating a Multiplication Table

Posted 22 July 2009 - 11:11 AM

Thanks a ton. Im seeing what I did wrong and what I was missing.

So, it would seem to me that I should have used local variables in each of the for loops instead of trying to track the values globally.

#4 eker676

• Software Engineer

Reputation: 379
• Posts: 1,833
• Joined: 18-April 09

Re: Creating a Multiplication Table

Posted 22 July 2009 - 06:22 PM

Always try to use local variables instead of global variables.