473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. if(num1 < num2 && num1 > num3)
  2.     cout << num1 << " is the median number." << endl;
  3. else if(num1 > num3 && num1 < num2)
  4.     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.
Feb 24 '08 #1
10 2302
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?
Feb 24 '08 #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?
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.
Feb 24 '08 #3
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?
Feb 24 '08 #4
Banfa
9,065 Expert Mod 8TB
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.
Feb 24 '08 #5
whodgson
542 512MB
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?
Feb 25 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. if (fabs(i - j) < 0.000001) { ... // almost equal }
  2.  
Feb 25 '08 #7
whodgson
542 512MB
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.
Feb 26 '08 #8
jhamb
18
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.
Expand|Select|Wrap|Line Numbers
  1.                 float a,b,c;
  2.                 cin>>a>>b>>c;
  3.     if(a>b&&b>c || a<b&&b<c)
  4.         {
  5.           cout<<"\n"<<b<<" is median\n";
  6.         }
  7.  
  8.     if(b>a&&a>c || b<a&&a<c)
  9.         {
  10.           cout<<"\n"<<a<<" is median\n";
  11.         }
  12.  
  13.     if(a>c&&c>b || a<c&&c<b)
  14.         {
  15.           cout<<"\n"<<c<<" is median\n";
  16.         }
  17.  
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. :)
Feb 26 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
And read Post #7 again.

Do not use the comparision operators directly with floating point. They are not reliable.
Feb 26 '08 #10
jhamb
18
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... :):)
Feb 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rookie | last post by:
I have done a lot of programming some time ago using Fortran and various varieties of the Basic programming languages, Hewlett Packard Technical Basic, QBasic, Quick Basic. With each of these...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
16
by: ranger | last post by:
Hi, I'm a beginner with C++, studying on my own from a book and I've encoutered quite some problems.. If possible, someone out there might help me out.. The problem is the following.. I've...
1
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
6
by: deejayquai | last post by:
Hi I'm attempting to append multiple values into a new record, using multiple criteria from a listbox. I've got the basics for the code below but I get an "Error 3085 Undefined Function" for...
2
by: ste | last post by:
VS2005, .NET2 I have a dll that is using TraceSources with no problems. The dll is linked to a webservice. The webservice creates its own traceSource and every thing looks fine.. however...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
0
by: giladbarner | last post by:
Hello everyone, I have office 2003 Professional, and I work in Access's Visual Basic Editor. Sometimes after opening the "Microsoft Visual Basic Help" window by standing on a function in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.