How it shows the row and column position of a matrix in C and sums the row and column

Asked

Viewed 387 times

-1

Good afternoon Guys!!!! I know the question seems silly, but I’m starting now in programming needed to do a C program with a MATRIX[4][4] as described here below. I’ve been trying for a few days, but without success.
The user will not enter any data is only to show the same described data below neither more nor less.

Create a C PROGRAM that displays the following output:

Row: 0 column: 1 value: 1
Row: 1 column: 1 value: 2
Row: 2 column: 1 value: 3
Row: 3 column: 1 value: 4
Row: 4 column: 1 value: 5

This is the code I made.
The Program runs error-free, but does not print the values of the above example, prints the wrong numbers, does not show the values of the row or column.

Thanks in advance. :).

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int l, c, cont, matriz[4][4];
    char valor = 'x';
    cont = 0;
    for (l = 0; l < 4; l++)
    {
        for (c = 0; c < 2; c++)
        {
            matriz[l][c] = cont + 1;
        }
    }

    for (l = 0; l < 4; l++)
    {
        for (c = 0; c < 2; c++)
        {
            printf("Linha: %d   Coluna: %d  Valor: %s", matriz[l][c], valor);
        }
        printf("\n");
    }
    return (0);
}`
  • If I understand correctly your problem change: printf("Linha: %d Coluna: %d Valor: %s", matriz[l][c], valor); for: printf("Linha: %d Coluna: %d Valor: %d", l, c, matriz[l][c]);.

  • Why create the 4x4 matrix if you only use up the second column? Besides, it does not make sense that you print the letter "x" several times, I think that x should be exchanged for the respective value of that row and column, no? (As suggested by the comment above) - If I were to print the letter "x" several times, I wouldn’t even need this matrix, I would just loop it

  • Thanks for the help, my printf was wrong. Thinking here you are right to display several times the X.

  • I reversed the issue because it changed the question, and that’s bad because it invalidates the answers that have already been given. If you want to ask something else (even if it is related), then ask another question

  • I got it,I registered today and it was my first question here. More beauty, I’ll pay attention to it next time. VLW.

1 answer

0

I managed to add here. I was putting the variable value "which is responsible for adding up the row and column" in the wrong place. It follows the code that worked the sum.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int l, c, cont, matriz[4][4];
    int valor;
    cont = 0;
    valor = 0;
    for (l = 0; l < 4; l++)
    {
        for (c = 0; c < 2; c++)
        {
            matriz[l][c] = cont + 1;

        }
    }

    for (l = 0; l < 4; l++)
    {
        for (c = 0; c < 2; c++)
        {
            valor = valor + matriz[l][c];
            printf("Linha: %d   Coluna: %d  Valor: %d", l, c, valor);
        }
        printf("\n");
    }
    return (0);
}` 

Thank you very much to everyone who helped me. They are silly mistakes, but they go unnoticed so that you start now.

Browser other questions tagged

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