# can someone check my code and tell me what I'm doing wrong

• 09-20-2003
jlmac2001
can someone check my code and tell me what I'm doing wrong
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:

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; }```
• 09-20-2003
KingZoolerius66
Hold up, I'm gonna re-write your first example and post the necessary revisions.
• 09-20-2003
KingZoolerius66
Code:

```#include <iostream> #include <cstdlib> using std::cout; using std::cin; using std::endl; int GetMiddle(int, int, int); int GetMiddle(int first, int second, int third) {   int Memory[3]={ first, second, third };   for(int j= 0; j<3; j++)   {       for(int k= j + 1; k<3; k++)       {         if( Memory[j] > Memory[k] )         {             int temp= Memory[j];             Memory[j]= Memory[k];             Memory[k]= temp;         }       }   }   return Memory[1]; } int main( int argc, char *argv[] ) {     int a, b, c;     cout << "Input the first number: ";       cin >> a;     cout << "Input the second number: ";       cin >> b;     cout << "Input the third number: ";       cin >> c;     cout << "Middle number is: " << GetMiddle(a, b, c);     cin.get();     return 0; }```
• 09-20-2003
KingZoolerius66
I'm gonna re-write the sqaureroot one now. I hope you understand this.
• 09-20-2003
KingZoolerius66
If this is for homework then I won't be writing the second one.
• 09-20-2003
alphaoide
Dude, don't just re-write the whole program for the other person. Just try to find what's wrong with the codes. Good spirit, tho :D
• 09-20-2003
KingZoolerius66
I'm better at coding than explaining things. So that was my reason...
• 09-20-2003
alphaoide
But it does not help other people getting better at coding, but better at copying. Just analyze/walk through his codes, or compile it to see the errors, and suggest how to fix them.