Construct an array through a vector

2

I put nine digits in the vector but my array comes out with random numbers, I would like my array to come out with the numbers that are in the vector below below my code:

#include <stdio.h>
#include <locale.h>

int vetor [9];
int matriz [3][3];
int i=0, lin=0, col=0, k=0;

main(){
    setlocale(LC_ALL, "");
    printf("Digite 9 número para uma matriz \n" );
    for(i=0;i<9;i++){
        scanf("%i", &vetor[i]);
    }
    for(lin=0;lin<3;lin++){
        for(col=0;col<3;col++){
            matriz[lin][col] = vetor[k];
            k++;
            printf("%i\t", &matriz[lin][col]);
        }printf("\n");
}
}

What am I doing wrong?

    
asked by anonymous 07.06.2016 / 23:39

1 answer

4

The main reason for the problem is that you are getting the address of the array to print. So to solve, just get the & operator in the printf() argument. The operator is required in scanf() just to pass a reference, in printing this is not necessary, then the parameter is not expecting one. I gave a general improvement, but I can avoid the nested loop too, I preferred not to mess too much to make it difficult:

#include <stdio.h>
int main() {
    int vetor [9];
    int matriz [3][3];
    printf("Digite 9 número para uma matriz \n" );
    for (int i = 0; i < 9; i++) {
        scanf("%i", &vetor[i]);
    }
    for (int lin = 0, k = 0; lin < 3; lin++) {
        for (int col = 0; col < 3; col++, k++) {
            matriz[lin][col] = vetor[k];
            printf("%i\t", matriz[lin][col]);
        }
        printf("\n");
    }
}

See working on ideone and on CodingGround .

    
08.06.2016 / 00:05