Thanx in advance
Greatest of two numbers without using comparison operators..How to find greatest of 2 numbers without using any comparison operato
36 Replies - 28688 Views - Last Post: 24 December 2008 - 07:47 AM
#1
Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 01:34 AM
Thanx in advance
Replies To: Greatest of two numbers without using comparison operators..
#2
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 01:51 AM
#3
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 03:13 AM
http://www.dreaminco...showtopic=43479
This post has been edited by Bench: 20 December 2008 - 03:21 AM
#4
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 04:39 AM
Please post like this:
Thank you for helping us helping you.
--------
What is the definition of "comparison operator" that applies to this challenge?
I am assuming the relational operators " < ", " > ", " <= " and " >= " can't be used.
Can the equality operators " == " and " != " be used?
What is the definition of "number" that applies to this challenge?
Are negative values allowed?
How about zero?
----
OK I have my code without any use of " < ", " > ", " <= ", " >= ", " == " or " != ".
Where's yours so we can compare?
This post has been edited by janotte: 20 December 2008 - 05:46 AM
#5
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 06:25 AM
siddude, on 20 Dec, 2008 - 12:51 AM, said:
My code is
main()
{
int a,b,c;
cin>>a>>b;
c=a-b;
if(c>0)
cout<<"a is the biggest";
elseif(c<0)
cout<<"b is the biggest";
elseif(c==0)
cout<<"Both r equal"
}
But the problem is, here also, comparison operators r used
janotte, on 20 Dec, 2008 - 03:39 AM, said:
Please post like this:
Thank you for helping us helping you.
--------
What is the definition of "comparison operator" that applies to this challenge?
I am assuming the relational operators " < ", " > ", " <= " and " >= " can't be used.
Can the equality operators " == " and " != " be used?
What is the definition of "number" that applies to this challenge?
Are negative values allowed?
How about zero?
----
OK I have my code without any use of " < ", " > ", " <= ", " >= ", " == " or " != ".
Where's yours so we can compare?
#6
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 06:46 AM
That kinda went without saying.
Bit sad you have nothing better to offer so far.
You set the challenge.
Now show us what you can do.
I've got an answer.
Where's yours?
Just to prove I'm not lying here's the start of my code (with some snipped out because it was too much of a clue):
/* Challenge Code **********************************
** Determine the greater of 2 numbers without use **
** of comparison operators **
*****************************************************/
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char NEG_1[2] = "-";
cout << endl << "Enter the first number: ";
int x;
cin >> x;
cout << endl << "Enter the second number: ";
int y;
cin >> y;
int z = x - y;
// <snip>
cout << endl << "The two numbers are equal." << endl << endl;
//...
#7
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 10:17 AM
Please give me a clue.. I ll develop the code
#8
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 10:20 AM
If 'b' is greater then 'a' then 'a' minus 'b' would be negative. If they are equal then it would be zero. So on and so forth.
edited because i double posted for some reason
This post has been edited by KYA: 20 December 2008 - 10:21 AM
#9
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 04:05 PM
KYA, on 20 Dec, 2008 - 09:20 AM, said:
If 'b' is greater then 'a' then 'a' minus 'b' would be negative. If they are equal then it would be zero. So on and so forth.
edited because i double posted for some reason
KYA is giving you the same clue that this line in my code gives you:
char NEG_1[2] = "-";
about how I did it.
Now my answer (at this point) is only good for two numbers greater than zero but I could generalise it further if I cared enough (which I don't at this point).
No need to follow my path.
Find one of your own.
This is a logic puzzle based on what you know about numbers and C++ (or C).
There is no special coding knowledge required just some lateral thinking.
Because I suspect this may be for school I'm not giving you the answer now.
When is the assignment due?
I might share a week after that (maybe).
#10
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 09:21 PM
#11
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 09:27 PM
Greatest of two numbers without using comparison operators
shows some pretty relevant references.
When your post simply says "Thanx in advance", it's pretty offensive. It's not even like you are asking for help, just demanding the results, & oh by the way, thanks. Dream In Code is not the fast food coding site of the internet. You don't place an order & then pick up at the end of the bar.
#12
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 09:34 PM
c=a/b; if(c!=0) cout<<"a is the biggest"; else cout<<"b is the biggest";
but this doesnt work for negativ values
Excuse me No2pencil.. Its not that way. I dont need the code. I just need a clue about the logic..
#13
Re: Greatest of two numbers without using comparison operators..
Posted 20 December 2008 - 11:38 PM
suganthi14, on 20 Dec, 2008 - 08:34 PM, said:
if(c!=0)
<snip>
You can't use any of these:
" < ", " > ", " <= ", " >= ", " == " or " != "
If you include them there is no point in posting your code because it must be wrong.
You have to think in a different way.
Can you do it?
Here is a huge hint
(obscured somewhat but the hint is there if you are able to think like a programmer)
while(true)
---
BTW - What is a "viva question"?
Just asking out of interest because I have no idea.
This post has been edited by janotte: 21 December 2008 - 12:25 AM
#14
Re: Greatest of two numbers without using comparison operators..
Posted 21 December 2008 - 02:49 AM
c=a/b; if(c) cout<<a is the biggest; else cout<<b is the biggest;
I think this may be correct..
janotte, on 20 Dec, 2008 - 10:38 PM, said:
suganthi14, on 20 Dec, 2008 - 08:34 PM, said:
if(c!=0)
<snip>
You can't use any of these:
" < ", " > ", " <= ", " >= ", " == " or " != "
If you include them there is no point in posting your code because it must be wrong.
You have to think in a different way.
Can you do it?
Here is a huge hint
(obscured somewhat but the hint is there if you are able to think like a programmer)
while(true)
---
BTW - What is a "viva question"?
Just asking out of interest because I have no idea.
#15
Re: Greatest of two numbers without using comparison operators..
Posted 21 December 2008 - 03:08 AM
suganthi14, on 21 Dec, 2008 - 01:49 AM, said:
Ah yes - of course.
Sorry, I was being dense.
suganthi14, on 21 Dec, 2008 - 01:49 AM, said:
c=a/b; if(c) cout<<a is the biggest; else cout<<b is the biggest;
You have made the breakthrough with this line:
if(c)
but this:
c=a/b;
is terrible thinking.
Try compiling and running your code (wrapped in the appropriate support code) and see what it does.
Think about what this:
if(c)
does if c is:
- zero
- greater than zero
- less than zero
(or if you can't remember how an if() statement works just try various values of c in a simple program to see what if© does).
But be of good heart you have made the first major breakthrough.
Just thinking about what you have found out will allow you to mostly solve this.
You still need to make another breakthrough but the one you have made will get you 70% of the way to the solution.
|
|

New Topic/Question
Reply



MultiQuote





|