2

I am new to c++. I have to calculate a value via equation and store that value each time whenever the function is being called. I have created an array of size 10 and calculated myValue then tried to store that value. Is this right way to do so? Will it work in way that each time whenever the function is called it will calculate and store myValue and use that value n[i-1] in the next call to calculate my value n[i]. Let's say in 1st call myvalue is 0.5. In next call it would be myValue = (1-0.3)* 0.5. Will it store all 10 values in 10 calls and use the last stored value to calculate myValue. It is not showing me any error when I am compiling it but still I have doubt.

static double
CalculatemyValue(Node* ch)
{ 
float gamma=0.3;
double myValue = 0.0;
   int n[10];
   int i = 0;
   n[i] = myValue;
   myValue = ((1-gamma)*n[i-1]) //previous value
   return myValue;
} 
1
  • I am having trouble understanding what you need to do but it sounds like you may need a static local variable in there somewhere. Commented Jun 22, 2016 at 18:08

1 Answer 1

1

In the given code

static double
CalculatemyValue(Node* ch)
{ 
float gamma=0.3;
double myValue = 0.0;
   int n[10];
   int i = 0;
   n[i] = myValue;
   myValue = ((1-gamma)*n[i-1]) //previous value
   return myValue;
}

… the array n is a local automatic variable. It's created anew each time execution reaches the declaration, and destroyed when execution leaves the block. No information is retained between calls.


One good way to retain state between calls is to make the function a member function of a class, whose data members constitute the state you want to keep, e.g. like this:

class Power_sequence
{
private:
    double   gamma_;
    double   value_;
public:
    auto gamma() const -> double { return gamma_; }

    void advance() { value_ *= 1 - gamma_; }
    auto current() const -> double { return value_; }

    auto next()
        -> double
    {
        double const result = current();
        advance();
        return result;
    }

    Power_sequence( double const gamma = 0.3 )
        : gamma_( gamma )
        , value_( 1.0 )
    {}
};

… which you would use like this:

Power_sequence seq;
for( int i = 1; i <= 42; ++i )
{
    cout << seq.next() << endl;
}

Disclaimer: above code not checked by a compiler.

Another way is to pass the state as an explicit argument to the function.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Cheers and hth - Alf . Will this code store all the 42 values? Because my next step is to find out the max value among all the stored value.
This code only stores the most recent value. You can modify to store all the values. Whatever you want. A std::vector is usually a good choice for storing a dynamic number of values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.