Vectors - Separate negative and positive

Asked

Viewed 1,154 times

2

I have to make a program that receives a vector of 8 numbers and returns 2 vectors, the first with the positive numbers of the vector of 8 positions, and the second with the negative numbers.

THE PROBLEM IS THAT I DO NOT KNOW HOW TO USE VECTOR, SINCE THE LAST TWO VECTORS MUST NOT HAVE DEFINED CAPACITY..

This is causing my second vector to "lose" in some way the positions of the positive numbers that are typed first. I don’t know if it’s clear.

If you could help, I’d be grateful.

MY CODE:

using namespace Std; int main(){

vector<int> vet(8);

for(int i=0;i<8;i++){
    cin>>vet.at(i);
}

vector<int> vet1(8);
vector<int> vet2(8);

int cont1=0,cont2=0;

for(int i=0;i<8;i++){
    if(vet.at(i)>=0){
        vet1.at(i)=vet.at(i);
        cont1++;
    }else{
        vet2.at(i)=vet.at(i);
        cont2++;
    }
}

for(int i=0;i<cont1;i++){
    cout<<vet1.at(i)<<" ";
}
cout<<endl;
for(int i=0;i<cont2;i++){
    cout<<vet2.at(i)<<" ";
}

return 0;
  • 1

    Welcome to Stack Overflow, Thiago! Please edit your question, and explain better the problem you are having (what is the expected result? what is the current result?) - usually questions with problem statement are not very well regarded, because the community ends up thinking that you want her to do the work for you - I suggest you remove this excerpt and write in detail only the problem you want to solve.

  • I see no problem in the statement, it helps understand what you are doing, has a code that tried, but it remains to say what the problem, what the doubt.

  • The problem is my resolution is wrong. The vector of positive numbers being printed on the screen is successful, but the second is in trouble. Also, I could not declare the size of "vet1" and "vet2", since I do not know the amount of positive and negative numbers.

  • It is good to do the vectors with the same size as the input. Look at the answer I gave below, I hope to have helped, any doubt communicate me.

1 answer

1


The problem is that it is inserting the vectors incorrectly, as it is using the same int to iterate the loop and to add to each vector.

For example if the vector 1,-1,67 comes,...

the vector vet1 will be 1,"garbage",67,... and the vector vet2 will be "garbage",-1,""

you must use the counters as indexes of the storage vectors as follows the code:

for(int i=0;i<8;i++){
    if(vet.at(i)>=0){
        vet1.at(cont1)=vet.at(i);
        cont1++;
    }else{
        vet2.at(cont2)=vet.at(i);
        cont2++;
    }
}

Tell me later if it worked. = D

  • Matthew, thank you so much for the answer!!! It worked. I have one more question about declaring the size of "vet1" and "vet2", because I do not know the amount of positive and negative numbers that the user will type, so I should not then create a vector without declaring its initial size? EDIT I say this because my teacher talks a lot about not wasting memory at all, making the program as light as possible.

  • Good is always good to save memory, make the program lighter, your teacher is right. You can see more about this if you search for dynamic vector allocation. You can do it in many ways. I think what your teacher is trying to get you to do is a pointer vector. You can learn a little more here: http://www.cplusplus.com/doc/tutorial/pointers/

  • Thanks again!!

Browser other questions tagged

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