Problem with changing positions in vector

-2

Good morning!

Using C ++ Dev, I'm doing the following problem in C:

  

Make a program in C that reads a vector of 20 real-type positions. Change 1st place with 11th, 2nd with 12th, 3rd with 13th, ... 10th with 20th.   the modified vector.

This is my code that is not working:

#include<stdio.h>
#include<conio.c>

main () {
    float vet[20], aux;
    int i;

    clrscr();

//entrada dos dados
printf("Favor informar 20 valores: ");
for(i=0;i<20;i++) {
    gotoxy(i*3+2,8);
    scanf("%f", &vet[i]);
}
gotoxy(5,20);

//troca dos vetores
for(i=0;i<20;i++) {
    aux=vet[i];
    vet[i]=vet[19-i];
    vet[19-i]=aux;
}

//saida dos dados
gotoxy(2,12);
printf("Vetor modificado: ");
for(i=0;i<20;i++) {
    gotoxy(i*3+2,14);
    printf("%i", vet[i]);
}

getch();
}

I can not identify the error. What can it be?

    
asked by anonymous 14.08.2017 / 17:57

1 answer

0

The error is in the loop where you change the values. You are exchanging the 1st value with the 20 °, the 2nd with the 19th and so on. The code below does what is requested.

//troca dos valores
for(i=0;i<10;i++) {
    aux=vet[i];
    vet[i]=vet[10+i];
    vet[10+i]=aux;
}
    
14.08.2017 / 18:13