successive approximation

Hi, I am working on a program that will allows the user to compute the Nth root of a value, X. I am having a bit to trouble getting the getNthRoot method below to compile. The rest of the code was provided to me and is in good working order, so I am not looking to change anything outside of the getNthRoot method. However I am not sure if I am passing the correct parameters to the power method and I also have the following errors when compiling (which may or may not be a related issue).

135: error: âfâ was not declared in this scope
135: error: âposIntâ was not declared in this scope
138: error: âresultâ was not declared in this scope
145: error: ârootâ was not declared in this scope


I would appriciate any info on how to get this to compile, as I think I am just missing something minor and can't see it.

Thanks!


-------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

// the final result must be accurate to within 0.001%
const float Accuracy = 1.00001;

//computes and returns the nth root of x, to within the accuracy bounds
// specified by a constant defined within the program
float getNthRoot(float x, int n);

//
void displayRoot(float x, int n, float root);

//
int getPosInt(float x);

//
float getFloat();

//
float power(float f, int p);

//
void displayInstructions();

int main()
{
float x = getFloat();
int n = getPosInt(x);
float root = 1;
root = getNthRoot(x, n);
displayRoot(x, n, root);
return 0;
}

// *******************************************************************

int getPosInt(float x)
{
string userEntry;
int posInt = 0;
do {
cout << "Please enter a positive integer, N,";
cout << " to compute the Nth root of " << x << endl;
cin >> userEntry;
posInt = atoi(userEntry.c_str());
if (posInt < 1) {
cout << posInt << " is too small, please try again\n";
}
} while (posInt < 1);
return posInt;
}

//
float getFloat()
{
string userEntry;
float f = 0;
do {
cout << "Please enter the value, X, you wish to compute the root of\n";
cin >> userEntry;
f = atof(userEntry.c_str());
if (f <= 1) {
cout << f << " is too small, please try again" << endl;
}
} while (f <= 1);
return f;
}

float power(float f, int p)
{
float result = 1;
for (int i = 1; i <= p; i++)
result *= f; //result is = to result times f
return result;
}

void displayRoot(float x, int n, float root)
{
cout << "The ";
switch (n) {
case 1: cout << "1st"; break;
case 2: cout << "square"; break;
case 3: cout << "cube"; break;
default: cout << n << "th"; break;
}
cout << " root of " << x << " is approximately " << root << endl;
}

//displays instructions to the user
void displayInstructions()
{
cout << "This program allows you to compute the Nth root of a\n";
cout << "positive number, X, provided both are greater than 1,\n";
cout << "and N is an integer" << endl;
}

// *******************************************************************

float getNthRoot(float x, int n)
{
// use a midpoint variable for the root approximations
float midpoint = 1;

// declare upper and lower bounds for the computed root
// initialized to x and 1 respectively
float upperBound = x;
float lowerBound = 1;

// apply shortcuts:
// if n is 0 return 0,
if (n == 0) {
return 0;
}

// if n is 1 return x
if (n == 1) {
return x;
}

// main approximation loop:
// repeat
do {
// update midpoint as the mean of the upper and lower bounds
midpoint = (lowerBound + upperBound)/2;

// call the power routine to compute midpoint^n
power(f, posInt);

// if the result matches x we're done, return the midpoint
if (result == x) {
return midpoint;
}

// otherwise if the result is less than x
// replace the old lower bound with the midpoint
// (since we know the real root must be bigger than the midpoint)
else if (root > midpoint) {
lowerBound = midpoint;

// otherwise replace the old upper bound with the midpoint
} else {
upperBound = midpoint;
}
} while (lowerBound*Accuracy < upperBound);
// upper and lower not close enough yet

// return the computed midpoint
return midpoint;
}


in getNthRoot
power(f, posInt);

f and posInt are not declared in this function or passed to it so they cant be used in this function. according to the errors you posted you also did this with the result and root variables somewhere
Last edited on
Topic archived. No new replies allowed.