# Print a 10x10 times table

I have been on the C++ learning grind for the last 2 weeks using caveofprogramming videos so don't judge too hard if this seems like a dumb question.

My last lesson covered arrays and the assignment was to make a 10x10 times table using multidimensional arrays. I succeeded in making this times table but with my knowledge I had to type type out each integer. Is this the best way to handle this scenario or is there a way that I could calculate most of the integers without typing out 10 lines of arrays?

#include<iostream>
#include<Windows.h>
using namespace std;

int main() {
int animals[10][10] = {
{1,2,3,4,5,6,7,8,9,10},
{2,4,6,8,10,12,14,16,18,20},
{3,6,9,12,15,18,21,24,27,30},
{4,8,12,16,20,24,28,32,36,40},
{5,10,16,20,25,30,35,40,45,50},
{6,12,18,24,30,36,42,48,54,60},
{7,14,21,28,35,42,49,56,63,70},
{8,16,24,32,40,48,56,64,72,80},
{9,18,27,36,45,54,63,72,81,90},
{10,20,30,40,50,60,70,80,90,100},
};
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout << animals[i][j] << " " << flush;
}
cout << endl;
}
return 0;
}


If you're sure about the data that you want in your array, like the one you mentioned, each row is an arithmetic progression, then this can be done easily using loop.

int n = 10, m = 10; //you can take this as user input
int animals[n][m];
int indexRow, indexColumn;
for (indexRow = 0; indexRow < n ; indexRow++) {
for (indexColumn = 0; indexColumn < m; indexColumn++) {
animals[indexRow][indexColumn] = (indexRow + 1) + (indexColumn)*(indexRow + 1);
}
}


What we have done here is, used formula of nth term of an arithmetic progression while setting values for each row. As you see, first element of each row is it's index number + 1, and nth term in the series is column's index number + 1 So we applied the following formula:

a + (n-1)d


where a is the first term of the series, n refers to the nth term, and d is the common difference.

After applying the above, we can see the array as:

for (indexRow = 0; indexRow < n ; indexRow++) {
for (indexColumn = 0; indexColumn < m; indexColumn++) {
cout << animals[indexRow][indexColumn] << " " << flush;
}
}


which prints expected output.

animals is an, um, unconventional name for a table of products.

There doesn't seem to be any compelling need to have the array, though - just print out the values as you reach them:

for (int i = 1;  i <= 10;  ++i) {
for (int j = 1; j <= 10;  ++j) {
std::cout << i * j << " ";
}
std::cout << '\n';
}


It would be nice to make the numbers line up (since it's supposed to be a table). We can do this using the I/O manipulator std::setw() (found in <iomanip>). That would give the resulting complete program:

#include <iomanip>
#include <iostream>

int main()
{
static constexpr int limit = 10;
for (int i = 1;  i <= limit;  ++i) {
for (int j = 1;  j <= limit;  ++j) {
std::cout << std::setw(4)
<< i * j;
}
std::cout << '\n';
}
}


Some other observations, incorporated above:

• Avoid using namespace std, especially at file scope.
• Returning 0 from main() is optional; in the main function you're allowed to run off the end, and a return value of zero is inferred. That's not allowed in any other value-returning function, of course.
• Prefer to use prefix ++ and -- when the value isn't used. Although that likely doesn't affect your code applying them to int, it can save a costly copy with some types, so it's a good habit to cultivate.
• There's no need to flush the output (with std::flush or std::endl). Just let the buffering do its job.
• When I was at school, multiplication tables went up to 12. Perhaps that's different where/when you are, though (I guess decimal currency reduces the need to have multiples of 11 and 12 learnt by rote?). We can use a named constant so we can easily change the limit and still have a square table.

The two previous answers create the array at run-time. I want to add the fact that you can use constexpr to compute it at compile time exactly as if you had typed it in.

Also, Don’t write using namespace std;.

You should use just output \n instead of using endl. And your explicit call to flush is not necessary and just slows it down.

Don't include <windows.h> when your code only uses standard C++ and doesn't call any Windows API.

Even though devRabbit's answer yields the right answer, it makes more sense to me to create a times table with multiplication.

const int n = 10, m = 10;
int animals[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
animals[i][j] = (i + 1)*(j + 1);
}
}


Also, when printing, the numbers will be lined up nicely if you use a tab instead of a space to separate them.

• That makes a lot of sense now that I see it. Thank you for the answer! Jul 26 at 12:18