I hd to write a total of 4 small programs that deal with writing function. I have two of them that i need to have checked because they are not working correctly and I dont know whatI'm doing wrong. Can someone please help me? My code is after the program description.

Exercise 1

Filename: middle.cpp

Write a function called Middle that takes in three integer parameters and returns the middle value (i.e. if they were arranged in numerical order)

To test this function, write a main() routine (in the same file) that prompts the user and allows entry of three integers, then passes the three integers into the function and prints out the returned result.

Sample run 1:

Please input first integer: 12

Please input second integer: 19

Please input third integer: 15

The middle number is: 15

--------------------------------------------------------------------------------Code:

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

int middle (int, int, int);

int main ()

{

int integer1, integer2, integer3;

cout << "Please input first integer: ";

cin >> integer1;

cout << "Please input second integer: ";

cin >> integer2;

cout << "Please input third integer: ";

cin >> integer3;

cout << "The middle number is: " << middle(integer1,integer2,integer3) << endl;

return 0;

}

int middle (int a, int b, int c)

{

int middle = b;

//if ( a < middle)

//middle = a;

if( c < middle)

middle = c;

if (c > middle && a < middle)

middle = a;

if (middle > a )

middle = a;

if (middle > c)

middle = c;

return middle;

}

Exercise 2

Filename: root.cpp

Write a function called SquareRoot that takes in a single parameter (type double) and returns an approximated square root (also a double), using the following calculation process:

The square root of a number, num can be approximated by repeatedly performing a calculation using the following formula:

nextGuess = (lastGuess + (num / lastGuess)) / 2

When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. You can start the initial guess (i.e. the first value of lastGuess at 1.0, for the first computation. Once the difference between nextGuess and lastGuess is less than a small number (use the value 0.0001), you may claim that nextGuess is the approximated square root of num. (You may not use the square root function in the math library -- the point of this exercise is to write your own function).

To test this function, write a main() routine that prompts the user and lets them enter a number (of type double), then passes the number into your square root function and prints out the returned value.

Sample run 1:

Input Number: 123

square root of 123 = 11.0905

Code:

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

double squareroot (double);

int main()

{

double num;

cout << "Enter a value " ;

cin >> num;

cout << "The square root of : " << num << " is " <<

squareroot(num) << endl;

return 0;

}

double squareroot(double num)

{

double lastGuess = 1.0;

double nextGuess = (lastGuess + (num / lastGuess))/2.0;

while(!( (nextGuess <= (lastGuess + 0.0001)) && (nextGuess >= (lastGuess - 0.00001))) )

{

nextGuess = (lastGuess + (num / lastGuess))/2.0;

num = nextGuess;

}

return nextGuess;

}