So I have recursive function that needs to be memoized since it takes so long to run. I understand that you store it in a cache, but need help doing that. Also one thing I noticed was that when I printed the value after the function call, I would get the correct value, but when i removed it, the answer would be wrong. Any help is appreciated. The function is supposed to calculate the probability of getting a number after a rolling a s sided die that isn't within 1 of the previous roll after rolling t times
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <vector>
using namespace std;
double dond(int s, int t, int l);
int main(int argc, char *argv[]) {
int s, t, l;
s = atoi(argv[1]);
t = atoi(argv[2]);
l = atoi(argv[3]);
//printf("%d %d %d\n", s, t, l);
cout << dond(s, t, l) << endl;
}
double dond(int s, int t, int l) {
int i, j;
double a, d = 0, yuh = 1.0/s;
vector <double> cache;
if(t == 1 && l == -1) return 1;
if(t == 1) {
if(l == 0 || l == s-1) return (s-2)/(double)s;
else return (s-3)/(double)s;
}
for(i = 0; i < s; i++) {
if(l == -1 || (i != l && i != l-1 && i != l+1)) {
cache.push_back(dond(s, t-1, i)); //store into cache
// printf("A: %f\n", a);
//printf("",a );
}
}
for(i = 0; i < cache.size(); i++) a += cache[i];
return yuh * a;
}