Copia de Vetores

Asked

Viewed 583 times

1

good night! I have a question about how to copy a vector.

I declared a "matrix [a][j]" vector and I need to take the arithmetic mean only from the values contained in "matrix[a]", so I need to make a copy only of the values "[a]" for an auxiliary vector, and I’m not getting it. I created a function that copies the matrix to another, but this giving error because the main vector is different from the auxiliary.

Follows the code of the copy function:

void copiaMatriz (int matriz[], int vetorAux[]){

int count;
for(count = 0; count < LINHA; count++)
    vetorAux[count] = matriz[count]; }

How should I declare the main matrix for it to copy only the elements contained in "[a]"?

Thank you.

  • Why do you think you need to make a copy? I’m not seeing the general context, but it seems to be totally unnecessary to do this, even if you have the problem may be somewhere else.

  • you can use the memcpy to make a copy of your vector, even though it seems unnecessary in this context.

1 answer

0

hello, I believe you want to do the arithmetic media of a row of your matrix using an auxiliary vector, so you should declare the original matrix with the two dimensions[i][j].

void copiaMatriz (int matriz[][colunas], int vetorAux[]){ /* pode-se usar um #define para as "colunas"*/

/* linha = linha da matriz em que se deseja copiar */

int i;
for(i = 0; i < linha; i++){
vetorAux[i] = matriz[linha][i];

or, if you prefer, you can average directly:

float media_matriz(int n, float mat[]["n"]) {  /* lembrando que não se deve utilizar a variavel diretamente na matriz sem antes defini-la, por isso as aspas */
/* n = numero de colunas */
float media;
int i;
    for(i=0;i<n;i++) 
    {
            media = media + mat["linhaDesejada"][i];
    }
    media = media/("Colunas");

return media;
}

Browser other questions tagged

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