I have been trying VERY hard on this one and I cannot figure it out (I get the logic-- but not how to apply it in a COMPUTER).

Here it is (please read the whole thing before answering):

This is C programming (not C++)
I need to design a program that will find the highest and lowest number of any 4 integers.

3 catches--

1) It has to be a cascade of "if" and "else"
2) I can only use a total of 4 "if"s (elses are not limited)
3) It has to be very very basic (this is only the 2nd week of class)

What I did-- which worked with LOGIC-- was this:

If ((A>B && A>C && A>D) || (A<B && A<C && A<D))

.... and so on, in a process of elimination. (where I ran into trouble was with the number of "if"s. I was only able to get down to 5, using this method).

(beginning with a comparison of A to B and C to D didn't work because it required that the max be in the AB group and the min be in the CD group, or vice versa (you couldn't have max AND min in the AB group)--- remember, I am only allowed 4 "if"s.

Anyone have a solution? I have been working for 2 days and cannot get it. I tried my best. But part of life is knowing when to ask for help (and the teacher is cool, but he won't help)........

I would be grateful : )

Recommended Answers

All 51 Replies

Can you use the ternary operator, like this?

(A > B) && (A > C) && (A > D) ? max = A: ;

You could put the elements into an array, sort them using loops (and less than four if statements) and then you would know they're sorted, so the max is at one end of the array and the min at the other.

I think so.

Well, I know I can use "max" and the "?" because I read them in the book. I don't know about the : (what does that thing mean?)

I was wondering if the "max" and "min" are already programmed into codeblocks.... like if I enter them, will codeblocks know what it is?

I considered trying to use those...... but I really have no idea what to do with them (beyond that the ? can question something?? Like if it is == or whatever).

