我正在尝试创建一个程序,让我找到一个数字的第 n 个根。我尝试在 xcode 上运行它,但我收到一个错误,阻止我运行它。我收到这条线的错误:
double f(double x) {
在这一行中,我试图声明一个函数,但似乎我声明它不正确。xcode 说expected ;
。我该如何解决这个问题?
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <stdio.h>
#include <ctype.h>
#include "helloworld.h"
#include <locale>
using namespace std;
double newton( int n, int number);
string lowercase (string word);
int main()
{
int n;
int number;
cout << "This program can find the nth root of a number without actually solving the problem" <<endl;
cout << "Tell me what the value of n is: ";
cin >> n;
cout << "What is the number that you want to get rooted";
cin >> number;
newton(n, number);
}
double newton( int n, int number) {
const double epsilon = .0001;
double x0;
double x1 = number;
double f(double x) {
return (double) pow(x, n) - number;
}
double der_f(double x) {
return (double) n*pow(x, n-1);
}
while( abs(x1-x0) < epsilon) {
x0 = x1;
x1 = x0 -f(x0)/der_f(x0);
}
return x1;
}