u/ajNzSDlz - Archived Voat Post in v/programming
u/ajNzSDlz
  • home
  • search

u/ajNzSDlz

0 posts · 1 comment · 1 total

Active in: v/programming (1)

  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››
Comment on: quiz. write a function recurseList(f,x,n) that returst a list, [f(x), f(f(x)), ...], length n.

C++:

#include <iostream>
#include <vector>
int example(int i){
    return i+i;
}
template<typename T> std::vector<T> recurseList(T (*f)(T),T x,size_t n)
{
    if(f == NULL)
        return std::vector<T>(); //TODO:Add better error handling
    std::vector<T> V(n);
    V[0] = f(x);
    for(size_t i = 1; i < n; i++)
        V[i] = f(V[i-1]);
    return V;
}
int main() {
    for(auto &i: recurseList(example,3,5)) {
        std::cout << i << " ";
    }
}
2 01 Nov 2017 12:54 u/ajNzSDlz in v/programming
  • ‹‹‹
  • ‹‹
  • ‹
  • 1
  • ›
  • ››
  • ›››

archive has 9,592 posts and 65,719 comments. source code.