c++ - Recursive equation implementation issue -


i given assignment few months ago had handful of random components. 1 recursive function had lot of fluff shouldn't have been hard. however, couldn't figure out how write it. i've gone , still can't quite figure out. can see full assignment here if you'd like: http://www.cs.fsu.edu/~asriniva/courses/ds14/projs/proj2.html

i'm dealing section called main.cpp: program evaluate recursive function. has nothing previous part of assignment. command: ./recurse n1 c1 a1 m1 m2 m3 m4 d1 d2 s1 s2 arg op cause function given below evaluated recursively , answer output. function defined follows.

f(n) = 0, if n < n1  f(n1) = c1  f(n)= a1 + m1*f(m2*n/d1 - s1) op m3*f(m4*n/d2 - s2), if n > n1 

here, f(arg) needs evaluated. n1 c1 a1 m1 m2 m3 m4 d1 d2 s1 s2, arg integers , op either + or -. division performed usual integer division truncation.

i got semi working program but, first, uses global variable, second, warning "recurse.cpp:37: warning: control reaches end of non-void function", and, importantly, doesn't work. gives result incorrect better original attempt couldn't give result. please me understand how thing working, i've spent more time i'd trying myself.

this supposed run on unix machine executable call followed command line arguments. so: prompt> ./recurse 2 3 2 1 2 0 1 3 6 0 0 18 +

13 (program output)

#include <iostream> #include <cstdlib> using namespace std;  char * op; int n1; int c1; int a1; int m1; int m2; int m3; int m4; int d1; int d2; int s1; int s2;   int recurse(int n){  if (n < n1){     return 0; } if (n == n1){     return c1;   } if (n > n1){      if (*op == '+')         return (a1 + m1 * recurse((m2*n / d1 - s1)) + m3 *              recurse((m4*n / d2 - s2)));     else          return (a1 + m1 * recurse((m2*n / d1 - s1)) - m3 *              recurse((m4*n / d2 - s2))); } }   int main(int argc, char* argv[]){  n1 = atoi(argv[1]); c1 = atoi(argv[2]); a1 = atoi(argv[3]); m1 = atoi(argv[4]); m2 = atoi(argv[5]); m3 = atoi(argv[6]); m4 = atoi(argv[7]); d1 = atoi(argv[8]); d2 = atoi(argv[9]); s1 = atoi(argv[10]); s2 = atoi(argv[11]); op = argv[12];  int val; if (*op == '+')     val = ( ( a1 + m1 * recurse(m2 / d1 - s1) + m3 *          recurse(m4 / d2 - s2) ) ); else      val = ( ( a1 + m1 * recurse(m2 / d1 - s1) - m3 *          recurse(m4 / d2 - s2) ) );  cout << val << endl;   return 0; } 

thanks help!

you main problem don't provide first n function. you're initializing 12 variables:

n1 = atoi(argv[1]); c1 = atoi(argv[2]); a1 = atoi(argv[3]); m1 = atoi(argv[4]); m2 = atoi(argv[5]); m3 = atoi(argv[6]); m4 = atoi(argv[7]); d1 = atoi(argv[8]); d2 = atoi(argv[9]); s1 = atoi(argv[10]); s2 = atoi(argv[11]); op = argv[12]; 

however pass 13 arguments command: n1 c1 a1 m1 m2 m3 m4 d1 d2 s1 s2 arg op. not collecting value of arg wich guess has stored in n.

on other hand, not practice use global variables. write better code using functor.

struct recurse {     recurse(){}     int n1, c1, a1, m1, m2, m3, m4, d1, d2, s1, s2;     string op; // since you're using c++ can use string instead char *.     int operator()(int n); };   int recurse::operator()(int n) {     if (n < n1)         return 0;      if (n == n1)         return c1;      if (n > n1)     {         if (op == "+")         {             return (a1 + m1 * this->operator()((m2*n / d1 - s1)) + m3 * this->operator()((m4*n / d2 - s2)));         }         else         {             return (a1 + m1 * this->operator()((m2*n / d1 - s1)) - m3 * this->operator()((m4*n / d2 - s2)));         }     }  } 

usage:

int main(int argc, char *argv[]) {      int n; // seems missed this.      recurse recurse;     recurse.n1 = atoi(argv[1]);     recurse.c1 = atoi(argv[2]);     recurse.a1 = atoi(argv[3]);     recurse.m1 = atoi(argv[4]);     recurse.m2 = atoi(argv[5]);     recurse.m3 = atoi(argv[6]);     recurse.m4 = atoi(argv[7]);     recurse.d1 = atoi(argv[8]);     recurse.d2 = atoi(argv[9]);     recurse.s1 = atoi(argv[10]);     recurse.s2 = atoi(argv[11]);      n = atoi(argv[12]);    // again, forgot this.      recurse.op = string(argv[13]);      cout << recurse(n) << endl;     return 0; } 

with input data you're providing output 13.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -