简体   繁体   中英

Recursive equation implementation issue

I was given an assignment a few months ago that had a handful of random components. One was a recursive function that had a lot of fluff to it but shouldn't have been too hard. However, I couldn't figure out how to write it. I've gone back to it and I still can't quite figure it out. You can see the full assignment here if you'd like: http://www.cs.fsu.edu/~asriniva/courses/DS14/projs/proj2.html

I'm only dealing with the section called main.cpp: This is a program to evaluate a recursive function. It has nothing to do with the previous part of the assignment. The command: ./recurse N1 C1 A1 M1 M2 M3 M4 D1 D2 S1 S2 Arg Op will cause the function given below to be evaluated recursively and the answer output. The function is defined as 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 to be evaluated. N1 C1 A1 M1 M2 M3 M4 D1 D2 S1 S2, Arg are integers and Op is either + or -. The division performed is the usual integer division with truncation.

I got a semi working program but, first, it uses some global variable, second, I get a warning "recurse.cpp:37: warning: control reaches end of non-void function", and, most importantly, it doesn't actually work. I gives a result that is incorrect but its better than my original attempt that couldn't actually give a result. Please help me understand how to get this thing working, I've spent more time than I'd like trying to by myself.

This is supposed to run on a unix machine with an executable call followed by command line arguments. Like 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 for the help!

You main problem is that you don't provide the first N to your 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 you pass 13 arguments to the command: N1 C1 A1 M1 M2 M3 M4 D1 D2 S1 S2 Arg Op . So you are not collecting the value of Arg wich I guess has to be stored in N .

On the other hand, is not good practice to use global variables. You could write better code using a functor .

struct Recurse
{
    Recurse(){}
    int N1, C1, A1, M1, M2, M3, M4, D1, D2, S1, S2;
    string Op; // Since you're using C++ you 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; // It seems you 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, you forgot this.

    recurse.Op = string(argv[13]);

    cout << recurse(N) << endl;
    return 0;
}

With the input data you're providing the output is 13.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM