/* -----------
Starter code for Assignment #1, CSC270H Summer 2002

This code is not guaranteed to be correct; you *will* need to change
some things as well as add a lot of your own code.  Note: please do
not change any of the output messages (except to add error messages
for invalid input) as this may interfere with automarking software the
markers may choose to use.

It's best to pass f() to your root-finding methods using what is known
as a function pointer.  A function pointer is like a regular pointer,
except that it points to a function instead of a variable.  Like an
array, leaving the parentheses off a function call gives you a pointer
to the function. Also, you do not need to dereference the pointer in
order to call the function.  Example:

int g(int x);            Some function g(x) taking int and returning int 
double h(int (*i)());    Some function h(x) taking a pointer to a function
		         returning int, and returning double 
h(g);                    Example of calling h() with g() as argument
i(10);                   Example of calling g() from within h()

Here's a complete example:

#include <stdio.h>
int f(int x) { return x*x*x; }
int g(int x) { return x*x; }
double h(int (*i)()) { return i(10) + 5.0; }
int main(void) {
  printf("h(f)=%f  h(g)=%f\n", h(f), h(g));
  return 0;
}

Check your C book for further details.

----------- */

#include <stdio.h>

int main(void) {

  int choice;

  printf("This program finds the root of f(x).\n");

  printf("Enter the maximum number of iterations: ");

  printf("Enter the tolerance: ");

  printf("Enter the initial guess p0: ");

  printf("Enter the initial guess p1: ");

  while (choice != 4) {

    printf("\nMENU\n"
	   "====\n"
	   "1. Bisection method\n"
	   "2. Newton--Raphson method\n"
	   "3. Secant method\n"
	   "4. Quit\n"
	   "Your selection: ");

    switch(choice) {

    case 1:
      printf("Bisection method:\n");
      bisection();
      break;

    case 2:
      printf("Newton--Raphson method:\n");
      newton_raphson();
      break;

    case 3:      
      printf("Secant method:\n");
      secant();
      break;

    }

    if (...)
      printf("The root is %f.\n", ...);
    else
      printf("Root not found.\n");
  }

}

void bisection() {

  printf("Iteration\tApproximation\n"
	 "---------\t-------------\n");
  while (...) {
    printf("%9d\t%.12f\n",...);
  }

}

void newton_raphson() {

  printf("Iteration\tApproximation\n"
	 "---------\t-------------\n");
  while (...) {
    printf("%9d\t%.12f\n",...);
  }

}

void secant() {

  printf("Iteration\tApproximation\n"
	 "---------\t-------------\n");
  while (...) {
    printf("%9d\t%.12f\n",...);
  }

}

