Click here to Skip to main content
16,021,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
#include<vector>

using namespace std;

vector<int> mix(vector<int> v1, vector<int> v2)
{
    vector<int> newVec{v1.at(0), v2.at(0)};
        if(v2.size()+2>1){
            vector<int> restV1(v1.begin() + 1, v1.end());
            vector<int> restV2(v2.begin() + 1, v2.end());
                vector<int> call_vec = mix(restV1, restV2);
                newVec.reserve(newVec.size());

                 vector<int> newVeca=newVec;
                 for(size_t i(0);i<newVeca.size();i++){
                    cout<< newVeca[i]<< "|";
                 }
             }
        return newVec;
}

int main()
{
    vector<int> v1{1,2,3,4};
    vector<int> v2{5,6,7,8};

    mix(v1,v2);

    return 0;
}


What I have tried:

Hey every one !!!
I have this homework, that we need to mack it without loop and i have a trouble understanding, the tow thing here
1- why the new-Vec is not in the right order (is that from the recursion or somthing i done wrong in my code )
2- i coudnt get the last tow integer in add the new integer
everytime i'm treing to increse the size in if ( if(v2.size()+2>1))i get this error:
(terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0))



Write a function that gets two vectors of int values as parameters and returns a vector of int values as a return value. The returned vector should always contain alternately a value from the first parameter and a value from the second parameter (starting with the first value from the first parameter). The relative order of the values should be the same in the result vector as in the input vectors. If the two input vectors are of different lengths, the remainder of the longer vector is unchanged

Parameter: Ergebnis:
{1,2,3} {4,5,6} {1,4,2,5,3,6}
{1,2,3,4,5,6} {7,8,9} {1,7,2,8,3,9,4,5,6}
Posted
Updated 2-Nov-18 3:34am
v2

this
C++
if(v2.size()+2>1)
always is true. So it is a problem. You arent using the call_vec? Isnt that the result?

Make some output and use the debugger.

if it is possible use call by reference with the 6-operator:
C++
vector<int> mix(vector<int> &v1, vector<int> &v2)
This aovids creating new instances in the function call. (Use "step into" debugger commands to prove it9
 
Share this answer
 
Comments
Anas Zahed 2-Nov-18 6:47am    
i used the call_vec just to create looping, i thought the result is in newVec, but you are right

when i use if(v2.size()>1) the code will give me no error but , the result look like this 3|7|2|6|1|5| so it missed to numbers and its reversed
KarstenK 2-Nov-18 8:40am    
As I wrote: use the debugger. The first thing what goes wrong is your next step to the soluation.

I think your real result is newVec and append call_vec at the end.
thanks
done it


#include<iostream>
#include<vector>

using namespace std;

vector<int> mix(vector<int> &v1, vector<int> &v2)
{            vector<int> newVec{v1.at(0), v2.at(0)};

        if(v1.size()>1){
            vector<int> restV1(v1.begin() + 1, v1.end());
            vector<int> restV2(v2.begin() + 1, v2.end());
                vector<int> call_vec = mix(restV1, restV2);

        newVec.insert(newVec.end(), call_vec.begin(), call_vec.end());
}

 return newVec;


    }




int main(){

    vector<int> v1{1,2,3,4};
    vector<int> v2{5,6,7,8};

    vector<int> v =  mix(v1,v2);


                 for(size_t i(0);i<v.size();i++){
                    cout<< v[i]<< "|";

        }
}
 
Share this answer
 
v2
Comments
CPallini 2-Nov-18 16:41pm    
Is recursion required? I think an iterative approach would be better.
Anas Zahed 25-Nov-18 10:42am    
jap !! it musst be in recurion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900