Greatest of two numbers without using comparison operators..

How to find greatest of 2 numbers without using any comparison operato

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 39266 Views - Last Post: 24 December 2008 - 07:47 AM Rate Topic: -----

#1 suganthi14  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-December 08

Greatest of two numbers without using comparison operators..

Post icon  Posted 20 December 2008 - 01:34 AM

:blink:
Thanx in advance
Is This A Good Question/Topic? 0
  • +

Replies To: Greatest of two numbers without using comparison operators..

#2 siddude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-December 08

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 01:51 AM

View Postsuganthi14, on 20 Dec, 2008 - 12:34 AM, said:

:blink:
Thanx in advance



void great(int a,int b)
{
int c;
c=a-b;
if(c>0)
{
cout<<"a is greater then b"<<endl;
}
else
cout<<"b is greater then a"<<endl;
}




i hope comparison operator can be used in this way,if not let me know
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 03:13 AM

You might find this thread interesting (The discussion is on essentially the same subject)
http://www.dreaminco...showtopic=43479

This post has been edited by Bench: 20 December 2008 - 03:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 04:39 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

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

Was This Post Helpful? 0
  • +
  • -

#5 suganthi14  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-December 08

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 06:25 AM

Here too the comparison operator is used na..? I too gave the same answer when I was asked. But its wrong :blink:

View Postsiddude, on 20 Dec, 2008 - 12:51 AM, said:

View Postsuganthi14, on 20 Dec, 2008 - 12:34 AM, said:

:blink:
Thanx in advance



void great(int a,int b)
{
int c;
c=a-b;
if(c>0)
{
cout<<"a is greater then b"<<endl;
}
else
cout<<"b is greater then a"<<endl;
}




i hope comparison operator can be used in this way,if not let me know



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

View Postjanotte, on 20 Dec, 2008 - 03:39 AM, said:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

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?

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 06:46 AM

Well it was obvious that siddude's solution was wrong.
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;
//...


Was This Post Helpful? 0
  • +
  • -

#7 suganthi14  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-December 08

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 10:17 AM

I cant get the logic.
Please give me a clue.. I ll develop the code
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 10:20 AM

Let's say we have two numbers 'a' and 'b'.

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

Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 04:05 PM

View PostKYA, on 20 Dec, 2008 - 09:20 AM, said:

Let's say we have two numbers 'a' and 'b'.

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).
Was This Post Helpful? 0
  • +
  • -

#10 suganthi14  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-December 08

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 09:21 PM

Its not assignment. It was asked as one of my viva questions.. I answered everythin but this! So the due date is over.
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 09:27 PM

Doing a Google search for the phrase :

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.
Was This Post Helpful? 0
  • +
  • -

#12 suganthi14  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-December 08

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 :blink:

Excuse me No2pencil.. Its not that way. I dont need the code. I just need a clue about the logic.. :/
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 11:38 PM

View Postsuganthi14, on 20 Dec, 2008 - 08:34 PM, said:

<snip>
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

Was This Post Helpful? 0
  • +
  • -

#14 suganthi14  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 13-December 08

Re: Greatest of two numbers without using comparison operators..

Posted 21 December 2008 - 02:49 AM

viva voce refers to oral questions asked by the external examiner during the practical exam..

c=a/b;
if(c)
cout<<a is the biggest;
else
cout<<b is the biggest;



I think this may be correct..

View Postjanotte, on 20 Dec, 2008 - 10:38 PM, said:

View Postsuganthi14, on 20 Dec, 2008 - 08:34 PM, said:

<snip>
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.

Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Greatest of two numbers without using comparison operators..

Posted 21 December 2008 - 03:08 AM

View Postsuganthi14, on 21 Dec, 2008 - 01:49 AM, said:

viva voce refers to oral questions asked by the external examiner during the practical exam..

Ah yes - of course.
Sorry, I was being dense.

View Postsuganthi14, on 21 Dec, 2008 - 01:49 AM, said:

I think this may be correct..
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.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3