Array to store value each time calculated by equation whenever the function is being called

Jun 23, 2016 at 5:03am
Hello everyone! I am new to c++. I have to calculate a value through an 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. I am not sure that this is a correct way to do so. I did not run a loop because if I'll run a loop here it will calculate 10 values in single call while I have to calculate and store 1 value in each call.

static double myValue = 0.0;
static double
CalculatemyValue(Node* ch)
{
float gamma=0.3;
double myValue = 0.0;
double n[10];
int i = 1;
n[i] = myValue;
myValue = ((1-gamma)*n[i-1]) //previous value
return myValue;
}
Last edited on Jun 23, 2016 at 5:04am
Jun 23, 2016 at 5:25am
Hi,

Just use a global variable which will increment every time you calculate

somewhat like this;

1
2
3
int val=0;//this is global (global so that you can use it in functions too)

double n[10];//I suppose this is where you want to store it 


then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static double myValue = 0.0;
static double
CalculatemyValue(Node* ch)
{ 
float gamma=0.3;
double myValue = 0.0;


n[val] = myValue;
if(val==0)
{
//put your condition for first value
}
else
{
myValue = ((1-gamma)*n[val-1]) //previous value
val++;//this will increment without loop
}
return myValue;
} 


HOPE IT HELPS
Jun 23, 2016 at 12:48pm
http://www.cplusplus.com/forum/beginner/193209/

Duplicate post. Please don't do this - it's a time waster for those who reply. All it is going to do is lower our willingness to reply. What was wrong with the replies in the other topic?

@programmer007

Not a good idea to have global variables, the scope should be as tight as possible.
Jun 23, 2016 at 12:58pm
Thank you so much everyone. My issue has been resolved.
Jun 23, 2016 at 1:06pm
welcome :)

@TheIdeasMan

then how will it work with functions?

@Aisha1234

Like TIM said please don't double post in future
Jun 23, 2016 at 1:17pm
then how will it work with functions?


Send the variable to whatever function needs it. If sent by reference the it will be available it the outer scope. If the variable can be local to the function then do that.
Jun 23, 2016 at 2:26pm
sorry I next time i'll be careful.. thanks
Jun 24, 2016 at 2:26am
Send the variable to whatever function needs it. If sent by reference the it will be available it the outer scope. If the variable can be local to the function then do that.


like this???

1
2
3
4
5
6
7
8
9
10
11
12
13
main()
{
int counter;
//..

function(val1,val2,*counter)
}

function(int v,char v2,int *c)
{
//..
(*c)++;
}


PS: There was no need to report Aisha1234, it was a honest mistake
Jun 24, 2016 at 5:00am
like this???

No more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Function prototype.
void function(int& c);

int main() /// Note that main should be defined to return an int.
{
   int counter = 2;
   function(counter);
}

void function(int& c) // Pass by reference.
{
   c++;
}

C++ doesn't allow default return types you must explicitly state the function return types. You should also prefer passing by reference over passing by pointer whenever possible.



Jun 24, 2016 at 6:17am
closed account (48T7M4Gy)
http://www.cplusplus.com/articles/z6vU7k9E/
Jun 24, 2016 at 1:42pm
ooh! that's neat.... thanks
Jun 24, 2016 at 1:47pm
whoops! 2 questions...

how will I get to know the value of counter??? another function?

and what is wrong in my previous code (apart from no-return type defined which I left)
Jun 24, 2016 at 2:53pm
what is wrong in my previous code (apart from no-return type defined which I left)

This is C++ you should prefer reference parameters over pointer parameters whenever possible.

how will I get to know the value of counter??? another function?

What? The snippet posted is in no way a complete program, get the value anywhere you want.

Jun 25, 2016 at 2:43am
get the value anywhere you want


???

could you please explain with code? The way I think of it, I can't catch the value of counter in the code without using a complicated method (which requires returning value of counter with a if-statement and increasing the argument of function)
Jun 25, 2016 at 4:03am
could you please explain with code?

I suggest you write a program then ask questions on the code you provide.

I think of it, I can't catch the value of counter in the code without using a complicated method (which requires returning value of counter with a if-statement and increasing the argument of function)

That doesn't make a lot of sense. Perhaps you should post some code to illustrate your problem.

By the way when you do post the question I suggest you open your own topic instead of hijacking the topic of someone else.

Jun 25, 2016 at 5:15am
I did posted my code above, but apparently it had faults, and sorry for hijacking the topic
Jun 25, 2016 at 8:42am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void function(int* c);

int main()
{
   int counter = 2;
   
   function( &counter );   
   std::cout << "Counter: " << counter;
}

void function(int* c)
{
   (*c)++;
}

Counter: 3 
Last edited on Jun 25, 2016 at 8:43am
Jun 25, 2016 at 2:49pm
thanks... :)
Topic archived. No new replies allowed.