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

This is a discussion on can someone check my code and tell me what I'm doing wrong within the C++ Programming forums, part of the General Programming Boards category; I hd to write a total of 4 small programs that deal with writing function. I have two of them ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    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:

    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;
    }

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Hold up, I'm gonna re-write your first example and post the necessary revisions.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    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;
    
    }
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    I'm gonna re-write the sqaureroot one now. I hope you understand this.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    If this is for homework then I won't be writing the second one.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    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

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    I'm better at coding than explaining things. So that was my reason...
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21