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;
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.
– Daniel
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.
– Maniero
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.
– ThiagoAncap
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.
– Um Programador