comparison logic!!

C

curiousEngine

Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.


//TO DO RIGOUROUS TESTING

#include<iostream>
using namespace std;

void main(){
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >> A >> B >> C;

if (A > B){
if (A > C)
max = A;
} else {
if (B > C)
max = B;
else if (C > A)
max = C;
}


if (A < B){
if (A < C)
min = A;
} else {
if (B < C)
min = C;
else if (C < A)
min = C;
}


cout <<"Max integer: "<<max<<endl;
cout <<"Min integer: "<<min<<endl;

}
 
M

Markus Moll

Hi
Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.

So what do you want us to do?
//TO DO RIGOUROUS TESTING

That's a joke, right?
#include<iostream>
using namespace std;

void main(){
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >> A >> B >> C;

if (A > B){
if (A > C)
max = A;
} else {
if (B > C)
max = B;
else if (C > A)
max = C;
}

You have a problem. One of your if's is missing an else-branch. So either
one of your conditions is always true or there is a path where no maximum
element is found. Try drawing three elements and your knowledge about them
in a diagram where when A < B then A is located lower than B on your paper
and there's a line connecting them. This helps you to find the necessary
comparisons. (And it has nothing to do with C++)

Markus
 
A

Anony Mouse

Markus said:
Hi


So what do you want us to do?


That's a joke, right?


You have a problem. One of your if's is missing an else-branch. So either
one of your conditions is always true or there is a path where no maximum
element is found. Try drawing three elements and your knowledge about them
in a diagram where when A < B then A is located lower than B on your paper
and there's a line connecting them. This helps you to find the necessary
comparisons. (And it has nothing to do with C++)

Markus

Actually, that doesn't matter as 'if (A > C)' is only called when 'if (A
> B) returns true, and if it doesn't, the line is skipped either way.

I'm not at my home comp right now, so I can't mess around with some code
myself, but I played all combinations I could think of through in my
head, and that piece of code always seems to set the right result.

What I don't get, though:
You could either make the code more readable by using two ifs per
result. Not really more efficient, but a lot easier to play through in
your head.

e.g.

if (A > B) {
if (A > C)
max = A; }


else if (B > A) {
if (B > C)
max = B; }


else if (C > A) {
if (C > B)
max = C; }

Not that much slower. True, yours is more efficient, but really gives
one a headache when one tries to follow it in ones head.

What I also noticed, your int min(0) is currently unused.

Thing with either of the solutions is, both handle only three integers.
Why not use an array and a sorting loop? I like flexibility~
 
R

red floyd

curiousEngine said:
Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.


//TO DO RIGOUROUS TESTING

#include<iostream>
using namespace std;

void main(){

At this point, you have an ill-formed program and anything can happen,
including your compiler not accepting it.

main() returns int. Period.
 
A

Anony Mouse

Anony said:
What I also noticed, your int min(0) is currently unused.

Woops. That's what I get for only reading Markus' quote. Same thing for
that one, though.
 
P

Puppet_Sock

Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.

//TO DO RIGOUROUS TESTING

#include<iostream>
using namespace std;

Try to avoid this. It dumps everything in std into
your namespace, and basically defeats the purpose of
having the std namespace in the first place. Learn to
use the std:: decorator.
void main(){

Don't use void main. Using void main allows the Vorlons
to contact the Foul Marmidons. And nobody wants that.
Not even the Vorlons.
int A(0), B(0), C(0);
int max(0), min(0);

cout<<"Enter 3 integers:";
cin >> A >> B >> C;

Learn to use white space to make stuff readable.

cout << "Enter 3 integers: ";
if (A > B){
if (A > C)
max = A;
} else {
if (B > C)
max = B;
else if (C > A)
max = C;
}

This point is pretty personal, and lots of people
will disagree with me. But I like my { and } to line
up so as to make things readable. Plus, I usually put
in the braces for if statements, even when it's only
one statement and they are not needed.

if (A > B)
{
if (A > C)
{
max = A;
}
}
else
{
if (B > C)
{
max = B;
}
else if(C > A)
{
max = C;
}
}

Lots of people prefer how you wrote it to
how I wrote it. This is a "religious" issue
that many people get very upset over. Usually
it's fine either way as long as you stick to it.

It's a lot easier to understand this if you
do max as follows.

max = A;
if(B > max)
{
max = B;
}
if(C > max)
{
max = C;
}

This way it is more clear that you have covered
every possible case of relative sizes, and it
uses, at worst, the same number of tests. It is
also a lot easier to add more variables. It's
even very easy to do it for a collection.
Socks
 
P

Pete Becker

Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.

Start out by setting the maximum value to the first value. Then look at
the second value, and if its greater than the maximum so far, store its
value in the maximum. Then move on to the third. This has the advantage
of working for any number of values, not just three. And its much
simpler and clearer than all those messy if statements.
 
J

James Kanze

On 2007-10-12 05:51:42 -1000, curiousEngine <[email protected]> said:
Start out by setting the maximum value to the first value.
Then look at the second value, and if its greater than the
maximum so far, store its value in the maximum. Then move on
to the third. This has the advantage of working for any number
of values, not just three. And its much simpler and clearer
than all those messy if statements.

In fact, the best solution is to get rid of the if's entirely:

std::cout << "max is: " << std::max( std::max( A, B (, C ) ))
std::endl ;
std::cout << "min is: " << std::min( std::min( A, B (, C ) ))
std::endl ;

More generally, I'd put the values in an array (std::vector),
and use std::max_element.
 
J

Juha Nieminen

curiousEngine said:
if (A > B){
if (A > C)
max = A;
} else {
if (B > C)
max = B;
else if (C > A)
max = C;
}

If you want to do your homework in a bit more obfuscated way, you can
do that with a one-liner:

max = A > B ? A > C ? A : C : B > C ? B : C;
 
P

Puppet_Sock

If you want to do your homework in a bit more obfuscated way, you can
do that with a one-liner:

max = A > B ? A > C ? A : C : B > C ? B : C;

I compiled this, with appropriate int main etc.
added to make a complete program. It printed out
the words to Aida.
Socks
 
J

Juha Nieminen

Puppet_Sock said:
I compiled this, with appropriate int main etc.
added to make a complete program. It printed out
the words to Aida.

Are you trying to imply that the standard doesn't define precedence
rules for nested ?: operators?

I suppose you could make it more unambiguous by using clarifying
parentheses, like:

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

However, are they really necessary (except for the human reader)?
 
M

Markus Moll

Hi

Anony said:
Actually, that doesn't matter as 'if (A > C)' is only called when 'if (A

Um... I am a bit late, but above code does certainly _not_ work for A > B
but A < C (e.g. A=2, B=1, C=3). I'm not sure what you're trying to say.
I played all combinations I could think of through in my head, and that
piece of code always seems to set the right result.

There are only 3! = 6 to check. You missed the important one.

Markus
 
M

Michael DOUBEZ

curiousEngine a écrit :
Write a program that allows you to input 3 integer values A, B and C
and output the largest of the 3 values.


//TO DO RIGOUROUS TESTING

[snip: logic]

If fact, the easiest way is to perform the comparison at read time:

cout<<"Enter 3 integers:";

// number of integer to read
const int nb_read=3;
// computed values
int max_value(std::numeric_limits<int>::min());
int min_value(std::numeric_limits<int>::max());

for(int i=0;i<nb_read;++i)
{
int value;
if(cin>>value)
{
if(value>max_value)
{
max_value=value;
}
if(value<min_value)
{
min_value=value;
}
}
//else do something with invalid input data
}

cout <<"Max integer: "<<max_value<<endl;
cout <<"Min integer: "<<min_value<<endl;



Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top