Problem in assembling a C matrix

Asked

Viewed 96 times

1

I have a problem with a matrix that I set up to be typed letters and these transformed into ascii table numbers, but it doesn’t print Where am I wrong? Someone can help me?

#include <stdio.h>
#include <stdlib.h>
int main(){
 char i, j, m[3][3];
 char tecla[0];


 //captura os elementos
 for(i=0;i<3;i++)
 for(j=0;j<3;j++){
 printf("Elemento[%d][%d]= ",i,j);
 tecla[0] = getche();
 scanf("%c",&m[i][j]);
 }
 //EXIBIR VALORES ORIGINAIS
 printf("\n::: Valores Originais :::\n");
 for(i=0;i<3;i++){
 for(j=0;j<3;j++)
 printf("%c ",m[i][j]);
 tecla[0] = getche();
 printf("\n");
 }
  • 1

    Are you sure you want to create a zero-size vector?

  • 1

    Watch the { and } of these ties for. Seems to be wrong.

  • I wanted the typed letters to be displayed at the end, and that doesn’t happen, even with the change suggested by you

  • My code only shows the last letter typed

  • 2

    @Williamhenrique Do not "correct" the identation of questions where identation problems are part of the question problem!

  • @Victorstafusa blz mano! thanks for the touch!

Show 1 more comment

1 answer

3

I think what you wanted was this:

#include <stdio.h>
#include <stdlib.h>
int main() {
    char i, j, m[3][3];
    char tecla;

    //captura os elementos
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("Elemento[%d][%d] = ", i, j);
            tecla = getche();
            scanf("%c", &m[i][j]);
         }
     }

     //EXIBIR VALORES ORIGINAIS
     printf("\n::: Valores Originais :::\n");
     for (i = 0; i < 3; i++) {
         for (j = 0; j < 3; j++) {
             printf("%c ", m[i][j]);
         }
         printf("\n");
     }
     tecla = getche();
}

Always use the { and the } to avoid making mistakes. For example, in this code you posted:

 //EXIBIR VALORES ORIGINAIS
 printf("\n::: Valores Originais :::\n");
 for(i=0;i<3;i++){
 for(j=0;j<3;j++)
 printf("%c ",m[i][j]);
 tecla[0] = getche();
 printf("\n");
 }

Note that the for from within has not the {. This makes the tecla[0] = getche(); and the printf("\n"); are out of the for internal. If you always use the { in all ties for, you won’t have this problem. Ideating everything properly will also help you a lot.

Moreover, char tecla[0]; declares an array of zero positions and you write at its first position (position 0), which is already outside the array. This doesn’t make sense, it’s easier to use just a common variable char tecla;.

  • Unfortunately it was I who indented lol mal, much due to the confused way as there were { and } missing. I will find a way, but it does not invalidate the very advised use of { } clear-cut.

  • @Isac It’s because of situations like this that I created this: https://pt.meta.stackoverflow.com/q/6300/132

  • I do understand your point of view, but it’s pretty hard to read all the code running with no indentation, so I’ve indented it. In most cases it does not affect interpretation but in this one it did. We can always go back in the edits and go back to the unnamed state and reapply the tag setting

  • @Isac Yes, of course. But I’m always extremely conservative about editing the question code because I’ve seen countless cases where right identar is part of the answer.

  • 1

    Yes I agree. I will take the advice and be much more conservative when it comes to code edits.

Browser other questions tagged

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