Store the positives in one vector and the negatives in another

3
  

Make an algorithm that reads a set of 30 integer numeric values and distributes them between two vectors, separating the positive numbers from the negative ones. The vectors must have 30 positions each. Show the vectors at the end of processing.

int vetPos[3], vetNeg[3];
int valor, i, cont;
cont = 1;

do{
    printf("Informe um valor: ");
    scanf("%d",&valor);

    if(valor >= 0){
        //insere no vetor positivo
        for(i=0;i<3;i++){
            vetPos[i] = valor;
        }            
    }else if(valor < 0){
        //insere no vetor negativo
        for(i=0;i<3;i++){
            vetNeg[i] = valor;
        }
    }
    valor = 0;
    cont++;
}while(cont<=3);


//saida de dados
printf("Os números positivos digitados foram: ");
for(i=0;i<3;i++){
    printf("%3d",vetPos[i]);
}
printf("\nOs números negativos digitados foram: ");
for(i=0;i<3;i++){
    printf("%3d",vetNeg[i]);
}

No do..while made up to 3 just for testing.

In the compiler the result was this:

I understand that this error occurs because it needs something like a delimiter for the vector, but I'm not sure if that's all, what can I do?

    
asked by anonymous 28.07.2018 / 22:59

1 answer

4

It's much simpler than this:

#include <stdio.h>

int main(void) {
    int pos[3], neg[3], posCount = 0, negCount = 0;
    for (int i = 0; i < 3; i++) {
        int valor;
        printf("Informe um valor: ");
        scanf("%d", &valor);
        if (valor < 0) neg[negCount++] = valor;
        else pos[posCount++] = valor;
    }
    printf("Os números positivos digitados foram: ");
    for (int i = 0; i < posCount; i++) printf("%d ", pos[i]);
    printf("\nOs números negativos digitados foram: ");
    for (int i = 0; i < negCount; i++) printf("%d ", neg[i]);
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

It makes no sense to have ties to fill the vector, which will make all positions have the same value and only the last value entered. It also does not make sense to have else if when it is exactly the opposite of if . You have too variable and missing variables to control how many elements were inserted into each vector.

    
28.07.2018 / 23:19