1

I am trying to create a program that allows me to find the nth root of a number. I tried running this on xcode and, I get an error that prevents me from running it. I'm getting an error for this line:

double f(double x) {

In this line, I am trying to declare a function, but it seems I'm declaring it incorrectly. xcode says expected ;. How can I address this issue?

#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;
}
jogojapan
  • 63,098
  • 9
  • 87
  • 125
mtber75
  • 858
  • 1
  • 7
  • 15

4 Answers4

2

If you really want function inside function - there is a hack. You could define struct with static function inside your function.

Example:

double newton( int n, int number) {
    const double epsilon = .0001;
    double x0;
    double x1 = number;

    struct wrap {
       static int n;
       static double f(double x) {
           return (double) pow(x, n) - number;
       }
       static double der_f(double x) {
           return (double) n*pow(x, n-1);
       }
    };
    wrap::n = n;

    while( abs(x1-x0) < epsilon) {
        x0 = x1;
        x1 = x0 -wrap::f(x0)/wrap::der_f(x0); 
    }
    return x1;
}

Something like this.

kassak
  • 3,631
  • 23
  • 36
  • This is good, but I am still having debugging issues. Now, the program can not run because I receive this message "Reference to local variable 'n' declared in enclosed function 'newton'" – mtber75 Dec 25 '12 at 00:55
1

Move

   double f(double x) {
        return (double) pow(x, n) - number;
    }
    double der_f(double x) {
        return (double) n*pow(x, n-1);
    }

to outside of double newton( int n, int number) {

Preet Sangha
  • 61,126
  • 17
  • 134
  • 202
  • I am aware that I can solve that problem by moving the function outside. Can't this function be inside the newton function? – mtber75 Dec 24 '12 at 07:13
  • If you use C++11, you can use lambda functions for that (or boost::lambda without C++11, but then you have to include a library) – Thilo Dec 24 '12 at 07:17
  • I see. Well, I am very use to javascript and I love how lenient it is compared to c++. I now know why I can not declare functions inside functions. Thank you. – mtber75 Dec 24 '12 at 07:21
1

This is happening because you're declaring new functions inside of a function, which is not possible. Try this:

#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);


double f(double x) {
    return (double) pow(x, n) - number;
}

double der_f(double x) {
    return (double) n*pow(x, n-1);
}

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;
  // and here call the functions
  f();
  der_f();
  while( abs(x1-x0) < epsilon ) {
    x0 = x1;
    x1 = x0 -f(x0)/der_f(x0); 
  }
  return x1;
}
L0j1k
  • 10,933
  • 7
  • 48
  • 64
0

As it is already pointed out, you have to get rid of local functions. Another way of doing it in C++11 is to use lambda-functions:

auto f = [=](double x) {
  return (double) pow(x, n) - number;
};
auto der_f = [=](double x) {
  return (double) n*pow(x, n-1);
};

All you need in this case is to replace double func_name(double x) with auto func_name = [=](double x).

The [=] is a lambda-introducer. By = you're telling that you want all of your local variables to be accessible in a lambda-function by value. This is the most appropriate way in your case since you only have variables of basic types and you don't want to modify them from your local functions.

The auto keyword tells a compiler to automatically deduce type of a variable for storing a function from the initialization clause. One can std::function<double(double)> instead of auto here.

Community
  • 1
  • 1
Mikhail
  • 18,155
  • 5
  • 56
  • 129