Hi,
I am trying to create a c++ program that will intake an original value of x_0 to begin a function that uses the previous value of x to calculate the one in question.
I.e.- f(x_0) = x_1, f(x_1) = x_2 and so on.
I understand that I will need to set up a loop using x = f(x) but am unsure how this is done exactly. The function being used is as follows: f(x) = a*sin(pi*x) where a and x_0 are the inputs. Any help would be great, thanks.
Try making f() a function that takes x and a as parameters and returns a*sin(pi*x). Prompt the user for x and a, then run a loop that assigns f(x, a) to x.
hanst99- yes this is what i meant, i wish to input x and a then run the loop but before doing so I need to create the function and this is what I am having trouble doing.
Zhuge- I had the function included in the same way you suggested but when building I get an error saying a function-definition is not allowed here before "{" token.
1 int main (){
2 int a,x;
3 cout<<"a=";
4 cin>>a;
5 cout<<"x=";
6 cin>>x;
7
8 int f(int x, int a) {
9 return a*sin(pi*x);
10 }
11
12 for (i=0; i<=20; i++){
13 x=f(x);
14 return f(x);
15 }
16 }
the errors occuring when compiling are:
a function-definition is not allowed here before "{" token (line 8)
"i" was not declared in the scope (line 12)
"f" was not declared in the scope (line 13)