Reverse position / value of the vector

0

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

  

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

     

inverse [5] = [5,4,3,2,1] < It is necessary that you do so invert the 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

}
    
asked by anonymous 21.06.2017 / 04:07

3 answers

0

I was able to resolve after the @Wtrmute to lighten 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;
}
    
21.06.2017 / 04:54
1

There is 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;
}
    
21.06.2017 / 04:35
0

A copy-copying function follows by inverting the contents of one vector from 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;
}

The inversion of the contents of the vectors can be done in-place , ie without the need for a second vector, follow the 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;
}

Output:

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