473,796 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing two numbers

The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy? huh
here's the catch
do not use 'if' or any conditional statements
if u want it to be a little more tougher you can use the if but this time
no relational operators or any of the predefined functions....

Can someone please help me solve the problem....

Nov 14 '05 #1
89 3471
purifier <cn******@gmail .com> scribbled the following:
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy? huh
here's the catch
do not use 'if' or any conditional statements
if u want it to be a little more tougher you can use the if but this time
no relational operators or any of the predefined functions.... Can someone please help me solve the problem....


How about (1) doing your homework and (2) speaking English instead of
d00dsp33k?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #2

"purifier" <cn******@gmail .com> wrote in message
news:19******** *************** *******@localho st.talkaboutpro gramming.com...
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy?
Yup.
huh
here's the catch
do not use 'if' or any conditional statements
Still easy.
if u want it to be a little more tougher you can use the if but this time
no relational operators or any of the predefined functions....
Still easy.
Can someone please help me solve the problem....


OK, I have the solutions all ready to go.

All I need now is your instructor's email
address, and I'll send them off.

-Mike
Nov 14 '05 #3

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:rw******** *********@newsr ead3.news.pas.e arthlink.net...

"purifier" <cn******@gmail .com> wrote in message

news:19******** *************** *******@localho st.talkaboutpro gramming.com...
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy?


OK, here's a hint for the first form:
0 evaluates to false, 1 evaluates to true.

BTW did your assignment specify whether the inputs
could be equal? If so, what should the result be?

-Mike
Nov 14 '05 #4
"purifier" <cn******@gmail .com> writes:
The problem is to write a program in 'C' to find the greatest of 2 given
numbers... Easy? huh
here's the catch
do not use 'if' or any conditional statements
if u want it to be a little more tougher you can use the if but this time
no relational operators or any of the predefined functions....

Can someone please help me solve the problem....


Q: How do I drive a screw without using a screwdriver?

A: You don't. Use a screwdriver. That's what they're for.

Now if you actually have some legitimate reason to drive a screw
without using a screwdriver, someone might be able to come up with a
method -- but nobody is going to help you unless you tell them *why*
you want to do such a silly thing, and why your first question isn't
"Where can I find a screwdriver?".

It's the same thing with your question. Without any background, it's
not particularly interesting. If the background is that it's a
homework question, we're not going to do your homework for you. If
it's just a fun little quiz, some of us might be interested, but you
need to say so.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
purifier wrote:
The problem is to write a program in 'C' to find the greatest
of 2 given numbers... Easy? huh
here's the catch
do not use 'if' or any conditional statements
if u want it to be a little more tougher you can use the if but
this time no relational operators or any of the predefined
functions....

Can someone please help me solve the problem....


I read a similar (but more restricted) question very recently
in another forum.

The following uses strtol() to convert arguments on the command
line, and printf() to output the maximum. Otherwise, it doesn't
use any conditional, logical, equality or relational operators,
nor does it use any selection or iteration statements.

Not sure if Google will mangle this...

% type max2.c
typedef unsigned long q1;int printf(const char*,...);long
strtol(const char*,char**,in t);q1 q2(q1 q3,q1 q4,q1 q5,q1
q6){return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-
q3))+1))-1)+q6;}q1 q7(long q8){return q2(q8,0,q2(q8/2,0,1
,0),0);}q1 q9(long q8){return q2(q8,-1,q2(q8/2,-1,1,q2(q8
/2,0,1,0) ),0);}q1 q10(long);q1 q11(long q8){return 0;}q1
q12(long q8){return 1;}q1 q13(long q8){return q10(q8/2);}
q1 q14(long q8){q1(*q15[])(long)={q12,q1 3};return q15[q2(
q9(q8),1,0,1)](q8);}q1 q10(long q8){q1(*q15[])(long)={q11
,q14};return q15[ q2(q7(q8),1,0 ,1)](q8);}q1 q17(long q3,
long q4){return q2(q10( q3),1,q2(q10(q4 ),1,q10(q3-q4),1),
q2(q10(q4),1 ,0,q10(q3-q4 )));}long q18(long q3,long q4){
return q3;}long q19(long q3,long q4){return q4;}long q20(
long q3,long q4){long(*q15[])(long,long)={q 19,q18};return
q15[q2(q17(q3,q4),1 ,0,1)](q3,q4);}long q21(const char*q2)
{return strtol(q2,0,10) ;}int q22(char **q23){printf(" %ld"
"\n",q20(q21(q2 3[1]),q21(q23[2])));return 0;}int q24(char
**q23){return 0;}int main(int q25,char**q23){ int(*q15[])(
char**)={q24,q2 2};return q15[q2(q25,3,1,0)](q23);}