Oh-- and no loops. The teacher would know I had help!! (I'm going to have to tell him anyway, but I still want to keep it realistic-- like in stuff that he has shown us how to do).

The problem with finding whether "max = A" is that there can only be 4 'if's ..... like if max does NOT = A, does max = B?? You would have to go on like this several times, ending with "does max = C?; else max = D).

Then you run out of "if"s (that's 3) before even getting to the min. (an 'else if' counts as an 'if', but regular 'else' is not limited).

I tried a series of steps:

If ((A>B && A>C && A>D) || (A<B && A<C && AD))

.... but that didn't work for 2 reasons: 1) I am left with too many variables (XX, YY, ZZ), that I run out of 'if's, and 2) if I tweak it just right to account for everything, it's still 5 'if's

That doesn't work. Read my statement on why in the original post.

For that to work your Min has to be in the AB group while your max is in the CD group (or vice versa). That method ALWAYS fails when min and max are both in the AB (or both in the CD) groups.

It's bad math. I probably could've fooled the teacher with that one by lining up the numbers just right, but it's too late for that..... I already showed off my math and logic skill debunking it (drat!).

Code::Blocks is just the tool you're using. If it does anything for you that isn't part of standard C, try not to use that - you won't be doing youself any favours. As I recall, in terms of a compiler, it is little more than a nice wrapper around the MinGW compiler anyway, so it won't do much extra for you. It is very pleasing, by the way, to know that you're not trying to use Turbo C++ from twenty years ago.

If you can use ?, this becomes easy.

The use of it looks like this:

(some statement) ? (this if the statement is true) : (this if it's not true);

If (some statement) is true, then (this if the statement is true) is executed (and returned), otherwise (this if it's not true) is executed (and returned).

So, if you were comparing just three numbers to find if A is the maximum, you could have (some statement) as your test of the value,(this if the statement is true) as your setting something because you found the maximum, and (this if it's not true) left empty because you haven;t found the maximum. Here's an example:

((A > B) && (A > C)) ? max = A : ;

Does this work for 4 numbers and only 4 "if"s? ("else if" counts as an if).

Oh--- and that little symbol you are using.... is that the : colon, or the || (or)?

(not to mention.... is that even how you type "or" on C || ? )

Oh... actually, now that I take a closer look-- I tried basically that same method (but written differently), but it's too many "if"s.... I was only able to get down to 5.

It's a colon, exactly as this:
((A > B) && (A > C)) ? max = A : 0 ;

If A is bigger than B and bigger than C, max gets set to A. Otherwise, nothing is done (and in this case, I return 0, but that's not doing anything.

Yeah-- I already did that, just written differently. No way to get it down to 4 "if"s.

I need both min AND max, with only 4 "if"s (has to be cascading "if"s)

(that is the best thing so far-- just not quite right)

are you allowed to use for and while loops along with the if loops

You can do it with zero ifs using the ? operator.

It can't be disguised "if"s that are still the same thing

a ? is still an "if"-- serves the same function; if this, then that; otherwise this.


no loops-- too obvious it isn't strictly my work. No whiles or for or whatever (has to be in the first 5 chapters of KN King "C Programming" textbook.

Anyone have a teacher's edition? Maybe there is a solution?

BTW-- switch function is OK, but still only 4 steps (I have not yet used switch).

the ? is just another way to do if and no disguised its takes out using while and for as makeshift ifs

the ? is just another way to do if

Every decision the machine ever makes is just another way to do if. Are the rules are "you can't make more than four decisions" or are they "you cannot have the word if in more than four places in your code"?

As an aside, exactly this discussion - what are the actual requirements? - is one of the most poorly done parts of every software project I've ever worked on, and the majority of the world's failed software projects can generally be traced to not having clear requirements at the start.

uh.... yeah, I think that's right.

No disguised "if's". Still has to be 4 steps.

There MUST be a solution...... I just can't find it through math or logic because it has to go into the computer in a certain way (all of my logical solutions failed when bound by programming).

Well, it is supposed to be a cascade of 'if's and 'else" (where 'else if' counts as an 'if').... because that is what we have done in the textbook so far

If other words or functions were used it probably would not be a problem, so long as they were very basic and simple (and believably mine).

But yes, only 4 steps, regardless of the word "if" (it is the FUNCTION that really matters, not the word)

So the rules are "You MUST use four if statements in your code, and you may not use for, or while, or do-while"?

kvothetech's link and solution is actually correct.
It involves four (if-else) pairs.

There is nothing about "while or "for" or "do while" in the assignment.

But it is very clear that there must only be 4 decisions (4 or less).

I've gone at this and back again in a zillion ways (and I am a champion puzzle solver).....

I am starting to think the assignment is flawed, and that there is no real solution.....

That most people do this:

if (A>B)
W1 = A
L1 = B
W1 = B
L1 =A

if (C>D)
W2 = C
L2 = D
W2 = D
L2 = C

If (W1>W2)
X = W1
X = W2

if (L1 > L2)
Y = L2 (the loser of the losers; the smallest #)
Y = L1

Printf ("your biggest number is %d and your smallest number is %d", X, Y)

That is the solution most people come up with (including the teacher), but it is WRONG because it requires the winner to be in the AB and the loser to be in the CD group (or vice versa)..... winner and loser cannot both be in AB (or CD).

It's just bad math -- fails when loser and winner are side by side)

I think there is NO solution at all..... that everyone thinks this IS the solution..... and since it is completely WRONG (and I really shouldn't have debunked it to the teacher), then maybe there is no solution at all??

I think that is the same one as above, and it's wrong Put your winner and loser side by side and see....

Min and Max can be both at AB (or CD). What are you talking about? Can you explain why you think otherwise using logic?

No. Program it (the one I just wrote above).

I came up with that one too, just like everyone else...

Every time I put that into my codeblocks it doesn't work, and the reason why is because you are comparing A and B, C and D, then the winners and losers of the 2 groups...... never establishing that the winner in one group and the loser in the other.

Just try it.... try 1 2 3 4, try 2, 3 , 4, 1 try 3, 4, 1, 2 try 4, 1, 2, 3 one or more are bound to fail.

whoops-- just screwed up my own point (possibly)

Try 1 4 3 2

Try 3 2 4 1

Just keep trying until you get a fail. I promise you WILL

Okay... logic reasoning here..

Suppose you have a, b, c and d

group those into 2 pairs, ab and cd

if a is greater than b, a cannot be the minimum anymore because it is greater than something, whereas b cannot be maximum anymore.

if c is greater than d, c cannot be minimum and d cannot be max.

You don't have to compare b or d to anything else to get max. So go with the winners a and c.
You don't have to compare a or c to anything else to get min. So go with the losers b and d.


Just DO it. After you see it with your own eyes, you will be able to understand what I am saying better.

I KNOW it seems to make sense logically. I got the exact same answer---- it is only after it continually FAILED that I went looking for a reason.

Just TRY it.

(boy am I going to feel like an idiot if there is some reason it's not working)

But just TRY it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.