How to place multiple ordered outputs in a table

Asked

Viewed 42 times

0

I need help to improve visualization, can anyone help me ? I’m using C

#include <stdio.h>

int main() {
    int tab, mult, result;

    printf("Tabuada de 1 a 9.\n");
    tab = 1;
    for (tab = 1; tab <= 9; tab++) {
        mult = 0;
        while (mult <= 9 && tab <= 9) {
            mult++;
            result = tab * mult;
            printf("|%d * %d = %d |", tab, mult, result);
        }
        printf("\n");
    }
    return 0;
}

2 answers

3

A possible view

#include <stdio.h>

int main() {
    int tab, mult, result;

    printf("Tabuada de 1 a 9.\n");
    tab = 1;
    for (tab = 1; tab <= 9; tab++) {
        mult = 0;
        while (mult <= 9 && tab <= 9) {
            mult++;
            result = tab * mult;            
            printf("%d * %d = %d \n", tab, mult, result);
        }
        printf("\n");
    }
    return 0;
}
  • You didn’t even report the change you made. It is not very useful to give the code to work if the person does not know what he has changed to make it work.

0

You can implement a function capable of printing your table with a parameterized amount of columns, see only:

void exibir_tabuada( int ncolunas )
{
    int i = 0;
    int j = 0;
    int coluna = 0;
    int linha = 0;

    for( i = 0; i < 10; i += ncolunas, j += ncolunas )
    {
        for( coluna = 0; coluna < ncolunas; coluna++ )
            printf("+------------");
        printf("+\n");

        for( linha = 0; linha < 10; linha++ )
        {
            for( coluna = 0; coluna < ncolunas; coluna++ )
                if( coluna + j <= 9 )
                    printf("| %d x %d = %2d ", coluna + j, linha, (coluna + j) * linha );
                else
                    printf("|            ");
            printf("|\n");
        }
    }

    for( coluna = 0; coluna < ncolunas; coluna++ )
        printf("+------------");
    printf("+\n");
}

Exit (ncolunas=5):

+------------+------------+------------+------------+------------+
| 0 x 0 =  0 | 1 x 0 =  0 | 2 x 0 =  0 | 3 x 0 =  0 | 4 x 0 =  0 |
| 0 x 1 =  0 | 1 x 1 =  1 | 2 x 1 =  2 | 3 x 1 =  3 | 4 x 1 =  4 |
| 0 x 2 =  0 | 1 x 2 =  2 | 2 x 2 =  4 | 3 x 2 =  6 | 4 x 2 =  8 |
| 0 x 3 =  0 | 1 x 3 =  3 | 2 x 3 =  6 | 3 x 3 =  9 | 4 x 3 = 12 |
| 0 x 4 =  0 | 1 x 4 =  4 | 2 x 4 =  8 | 3 x 4 = 12 | 4 x 4 = 16 |
| 0 x 5 =  0 | 1 x 5 =  5 | 2 x 5 = 10 | 3 x 5 = 15 | 4 x 5 = 20 |
| 0 x 6 =  0 | 1 x 6 =  6 | 2 x 6 = 12 | 3 x 6 = 18 | 4 x 6 = 24 |
| 0 x 7 =  0 | 1 x 7 =  7 | 2 x 7 = 14 | 3 x 7 = 21 | 4 x 7 = 28 |
| 0 x 8 =  0 | 1 x 8 =  8 | 2 x 8 = 16 | 3 x 8 = 24 | 4 x 8 = 32 |
| 0 x 9 =  0 | 1 x 9 =  9 | 2 x 9 = 18 | 3 x 9 = 27 | 4 x 9 = 36 |
+------------+------------+------------+------------+------------+
| 5 x 0 =  0 | 6 x 0 =  0 | 7 x 0 =  0 | 8 x 0 =  0 | 9 x 0 =  0 |
| 5 x 1 =  5 | 6 x 1 =  6 | 7 x 1 =  7 | 8 x 1 =  8 | 9 x 1 =  9 |
| 5 x 2 = 10 | 6 x 2 = 12 | 7 x 2 = 14 | 8 x 2 = 16 | 9 x 2 = 18 |
| 5 x 3 = 15 | 6 x 3 = 18 | 7 x 3 = 21 | 8 x 3 = 24 | 9 x 3 = 27 |
| 5 x 4 = 20 | 6 x 4 = 24 | 7 x 4 = 28 | 8 x 4 = 32 | 9 x 4 = 36 |
| 5 x 5 = 25 | 6 x 5 = 30 | 7 x 5 = 35 | 8 x 5 = 40 | 9 x 5 = 45 |
| 5 x 6 = 30 | 6 x 6 = 36 | 7 x 6 = 42 | 8 x 6 = 48 | 9 x 6 = 54 |
| 5 x 7 = 35 | 6 x 7 = 42 | 7 x 7 = 49 | 8 x 7 = 56 | 9 x 7 = 63 |
| 5 x 8 = 40 | 6 x 8 = 48 | 7 x 8 = 56 | 8 x 8 = 64 | 9 x 8 = 72 |
| 5 x 9 = 45 | 6 x 9 = 54 | 7 x 9 = 63 | 8 x 9 = 72 | 9 x 9 = 81 |
+------------+------------+------------+------------+------------+

