Reverse vector position/value

Asked

Viewed 7,433 times

0

I need to reverse one vector and store in another one this way:

vector[5] = [1,2,3,4,5] < I will pass the values like this

inverse[5] = [5,4,3,2,1] < And accurate to invert values and position.

My code:

#include <stdio.h>
#include <stdlib.h>

int main(){
  
  int cc[5], inverso[5]; //conta corrente
  int i=0, k=5;
  
  for(i; i < 5; i++){
    printf("Numero da conta corrente: ", i); // Inserir valores
    scanf(" %d", &cc[i]);
  }
  
  for(i=0; i < 5; i++){
    for(k; k >= 0; k--){
      if(inverso[i] == 0){
        inverso[i] = cc[k-1]; //inverter o vetor e armazenar, -1 pois o ultimo numero armazenado está na posição 4 do vetor: cc[].
      }else{
        inverso[i] = cc[k];
      }
    }
  }
  
  for(i=0; i < 5; i++)
    printf("%d", cc[i]); // Ver os valores armazenados
  
  
  printf("\n");
  
  for(i=0; i < 5; i++)
    printf("%d", inverso[i]); //Verificar se for invertido
  
}

4 answers

1

There’s absolutely no reason for you to use two for to scan a one-dimensional vector...

#include <stdio.h>
#include <stdlib.h>
#define TAM_VET 5

int
main(void) {

    int cc[TAM_VET], inverso[TAM_VET];
    int i = 0;

    /* ler valores da entrada padrão */
    for (; i < TAM_VET; i ++) {
        printf("Numero da %dª conta corrente: ", i + 1);
        scanf(" %d", &cc[i]);
    }

    /* gerar inverso a partir de cc */
    for (i=0; i < TAM_VET; i ++) {
        inverso[i] = cc[TAM_VET - i];
    }

    /* imprimir o vetor original */
    for (i = 0; i < TAM_VET; i ++) {
        printf("%d ", cc[i]); // Ver os valores armazenados
    }
    printf("\n");

    /* imprimir o vetor invertido */
    for (i = 0; i < TAM_VET; i ++)
        printf("%d ", inverso[i]); //Verificar se for invertido

    /* lembrar de retornar 0 para sinalizar execução bem-sucedida */
    return 0;
}
  • Dude, I used two for() because 1 is to traverse the 1st vector of 0:5 and another is to traverse the vector of 5:0, because I need to put in 1 position the value of the last position of the other vector, in case the code that you posted me the result: ex: vector 1: 12345 --- reversed: 55555

  • 1

    Argh, error of typing: [TAM_VET - i], nay [TAM_VET - 1]. Edited the answer.

  • I tested again, still error, I put: 1 2 3 4 5 - returns: 32765 5 4 3 2

0

A different solution

#include <stdio.h>
#include <stdlib.h>

//Escreva uma funcão (void) que recebe um vetor v e seu tamanho inteiro N
//e inverte a ordem de seus elementos.

void InverteVetor(int v[], int N);

int main(void)
{
      int i=0, N=5;
      int vetor[]={1,2,3,4,5};
      printf("\n\n**Vetor**\n");
      for(i=0; i<N; i++)printf("vetor[%d] = %d \n",i,vetor[i]);
      InverteVetor(vetor,N); //chamada da função pra inverter o vetor
      printf("\n\n**Vetor Invertido**\n");
      for(i=0; i<N; i++)printf("vetor[%d] = %d \n",i,vetor[i]);
      return 0;
}

void InverteVetor(int v[], int N)
{
    int i=0, aux=0, n=N;

    for(i=0; i<N/2; i++)
    {
        aux=v[i];
        v[i]=v[n-1];
        v[n-1]=aux;
        n--;
    }
}

0

Follows a copy capable function by reversing the contents of one array of integers to another:

#include <stdio.h>
#include <string.h>


#define sizeof_vector(v) (sizeof(v) / sizeof(v[0]))


void inverter( int invertido[], int original[], int tam )
{
    int i = 0;

    for( i = 0; i < tam; i++ )
        invertido[tam - i - 1] = original[i];
}


void exibir( int vetor[], int tam )
{
    int i = 0;

    for( i = 0; i < tam; i++ )
        printf("%s%d", (i>0)?", ":"", vetor[i] );

    printf("\n");
}


int main( int argc, char ** argv )
{
    int original[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int invertido[10];

    inverter( invertido, original, sizeof_vector(original) );

    printf("Original: ");
    exibir( original, sizeof_vector(original) );

    printf("Invertido: ");
    exibir( invertido, sizeof_vector(invertido) );

    return 0;
}

Inversion of vector content can be done in-place, that is, without the need for a second vector, follows example:

#include <stdio.h>
#include <string.h>


#define swap(a,b)        do{ int tmp = a; a = b; b = tmp; } while(0)
#define sizeof_vector(v) (sizeof(v) / sizeof(v[0]))


void inverter( int vetor[], int tam )
{
    int i = 0;

    for( i = 0; i < (tam / 2); i++ )
        swap( vetor[i], vetor[tam - i - 1]);
}


void exibir( int vetor[], int tam )
{
    int i = 0;

    for( i = 0; i < tam; i++ )
        printf("%s%d", (i>0)?", ":"", vetor[i] );

    printf("\n");
}


int main( int argc, char ** argv )
{
    int vetor[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    printf("Original: ");
    exibir( vetor, sizeof_vector(vetor) );

    inverter( vetor, sizeof_vector(vetor) );

    printf("Invertido: ");
    exibir( vetor, sizeof_vector(vetor) );

    return 0;
}

Exit:

./inverter
Original: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Invertido: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

0


I managed to resolve after the @Wtrmute give a light in my thoughts, thank you.

#include <stdio.h>
#include <stdlib.h>

int main(){

  int cc[5], inverso[5]; //conta corrente
  int i=0, k=0;

  for(i; i < 5; i++){
    printf("Numero da conta corrente: ", i); // Inserir valores
    scanf(" %d", &cc[i]);
  }

  for(i=4; i >= 0; i--){
    inverso[k] = cc[i]; // da ultima posicao até a 4:0
    k++; // troca as posicoes 0:4
  }

  for(i=0; i < 5; i++)
    printf("%d", cc[i]); // Ver os valores armazenados


  printf("\n");

  for(i=0; i < 5; i++)
    printf("%d", inverso[i]); //Verificar se for invertido


  return 0;
}

Browser other questions tagged

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