Help with basic C problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wolvenmoon
    New Member
    • Feb 2008
    • 7

    Help with basic C problem

    Hello,

    I have an assignment due tomorrow that I've been trying to debug for a week. Google has failed me horribly at finding out what's going on.

    The problem is to "use functional decomposition to write a C++ program that determines the median of three input numbers..."

    So far the program that I've made will do this, but only with whole numbers. If I enter the floating point values that are supposed to be used I get nothing.

    This is in the chapter for conditions, logical expressions, and selection control structures, just to let you know where I am.

    Here's a snippet of my code. My declared floating point variables are num1, num2, and num3. The numbers are entered by the user via cin.

    Code:
    if(num1 < num2 && num1 > num3)
    	cout << num1 << " is the median number." << endl;
    else if(num1 > num3 && num1 < num2)
    	cout << num1 << " is the median number." << endl;
    I used to have that statement wrapped up with an || operator, but started breaking it down to see if that would work.
  • Simonius
    New Member
    • Feb 2008
    • 47

    #2
    Doesn't seem to cover the whole thing.

    Quick question, why don't you just toss em into a vector, sort it and print the middle number?

    Comment

    • Wolvenmoon
      New Member
      • Feb 2008
      • 7

      #3
      Originally posted by Simonius
      Doesn't seem to cover the whole thing.

      Quick question, why don't you just toss em into a vector, sort it and print the middle number?
      We haven't covered that yet in this class. This is a 'basics of' class.

      I don't want to post up my entire assignment, but it seems that if I use any floating point number, it just skips the entire if/else if structure and goes right to a catch all else at the end.

      Comment

      • Wolvenmoon
        New Member
        • Feb 2008
        • 7

        #4
        Okay, further debugging has found that it isn't floating point numbers causing the problem, but any numbers with a certain amount of significant digits.

        Any ideas?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I would guess it's probably in the way that you capture the data rather than the comparisons. Try using a debugger or printing all values just before the conditional statements.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            You may not have covered all the options.
            If 1<2<3 and you ask the questions:
            is 1<2&&3 you get yy
            is 2<1&&3 you get ny
            is 3<1&&2 you get nn
            If you change the position of the 3 numbers to cover all combinations the guy with the ny or yn is the median.
            Have the 3 numbers been declared as float types?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Generally, you cannot use operators like ==, !=, >, <, <=, >=, etc wth floating point numbers.
              Due to the automatic rounding, these operators may report a condition as true when the numbers
              are only close in value.

              Google for Floating Point Arithmetic or read IEEE 754 standard specification for details.

              To compare two floating point numbers, you have to establish a sigma error tolerance.

              Code:
              if (fabs(i - j) < 0.000001) { ... // almost equal }

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                There are six (6) conditions (3)! you have to cover like
                1. if(num1<num2 && num2<num3)cout< <"mean is num2"; or some such.
                assuming mean means the middle number in the set
                If you are only using floats to 1 or 2 decimal places rounding errors are unlikely to affect < or > comparisons.... .i think because i`m only a newbie.

                Comment

                • jhamb
                  New Member
                  • Feb 2008
                  • 18

                  #9
                  Hey, You can check the condition like if you are sorting numbers in order
                  Then second number will always be a median... :):)
                  I think, this would not get you any obstacle while using float numbers too.
                  But that code would be too lengthy for you..
                  I recommend the following:
                  Get 3 variables as a,b,c or change as you may like.
                  Code:
                                  float a,b,c;
                                  cin>>a>>b>>c;
                  	if(a>b&&b>c || a<b&&b<c)
                  		{
                  		  cout<<"\n"<<b<<" is median\n";
                  		}
                  
                  	if(b>a&&a>c || b<a&&a<c)
                  		{
                  		  cout<<"\n"<<a<<" is median\n";
                  		}
                  
                  	if(a>c&&c>b || a<c&&c<b)
                  		{
                  		  cout<<"\n"<<c<<" is median\n";
                  		}
                  If we are comparing two numbers, float or int, no matter which type they are, if condition will give the result 0 or 1 according to the numbers, not according to their types..! :)
                  So I guess, this will solve your problem...
                  Please let us know if you need any further details. :)

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    And read Post #7 again.

                    Do not use the comparision operators directly with floating point. They are not reliable.

                    Comment

                    • jhamb
                      New Member
                      • Feb 2008
                      • 18

                      #11
                      Originally posted by weaknessforcats
                      And read Post #7 again.

                      Do not use the comparision operators directly with floating point. They are not reliable.
                      Then can try one thing only.
                      To calculate the difference between numbers and check them if they are positive or negative... :):)

                      Comment

                      Working...