473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

comparison logic!!

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;

}

Oct 12 '07 #1
12 1445
Hi

curiousEngine wrote:
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

Oct 12 '07 #2
Markus Moll wrote:
Hi

curiousEngine wrote:
>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
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~
Oct 12 '07 #3
curiousEngine wrote:
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.
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;

}
Oct 12 '07 #4
Anony Mouse wrote:
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.
Oct 12 '07 #5
On Oct 12, 11:51 am, curiousEngine <curious.eng...@gmail.comwrote:
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

Oct 12 '07 #6
On 2007-10-12 05:51:42 -1000, curiousEngine <cu************@gmail.comsaid:
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 12 '07 #7
On Oct 12, 8:01 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-10-12 05:51:42 -1000, curiousEngine <curious.eng...@gmail.comsaid:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 14 '07 #8
curiousEngine wrote:
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;
Oct 15 '07 #9
On Oct 15, 1:01 pm, Juha Nieminen <nos...@thanks.invalidwrote:
[snip]
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

Oct 15 '07 #10
Puppet_Sock wrote:
On Oct 15, 1:01 pm, Juha Nieminen <nos...@thanks.invalidwrote:
[snip]
> 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.
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)?
Oct 15 '07 #11
Hi

Anony Mouse wrote:
Markus Moll wrote:
>You have a problem. One of your if's is missing an else-branch.

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

Oct 16 '07 #12
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
Oct 16 '07 #13

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

Similar topics

6
by: F. Petitjean | last post by:
I want to know if iter(iterator) returns always its argument (when argument is an iterator) So : >>> iterable = range(10) >>> it = iter(iterable) >>> that = iter(it) >>> that is it True #...
8
by: Ben Fidge | last post by:
I have a requirement to compare two lists, generating a third which is a combination of both. With the first list as the reference list and the second as the test list, I want the each of the...
4
by: bstoddart | last post by:
I've been working on a project for the last week and I can't seem to get it working. It's supposed to be finished within the next few days so a speedy reply would be appriciated. Anyways, it seems...
24
by: cassetti | last post by:
Here's the issue: I have roughly 20 MS excel spreadsheets, each row contains a record. These records were hand entered by people in call centers. The problem is, there can and are duplicate...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
6
by: Jeff Gardner | last post by:
Greetings: I am attempting to get conditional output based on POSTed form data. If the posted value is either the key or value of an array, $x=key and $q=foo. elseif it is neither key nor...
3
by: chazzy69 | last post by:
Hello i seem to have trouble with comparing two chars e.g. This works if (char == 'z') but when i try if (char != 'z') it always returns false. Heres the actual line of code- while...
10
by: lilly07 | last post by:
Hi, I have one column of strings in 1st file file and another file which consists of 5 clumns in each line and my basic objective is to find each item/line of 1st file is available in 3rd column of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.