Exit (ncolunas = 3):

+------------+------------+------------+
| 0 x 0 =  0 | 1 x 0 =  0 | 2 x 0 =  0 |
| 0 x 1 =  0 | 1 x 1 =  1 | 2 x 1 =  2 |
| 0 x 2 =  0 | 1 x 2 =  2 | 2 x 2 =  4 |
| 0 x 3 =  0 | 1 x 3 =  3 | 2 x 3 =  6 |
| 0 x 4 =  0 | 1 x 4 =  4 | 2 x 4 =  8 |
| 0 x 5 =  0 | 1 x 5 =  5 | 2 x 5 = 10 |
| 0 x 6 =  0 | 1 x 6 =  6 | 2 x 6 = 12 |
| 0 x 7 =  0 | 1 x 7 =  7 | 2 x 7 = 14 |
| 0 x 8 =  0 | 1 x 8 =  8 | 2 x 8 = 16 |
| 0 x 9 =  0 | 1 x 9 =  9 | 2 x 9 = 18 |
+------------+------------+------------+
| 3 x 0 =  0 | 4 x 0 =  0 | 5 x 0 =  0 |
| 3 x 1 =  3 | 4 x 1 =  4 | 5 x 1 =  5 |
| 3 x 2 =  6 | 4 x 2 =  8 | 5 x 2 = 10 |
| 3 x 3 =  9 | 4 x 3 = 12 | 5 x 3 = 15 |
| 3 x 4 = 12 | 4 x 4 = 16 | 5 x 4 = 20 |
| 3 x 5 = 15 | 4 x 5 = 20 | 5 x 5 = 25 |
| 3 x 6 = 18 | 4 x 6 = 24 | 5 x 6 = 30 |
| 3 x 7 = 21 | 4 x 7 = 28 | 5 x 7 = 35 |
| 3 x 8 = 24 | 4 x 8 = 32 | 5 x 8 = 40 |
| 3 x 9 = 27 | 4 x 9 = 36 | 5 x 9 = 45 |
+------------+------------+------------+
| 6 x 0 =  0 | 7 x 0 =  0 | 8 x 0 =  0 |
| 6 x 1 =  6 | 7 x 1 =  7 | 8 x 1 =  8 |
| 6 x 2 = 12 | 7 x 2 = 14 | 8 x 2 = 16 |
| 6 x 3 = 18 | 7 x 3 = 21 | 8 x 3 = 24 |
| 6 x 4 = 24 | 7 x 4 = 28 | 8 x 4 = 32 |
| 6 x 5 = 30 | 7 x 5 = 35 | 8 x 5 = 40 |
| 6 x 6 = 36 | 7 x 6 = 42 | 8 x 6 = 48 |
| 6 x 7 = 42 | 7 x 7 = 49 | 8 x 7 = 56 |
| 6 x 8 = 48 | 7 x 8 = 56 | 8 x 8 = 64 |
| 6 x 9 = 54 | 7 x 9 = 63 | 8 x 9 = 72 |
+------------+------------+------------+
| 9 x 0 =  0 |            |            |
| 9 x 1 =  9 |            |            |
| 9 x 2 = 18 |            |            |
| 9 x 3 = 27 |            |            |
| 9 x 4 = 36 |            |            |
| 9 x 5 = 45 |            |            |
| 9 x 6 = 54 |            |            |
| 9 x 7 = 63 |            |            |
| 9 x 8 = 72 |            |            |
| 9 x 9 = 81 |            |            |
+------------+------------+------------+

