I have to invert a vector in c

0

Good! I'm starting to program in pseudocodigo similar to C and they gave me an exercise that is really costing me a lot ... The slogan is: Invert an array of 30 integers entered by keyboard (I can not use sorting methods such as bubbler, for example) I already create the vector and the load, but the part of inverting them is the one I can not get ... Can someone give me a little help? Thanks!

int v[30]; // creo el vector
int aux; // creo un auxiliar
int cont=0; // creo el contador

while(cont < 30) { //cargo las 30 posiciones del vector
scanf("%d", &aux)
v[cont] = aux
cont++
}
    
asked by Pil 04.11.2017 в 03:13
source

1 answer

0

The simplest alternative I think is that you do a swap operation. For this you need a loop and a temporary variable

int temp;

The operation consists of going element by element through the array (up to half) and making an exchange between the i-th element and the element SIZE - i, using a temporary variable so as not to lose value when doing this exchange .

The program will look like this:

int v[30]; // creo el vector
int aux; // creo un auxiliar
int temp; //variable temporal
int cont=0; // creo el contador

//ENTRADA DE DATOS
while(cont < 30) { //cargo las 30 posiciones del vector
    scanf("%d", &aux)
    v[cont] = aux
    cont++
}

//SWAP
cont = 0;
while(cont < 30/2)
{
    temp = v[cont];
    v[cont] = v[30 - 1 - cont];
    v[30 - 1 - cont] = temp;
    cont++;
}

The temporary variable is necessary because if we do the following:

   v[cont] = v[30 - 1 - cont];
   v[30 - 1 - cont] = v[cont];

We would lose forever the value of v[cont]

    
answered by 04.11.2017 / 03:24
source