Inverting elements of a vector in C

Asked

Viewed 5,734 times

-3

Good afternoon! Personal someone knows how to invert elements of a vector, for example, type 12345 and the program returns 54321 I did it, but it didn’t work! [! [ [1]: https://i.stack.Imgur.com/mVkRM.png][1]][1]

#include <stdio.h>
int main(){
    int vetOriginal[5], vetInvert[5];
    int i, j;

    for(i = 0; i < 5; i ++)
    {
        printf("Digite um número!\n");
        scanf("%d", &vetOriginal[i]);
    }

    for(j = 0; j >= 4; j-- )
    {
        vetInvert[i] = vetOriginal[j];
        printf("O vetor invertido é |\n %d", vetInvert[i]);
    }
    return 0;
}
  • Just change for(i = 0; j <= 4; i++, j--) for for (j = 0; j >= 4; j--)

  • Welcome. Avoid posting code image, prefer to post the code itself in the question. It is easier to simulate, copy and paste, etc. Take a look and take a look here

  • To invert do: for (i=0; i<5; i++) vetInvert[i] = vetOriginal[4-i];

  • @André Esse for that you suggested will never be executed, because if j starts at zero, the condition j >= 4 is false and therefore never enters the loop: https://ideone.com/xCBbTP

5 answers

0

#define LC_CTYPE 2

char* setlocale(int, const char*);
int printf(const char*, ...);
int scanf(const char*, ...);

int _cdecl main(void)
{
    setlocale(LC_CTYPE, "PtB"); // Para escrever em português do Brasil
    printf("Digite cinco números (ex. 1 12 23 34 45): ");

    int x;
    int vetor_original[5] = {0};

    for (x = 0; x < 5; x++)
    {
        if (!scanf("%i", &vetor_original[x])) {
            printf("Erro.\n");
            return 1;
        }
    }

    int y;
    int vetor_invertido[5] = {0};

    for (x = 4, y = 0; x > (-1); x--, y++) {
        vetor_invertido[y] = vetor_original[x];
        printf("%i\n", vetor_invertido[y]);
    }

    return 0;
}

That is, vi[0] = vo[4], ..., vi[4] = vo[0].

If the user does not separate the numbers by space or Enter, will receive error message. The program has been tested and works.

inserir a descrição da imagem aqui

0

include

int main(void){

int x[5];
int i;

printf("\nDigite 5 numeros: \n");
for(i=0;i<5;i++){

scanf("%d", &x[i]); 

}
//Eu coloquei 4 porque em um vetor de 5 posições, a primeira posição é 0. 
for(i=4;i>=0;i--){
    printf("%d", x[i]);
}   

Return 0; }

0

You are always putting in the first position what you want to reverse, without varying the i just the j

#include <stdio.h>
#define N 5
int main(){
    int vetOriginal[N], vetInvert[N];
    int i = 0, j = 0; // recomendo sempre iniciar com 0

    for(i = 0; i < N; i++){
        printf("Digite um número!\n");
        scanf("%d", &vetOriginal[i]);
    }
    i = 0;
    for(j = N-1; j >= 0; j-- ){
        vetInvert[i] = vetOriginal[j];
        i++;
    }
    printf("O vetor invertido é: ");
    for(i = 0; i < N; i++)
        printf("%d", vetInvert[i]);
    printf("\n");
    return 0;
}

If you want, you can put the second go to work within the first, so save 1 for

0

You can control the elements to be reversed with only one index:

#include <stdio.h>
#define N 5
int main() {
    int vetOriginal[N], vetInvert[N];
    int i;
    for(i = 0; i < N; i++) {
        printf("Digite um número!\n");
        scanf("%d", &vetOriginal[i]);
    }
    for(i = 0; i < N; i++) {
        vetInvert[i] = vetOriginal[N-i-1];
        printf("O vetor invertido é |\n %d", vetInvert[i]);
    }
    return 0;
}

If you want to use two variables as indexes control the two indexes in the loop:

#include <stdio.h>
int main() {
    int vetOriginal[5], vetInvert[5];
    int i, j;
    for(i = 0; i < 5; i++) {
        printf("Digite um número!\n");
        scanf("%d", &vetOriginal[i]);
    }

    for(i=0, j = 4; i<5 && j >= 0; i++, j-- ) {
        vetInvert[i] = vetOriginal[j];
        printf("O vetor invertido é |\n %d", vetInvert[i]);
    }
    return 0;
}

Note that in your solution the value contained in the variable i after the initial loop is 5 and you move to this same position, outside the area declared for the vector ranging from 0 to 4, all elements of the original vector.

0

In case you would have to reverse this way:

    #include <stdio.h>
    int main()
    {
      int vetor[5],vetorInv[5],i,n=5;
      printf("Digite 5 valores para um vetor: \n");
      for(i=0;i<5;i++)
        scanf("%d",vetor[i]);
      for(i=0;i<5;i++)
        printf("vetor[%d] = %d \n",i,vetor[i]);
      for(i=0;i<5;i++)
      {
        vetorInv[i]=vetor[n-1];
        n--;
      }
      printf("\n\n**Vetor Invertido**\n");
      for(i=0;i<5;i++)
        printf("vetor[%d] = %d \n",i,vetorInv[i]);
      return 0;
    }
  • Got it, gave it right! thank you very much!

Browser other questions tagged

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