Exit (ncolunas = 10):

+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| 0 x 0 =  0 | 1 x 0 =  0 | 2 x 0 =  0 | 3 x 0 =  0 | 4 x 0 =  0 | 5 x 0 =  0 | 6 x 0 =  0 | 7 x 0 =  0 | 8 x 0 =  0 | 9 x 0 =  0 |
| 0 x 1 =  0 | 1 x 1 =  1 | 2 x 1 =  2 | 3 x 1 =  3 | 4 x 1 =  4 | 5 x 1 =  5 | 6 x 1 =  6 | 7 x 1 =  7 | 8 x 1 =  8 | 9 x 1 =  9 |
| 0 x 2 =  0 | 1 x 2 =  2 | 2 x 2 =  4 | 3 x 2 =  6 | 4 x 2 =  8 | 5 x 2 = 10 | 6 x 2 = 12 | 7 x 2 = 14 | 8 x 2 = 16 | 9 x 2 = 18 |
| 0 x 3 =  0 | 1 x 3 =  3 | 2 x 3 =  6 | 3 x 3 =  9 | 4 x 3 = 12 | 5 x 3 = 15 | 6 x 3 = 18 | 7 x 3 = 21 | 8 x 3 = 24 | 9 x 3 = 27 |
| 0 x 4 =  0 | 1 x 4 =  4 | 2 x 4 =  8 | 3 x 4 = 12 | 4 x 4 = 16 | 5 x 4 = 20 | 6 x 4 = 24 | 7 x 4 = 28 | 8 x 4 = 32 | 9 x 4 = 36 |
| 0 x 5 =  0 | 1 x 5 =  5 | 2 x 5 = 10 | 3 x 5 = 15 | 4 x 5 = 20 | 5 x 5 = 25 | 6 x 5 = 30 | 7 x 5 = 35 | 8 x 5 = 40 | 9 x 5 = 45 |
| 0 x 6 =  0 | 1 x 6 =  6 | 2 x 6 = 12 | 3 x 6 = 18 | 4 x 6 = 24 | 5 x 6 = 30 | 6 x 6 = 36 | 7 x 6 = 42 | 8 x 6 = 48 | 9 x 6 = 54 |
| 0 x 7 =  0 | 1 x 7 =  7 | 2 x 7 = 14 | 3 x 7 = 21 | 4 x 7 = 28 | 5 x 7 = 35 | 6 x 7 = 42 | 7 x 7 = 49 | 8 x 7 = 56 | 9 x 7 = 63 |
| 0 x 8 =  0 | 1 x 8 =  8 | 2 x 8 = 16 | 3 x 8 = 24 | 4 x 8 = 32 | 5 x 8 = 40 | 6 x 8 = 48 | 7 x 8 = 56 | 8 x 8 = 64 | 9 x 8 = 72 |
| 0 x 9 =  0 | 1 x 9 =  9 | 2 x 9 = 18 | 3 x 9 = 27 | 4 x 9 = 36 | 5 x 9 = 45 | 6 x 9 = 54 | 7 x 9 = 63 | 8 x 9 = 72 | 9 x 9 = 81 |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+

See working on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.