Help with multiplication table in c ++

1

I need to create a multiplication table with a matrix in such a way that it looks like this:

But I can not make the table of 0 appear, this is my progress:

#include <iostream>
using namespace std;
    main()
{
    short A[12][12],f,c;
        for(c=0;c<11;c++)
        {
            A[0][c]=c;    
        }

        for(f=1;f<11;f++)
        {
            A[f][0]=f;
        }

        for(c=1;c<11;c++)
        {
            for(f=1;f<11;f++)
            {
                A[f][c]=A[f][0]*A[0][c];
            }
        }

        for(f=0;f<11;f++)
        {
            for(c=0;c<11;c++)
            {
                cout<<A[f][c]<<"  ";
            }
            cout<<endl<<endl;
        }

    system("pause");
    return 0;
}

Could you code it please?

Thank you.

    
asked by Frank Molina 13.07.2017 в 16:36
source

3 answers

2

It's pretty simple:

int tabla[12][12]{};

for (int fila = 0; fila < 12; ++fila)
{
    for (int columna = 0; columna < 12; ++columna)
    {
        tabla[fila][columna] = (fila - 1) * (columna - 1);
    }

    tabla[0][fila] = fila - 1;
    tabla[fila][0] = fila - 1;
}

**tabla = 0;

But this solution is very boring, we better make it generic with templates:

template <int NUMEROS>
struct tabla
{
    static constexpr int TAMANYO = NUMEROS + 2;
    int valores[TAMANYO][TAMANYO]{};

    constexpr tabla()
    {
        // Empezamos en 2, no en 0. Ver comentarios.
        for (int fila = 2; fila < TAMANYO; ++fila)
        {
            // Empezamos en 2, no en 0. Ver comentarios.
            for (int columna = 2; columna < TAMANYO; ++columna)
            {
                valores[fila][columna] = (fila - 1) * (columna - 1);
            }

            valores[0][fila] = fila - 1;
            valores[fila][0] = fila - 1;
        }
    }
};

If we add an injection operator in data flow:

template <int NUMEROS>
std::ostream &operator <<(std::ostream &o, const tabla<NUMEROS> &t)
{
    for (const auto &x : t.valores)
    {
        for (const auto &y : x)
            o << y << '\t';
        o << '\n';
    }

    return o;
}

We can do this:

std::cout << tabla<10>();

What will it show:

0   0   1   2   3   4   5   6   7   8   9  10 
0   0   0   0   0   0   0   0   0   0   0   0 
1   0   1   2   3   4   5   6   7   8   9  10 
2   0   2   4   6   8  10  12  14  16  18  20 
3   0   3   6   9  12  15  18  21  24  27  30 
4   0   4   8  12  16  20  24  28  32  36  40 
5   0   5  10  15  20  25  30  35  40  45  50 
6   0   6  12  18  24  30  36  42  48  54  60 
7   0   7  14  21  28  35  42  49  56  63  70 
8   0   8  16  24  32  40  48  56  64  72  80 
9   0   9  18  27  36  45  54  63  72  81  90 
10  0  10  20  30  40  50  60  70  80  90 100 

You can see the code working in Wandbox .

    
answered by 13.07.2017 в 17:35
0

Notice that the table you post has two 0's in both headings (rows / columns) while your code only outputs 1. Your code should rather start like this:

// inicializas la esquina superior izquierda
A[0][0] = 0;

And then you have to correct the loops:

// MAL
for(c=0;c<11;c++)
{
    A[0][c]=c;    
}

// Bien
for(c=1;c<12;c++)
{
    A[0][c]=c-1;    
}

Notice that I am changing both the path of the loop and the calculation of the assigned value. You are working with a 12x12 matrix, then the loop must iterate from 1 (because 0 is for the header) to 11 (included). With the rest of the loops something similar happens.

Now, you can also choose not to complicate your life so much:

A[0][0] = 0;

// Las cabeceras son simétricas
for( int i=0; i<=10; i++)
{
  A[0][i+1] = i;
  A[i+1][0] = i;
}

// Rellenamos la tabla
for( int i=0; i<=10; i++ )
{
  for( int j=0; j<=10; j++)
    A[i+1][j+1] = i*j;
}

And the presentation can be easily improved with the iomanip library:

for(f=0;f<12;f++)
{
  for(c=0;c<12;c++)
  {
    cout<<setw(3)<<A[f][c]<<"  ";
  }
  cout<<endl<<endl;
}
    
answered by 13.07.2017 в 17:03
0

I do not know if what you're looking for is something like the following.

#include<iostream>
using namespace std;
    main(){
        int T[11][11];
        // fill
        for(int i = 0; i <= 10; i ++){
            for(int j = 0; j<= 10; j ++){
                T[i][j] = i*j;
            }
        }
        //output
        for(int i = 0; i <= 10; i ++){
            for(int j = 0; j<= 10; j ++){
                cout<<T[i][j]<<" ";
            }
            cout<<"\n";
        }
    }
    
answered by 13.07.2017 в 17:12