C++ Filling out these functions /* input is a collatz map (map >)and a long if t
ID: 3602170 • Letter: C
Question
C++ Filling out these functions
/*
input is a collatz map (map >)and a long
if the number exists as a key in the map
returns the Collatz_to_string of that pair
otw returns an empty string
*/
string sequence_in_map_to_string(map > &m, long number) {
}
/*
input is a collatz map (map >)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 collatz_sequence(map > &m, long number) {
}
/*
input is a collatz map (map >)and a low and high long
fills the map from low to high inclusive with each element's collatz sequence
using the function collatz_sequence
*/
void collatz_range(map > &m, long low, long high) {
}
Explanation / Answer
lab09_fuctions.h
#pragma once
#include<vector>
using std::vector;
#include<string>
using std::string;
#include<map>
using std::map;
#include<utility>
using std::pair;
using Collatz = pair<long, vector<long> >;
long collatz_next(long n);
string Collatz_to_string(const Collatz &p);
string sequence_in_map_to_string(map<long, vector<long> > &m, long number);
vector<long> collatz_sequence(map<long, vector<long> > &m, long number);
void collatz_range(map<long, vector<long> > &m, long low, long high);
Lab08_functions.cpp
#include "stdafx.h"
#include "Lab08_functions.h"
#include <iostream>
int main()
{
return 0;
}
/**
* gives the next collatz number
*/
long collatz_next(long n)
{
// if number is 0 or less, throws range_error;
if (n < 1) { throw std::range_error("Number should be positive (1 or greater)!"); }
if (n % 2) { return n / 2; }
else { return 3 * n + 1; }
}
/*
* display format for collatz
*/
string Collatz_to_string(const Collatz & p)
{
std::string out{};
// output is a string of the format number :
out += std::to_string(p.first) + " : ";
// sequence(comma separated)
for (int i{ 0 }; i < p.second.size(); ++i)
{
out += p.second.at(i) + ", ";
}
// ending in 1 no trailing comma
out += "1";
return out;
}
string sequence_in_map_to_string(map<long, vector<long>>& m, long number)
{
// if the number exists as a key in the map
for (std::map<long, vector<long>>::iterator it = m.begin(); it != m.end(); ++it)
{
if (number == it->first) { return Collatz_to_string(*it); }
}
return string{};
}
/**
* returns the collatz sequence for that number
*/
vector<long> collatz_sequence(map<long, vector<long>>& m, long number)
{
Collatz collatz;
std::map<long, vector<long>>::iterator iterator{ m.find(number) };
if (iterator != m.end())
{
// copies the sequence(collatz?) from the map
collatz.first = number;
collatz.second = iterator->second;
// to the end of the current sequence and ends.
m.insert(collatz);
return iterator->second;
}
// only init this vector if seq is not found!
std::vector<long> collatz_seq{};
collatz_seq.push_back(number);
// uses collatz_next if the element in question is not in the map
while (1)
{
number = collatz_next(number);
if (number < 2) { break; }
else { collatz_seq.push_back(number); }
}
collatz.first = number;
collatz.second = collatz_seq;
m.insert(collatz);
return collatz_seq;
}
/*
* fills the map from low to high inclusive with each element's collatz sequence
* using the function collatz_sequence
*/
void collatz_range(map<long, vector<long>>& m, long low, long high)
{
for (long i{ low }; i < high + 1; ++i)
{
collatz_sequence(m, i);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.