Reputation: 85
i'm not much familiar to define function in cpp, but as usual i defined mine like:
#include<iostream>;
#include<Windows.h>;
using namespace std;
int main(){
int n;
int power;
//f(x) = x^power-n=0
cout << "please enter number of root: " << endl;
cin >> power;
cout << "what number you want to get root: "<< endl;
cin >> n;
double x = 2;//it's the first x0 that newton algorithm needs
for (int i = 0; i<20000 ;i++)
{
//f(x) = x^power-n=0 , f'(x) = 2*x //fx:x^power
x = x - (fx(power,x) - n)/dif(power,x);
}
cout << "answer is " << x << endl;
return 0;
}
double fx (int power,int x){
for (int i = 1; i<power; i++)
{
x = x*x;
}
return x;
}
double dif (int power,int x){
//f(x) = x^power-n=0 -> f'(x) = power * x^(power-1)
if(power>1)
{
for(int i = 1; i<power-1 ;i++){
x = x*x;
}
x = x*power;
return x;
}
return 1;
}
and i faced with 2 errors that say: Error 2 error C3861: 'fx': identifier not found Error 4 error C3861: 'dif': identifier not found so what should i do?
Upvotes: 0
Views: 526
Reputation: 17043
Place function declarations
double fx (int power,int x);
double dif (int power,int x);
before using them.
using namespace std;
double fx (int power,int x);
double dif (int power,int x);
int main(){
Now your functions are unknown for compiler in place where you are trying to call them.
Upvotes: 1
Reputation: 311028
Any name shall be declared before its using. You are using function name fx
but you placed its declaration (which at the same time its definition) after the usage of its name. Place before main its declaration
double fx (int power,int x);
The same is valid relative function dif
Upvotes: 0