Store some elements of an array in another array

Asked

Viewed 1,491 times

4

I have defined a array integer in which it will be pointed by a pointer and then I have another array which will store only a few numbers in which are even numbers, here is the code...

void main(void) {

    srand(time(NULL));
    //vetor de dimensao 10
    int vetor [MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    //apontador para vetor
    int *ptr_vetor;
    //novo vetor
    int novo_vetor[MAX];
    //apontador para novo vetor
    int *ptr_novo_vetor;
    //escolher numero
    int escolha = 0;
    //contador
    int contador = 0;

    //ponteiro apontado ao primeiro vetor
    ptr_vetor = &vetor[0];
}

shows the values of the vector

printf("\n--- Vetor ---");
    for (int i = 0; i < MAX; i++) {
        //mostra os valores do vetor
        printf("\nvalor : %d ", vetor[i]);
    }

shows even values pointing to the vector

printf("\n\n--- Ponteiro Vetor para Numero Pares ---");
    for (int i = 0; i < MAX; i++) {
        //escolha apenas numeros pares
        if (*(ptr_vetor + i) % 2 == 0) {
            //mostra os valores pares apontador ao vetor
            printf("\nNumero Par : %d ", *(ptr_vetor + i));
            //adiciona o numero par ao novo vetor
            novo_vetor[i] = *(ptr_vetor + i);
            //aqui devia receber apenas os dados do novo vetor mas
            //mostra aepnas o endereço
            ptr_novo_vetor = &novo_vetor[i];
            //usei isto para iterar os jogadores, nao deu certo
            //contador++;
        }
    }

shows the values of the new vector

printf("\n\n\n--- Novo Vetor ---");
    for (int i = 0; i < MAX; i++) {
        //mostra os valores do novo vetor
        printf("\nvalor: %d ", novo_vetor[i]);
    }

shows the values of the new pointer

printf("\n\n--- Ponteiro Novo Vetor ---");
    for (int i = 0; i < MAX; i++) {
        //mostra os valores do novo ponteiro
        printf("\nNovo Vetor : %d ", *(ptr_novo_vetor+i));
    }

when running the program

Resutado

How to show the data that has been added to the new array, that is, even numbers?

  • 1

    Why do you create a pointer that points to the vector? the vector is already a pointer. See, *pt equals pt[n];

  • 1

    Your problem is here novo_vetor[i] = *(ptr_vetor + i); create a counter for the new vector

2 answers

3


I’ll give you a solution that I don’t know if it’s what you want. But the code doesn’t make much sense either, so I don’t know if it’ll make a difference. You probably want to do something else and are far from the goal. I am answering for what you have given to understand.

Surely you have better ways of doing this, but I haven’t spent much time thinking about what to improve, because I see no reason for this whole code to exist.

#include <stdio.h>
#define MAX 10

int main(void) {

    //vetor de dimensao 10
    int vetor [MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    //apontador para vetor
    int *ptr_vetor;
    //novo vetor
    int novo_vetor[MAX];
    //apontador para novo vetor
    int *ptr_novo_vetor;
    //escolher numero
//    int escolha = 0;
    //contador
//    int contador = 0;

    //ponteiro apontado ao primeiro vetor
    ptr_vetor = &vetor[0];

    printf("\n--- Vetor ---");
    for (int i = 0; i < MAX; i++) {
        //mostra os valores do vetor
        printf("\nvalor : %d ", vetor[i]);
    }
    
    printf("\n\n--- Ponteiro Vetor para Numero Pares ---");
    for (int i = 0; i < MAX; i++) {
        //escolha apenas numeros pares
        if (*(ptr_vetor + i) % 2 == 0) {
            //mostra os valores pares apontador ao vetor
            printf("\nNumero Par : %d ", *(ptr_vetor + i));
            //adiciona o numero par ao novo vetor
            novo_vetor[i / 2] = *(ptr_vetor + i);
        }
    }
    ptr_novo_vetor = novo_vetor;

    printf("\n\n\n--- Novo Vetor ---");
    for (int i = 0; i < MAX / 2; i++) {
        //mostra os valores do novo vetor
        printf("\nvalor: %d ", novo_vetor[i]);
    }

    printf("\n\n--- Ponteiro Novo Vetor ---");
    for (int i = 0; i < MAX / 2; i++) {
        //mostra os valores do novo ponteiro
        printf("\nNovo Vetor : %d ", *(ptr_novo_vetor+i));
    }
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • excellent solution, thanks...

  • I tested for vector with different size and it worked, thank you

  • I have another question regarding this same question, that instead of using integer array, one could use struct array

  • Yes, I just don’t know if I should. This code already has too many weird things, it would get weirder. It seems to have no objective.

  • not for this code, this code only served as a basis for doubt of a work in which simulates a football match and in the end if there is a draw, would go to the prolongation, ie, these players will be added in another field extension array and and I already have that list more when I access, appears all players, as had in the previous doubt of integer array

  • in relation to this same question, that instead of using integer array, one could use struct array see link

Show 1 more comment

2

Its resulting vector has the wrong size. If it has to receive only even numbers, it cannot be the same size as the original vector. If you do this, you will have to fill the resulting vector with values that indicate empty. Better would be you, already knowing the input set, simply create a vector of 5 positions.

The reason you are skipping positions in the insert is that you are inserting when i is pair and placing in position i. Thus, only even positions will be filled.

Already the ptr_novo_vetor is showing these weird values because you never made him point anywhere.

  • because it is my doubt was really that, only I tried to implement an accountant and even so, it did not work

Browser other questions tagged

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