Add items to the beginning of a vector without affecting existing values

Asked

Viewed 69 times

0

How to include 3 numbers at the beginning of this vector without affecting the previous numbers?

I’m trying to do it this way:

int main()
{

   int v[30];
   int i,x;

   for(i=0; i < 15; i++)
   {
       printf("Digite 15 numeros");
       scanf("%d", &v[i]);
   }

for(v[i+3] = v[i]; i < 18; i++)
   {
      printf("Digite Mais 3 Numeros");
      scanf("%d", &v[i]);
   }

for(i=0; i < 15; i++)
   {
    printf("%d\n", v[i+3]);
   }

}

But when presenting he takes out the first 3 numbers and puts the 3 new numbers at the end.

  • 2

    Explain your use case better. If you already know right away that you need 18 items, just put the first 15 in position 3 on, and then the others at the beginning.

  • I’m trying to solve the vector exercises, which is asked to create a vector with 30 more numbers that includes only 15 and print them and then add 3 more numbers at the beginning of this vector

3 answers

3


After reading the 15 numbers, you should move them 3 points. And only then read the new 3 numbers.

#include <stdio.h>

int main() {

    int v[30];
    int i,x;

    printf("Digite 15 numeros:\n");
    for(i=0; i < 15; i++) {
        scanf("%d", &v[i]);
    }

    printf("Os  15 valores inseridos:\n");
    for(i=0; i < 15; i++) {
       printf("%d\n", v[i]);
    }

    // Move os valores 3 indices
    for(i = 15; i >= 0; --i) {
        v[i+3] = v[i];
    }

    // Salva os novos valores
    printf("Entre com mais 3 valores:\n");
    for(i = 0; i < 3; i++){
        scanf("%d", &v[i]);
    }

    printf("Os 18 valores inseridos:\n");
    for(i=0; i < 18; i++) {
        printf("%d\n", v[i]);
    }

    return 0;
}
  • Thank You For Your Help, Your Answer Was What I Needed!

2

If you already know right away that you need 18 items, just put the first 15 on index 3 on, and then the others at the beginning.

#include <stdio.h>

int main(void) {

    int v[30];
    int i;

    // Começa na quarta posição (índice 3)
    for(i=3; i<18; i++) {
        printf("Digite 15 numeros");
        scanf("%d", &v[i]);
    }

    // Agora preenche as 3 primeiras
    for(i=0; i<3; i++) {
        printf("Digite mais 3 numeros");
        scanf("%d", &v[i]);
    }

    // Imprime tudo
    for(i=0; i<18; i++) {
        printf("%d\n", v[i]);
    }

    return 0;
}

https://ideone.com/3HaT5X

  • From what I understand, in the statement, he asks to "move" the values of the array of positions 0, 1, 2 ... to positions 2, 3, 4... and not to save directly to these positions. But I may have misunderstood.

  • It may be, of the way he described the statement can be one thing or the other. No doubt the code of your answer is more useful, but I sometimes find it important to post the obvious.

  • I fully agree with the idea of posting the obvious, haha. Really the statement leaves this doubt in the air. Good part is that it learns both ways

  • John is really the way I need him, but he returns the 3 new numbers at the beginning and transforms the others into 1 2 3

1

Using what you said, we should do:

#include <stdio.h>
#define TAMANHO_VETOR 30

int main () {

  int vetor[TAMANHO_VETOR], i;

  // Recebe as primeiras posicoes
  for (i = 0; i < 15; i++) {
    printf ("Digite o numero para a posicao %d: ", i);
    scanf ("%d", &vetor[i]);
  }

  // Move as posicoes para receber os 3 primeiros valores
  for (i = 0; i < 15; i++) {
    vetor[i+3] = vetor[i];
  }

  // Recebe os tres primeiros numeros
  for (i = 0; i < 3; i++) {
    printf ("Digite o numero para a posicao %d: ", i);
    scanf ("%d", &vetor[i]);
  }

  // Imprime o vetor
  for (i = 0; i < 18; i++) {
    printf ("%d\t", vetor[i]);
  }

  return 0;
}
  • 1

    You have to move backwards like Ivanov did. Otherwise you will overwrite values that have not yet moved.

  • Perfect. I didn’t pay attention to it. Thanks.

Browser other questions tagged

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