% gcc -ansi -pedantic max2.c -o max2.exe

% max2 -5 42
42

%

--
Peter

Nov 14 '05 #6
It's a real sad thing to know, Sir... I didn't expect that my post would
make you think that it was my homework... I'm an Electronics Engineer, but
i'm interested a lot in programming... One of my friends asked me this
question in a mail. I just pasted that mail here... For your information i
even said "Can you please help me solve the problem" Why would I say huh
and all kinds of stuff and then ask people to solve the problem? I could
have directly given the problem...
Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
if possible, please give me some ideas...
Thank You

Nov 14 '05 #7
On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <ai***@acay.com .au>
wrote:

[...]
The following uses strtol() to convert arguments on the command
line, and printf() to output the maximum. Otherwise, it doesn't
use any conditional, logical, equality or relational operators,
nor does it use any selection or iteration statements.
[...]typedef unsigned long q1;
q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
{
return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;
}


I don't think that's portable, namely the use of the bitwise operator:

( q3 - q4 ) & ( q4 - q3 )

It seems to me that it assumes twos compliment and could give the
wrong answer on other architectures.

Nick.

Nov 14 '05 #8

"purifier" <cn******@gmail .com> wrote in message
news:8a******** *************** *******@localho st.talkaboutpro gramming.com...
It's a real sad thing to know, Sir... I didn't expect that my post would
make you think that it was my homework... I'm an Electronics Engineer, but
i'm interested a lot in programming... One of my friends asked me this
question in a mail. I just pasted that mail here... For your information i
even said "Can you please help me solve the problem" Why would I say huh
and all kinds of stuff and then ask people to solve the problem? I could
have directly given the problem...
Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
if possible, please give me some ideas...


I gave you one good clue already:

0 evaluates to false, 1 evalutates to true.

Another:

0 + x == x

-Mike
Nov 14 '05 #9
purifier wrote:

It's a real sad thing to know, Sir... I didn't expect that my post would
make you think that it was my homework... I'm an Electronics Engineer, but
i'm interested a lot in programming...
Then you shouldn't have any trouble deriving a solution yourself.
Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
if possible, please give me some ideas...


Here are some hints: in C, you can turn an expression into a 0 or a 1
using !!(expression), and you can put "first number is bigger" and
"second number is bigger" into an array like this:

char *bigger[] =
{
"first number is bigger",
"second number is bigger"
};

You can demonstrate equality as a separate step (possibly a preceding
step):

char *equality[] =
{
"numbers are equal",
"numbers are not equal"
};

Here's another hint: you can turn a number into a string using
sprintf, you can compare two strings for equality using strcmp,
which rather conveniently returns 0 when the two strings are
equal. !!strcmp, of course, returns either 0 or 1.

Here's yet another hint: you are not limited to storing your
original two numbers in strings. You can also store their
difference. If they weren't equal, one will be bigger than
the other. If the second is bigger than the first, the result
of a subtraction will be negative. Detecting a minus sign
in the first byte of a string is trivial.

If an electronic engineer can't get it with all those hints,
there's something wrong with the awards process.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1848
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends, other.extends) instead of
11
2395
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different compiler and got the same erroneous result on two different PCs (with OS Win98 & Win98SE), so it appears to be a problem with ANSI C. I thought that negative double variables could be compared as easily and *reliably* as integers, but apparently...
12
6861
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a))) Do the same issues involved in comparing 2 fp types for equality apply to comparing a float to zero? E.g. is if(x == 0.0) considered harmful?
2
3390
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
14
2515
by: nw | last post by:
Hi, I'd like to compare 2 floating point numbers within a given error. I'd rather not use a absolute error but one related to the number of values that can be represented between the two floats. I've been reading: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm where the following function is provided to do this: bool AlmostEqual2sComplement(float A, float B, int maxUlps) { // Make sure maxUlps is...
0
1098
by: shana07 | last post by:
I need help on comparing lines in my output file... How to delete the second line in text file (FileReader) and print out the first line for the same fileno? I put here sample of my output file: 1 : 30 at 2 1 : 134 at 10 2 : 35 at 2 4 : 30 at 2 4 : 80 at 8
27
3871
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually fails if comparing doubles of different magnitude since it's hard or not possible to find a suitable threshold
18
4121
by: Carramba | last post by:
Hi! is there a better/faster way to compare mantissas of to real number then in following code? #include <stdio.h> #include <stdlib.h> int main(void) { float a,b; int test;
18
2895
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
5
4519
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
9684
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.