// Arup Guha
// 7/31/02
// This program calculates the square roots of integers using an iterative
// method similar to the Newton-Rhapson approximation method.

#include <iostream.h>
#include <math.h>

void print_chart(int low, int high);
double my_sqrt(int n);
void iter(double& x, double y);

int main() {

  // Read in the boundaries for the chart.
  int number1, number2;
  cout << "Enter the first number for your square root chart: ";
  cin >> number1;

  cout << "Enter the second number for your square root chart: ";
  cin >> number2;

  // Print the chart.
  print_chart(number1, number2);
  return 0;
}

// Prints a chart of square roots for each integer, starting from low all
// the way through high.
void print_chart(int low, int high) {

  // Print out the square root for each integer.
  cout << "Number\tSquare Root" << endl;
  for (int i=low; i<=high; i++)
    cout << i << "\t" << my_sqrt(i) << endl;
}

// Computes the square root of n, iteratively.
double my_sqrt(int n) {

  double num1 = n;
  double num2=1;

  // Loop until the estimate is pretty good.
  while (fabs(num1*num1 - n)> 0.0000001) {

    // Revise the estimate.
    iter(num1, num2);
    num2 = n/num1;
  }
  return num1;
}

// Changes the estimate x based on the old values of x and y.
void iter(double& x, double y) {
  x = (x+y)/2;
}

