jeudi 26 octobre 2017

Print a map as a sequence C++

I am trying to fill out this function, however I am having some trouble with it. It takes in a map with a long, and vector(long) and a long as a parameter.

The function and its specifications are here down below.

Input is a collatz map (map(long, vector(long)) and a long returns a vector, the collatz sequence for that number Operation. As you iterate through the collatz sequence - uses collatz_next if the element in question is not in the map - if the element is in the map, copies the sequence from the map to the end of the current sequence and ends.

vector<long> collatz_sequence(map<long, vector<long>> &m, long number) {
// This is what i've tried. 
vector<long> my_vec = m[number];
return my_vec;
}

This is the other function that it uses:

long collatz_next(long n) {
long return_value = 0;
if (n > 0) {
    if (n%2 == 0) {
        return_value = n/2;
    } else {
        return_value = (3*n) + 1;
        //return return_value;
    }
} else {
    throw range_error("error");
}
return return_value;
}

Aucun commentaire:

Enregistrer un commentaire