473,804 Members | 3,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
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<<end l;
cout <<"Min integer: "<<min<<end l;

}

Oct 12 '07 #1
12 1473
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<iostre am>
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<iostr eam>
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<iostre am>
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<<end l;
cout <<"Min integer: "<<min<<end l;

}
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<iostre am>
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...@versatile coding.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_elemen t.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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

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

Similar topics

6
1797
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 # Good! >>> that is it is not it
8
19796
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 result list's elements to contain a moniker denoting whether that element: 1..Exists only in the reference list or
4
1121
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 that my program goes into some sort of endless loop that I can't find, as when I click a link to get to the page I'm working on, the page never loads, and the aspnet_wp process consumes 98% of cpu time (the other 2% is used by task manager). I'm using...
24
14419
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 phone numbers, and emails and addresses even person names. I need to sift through all this data (roughly 300,000+ records and use fuzzy logic to break it down, so that i have only unique records.
32
4122
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 ); } int main() { std::deque<double> pt ;
0
2512
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
6
1324
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 value(I tried || too) , $q=bar. Even though $x is properly assigned the new value, $q always ends up being bar. Abstractions aside, here's the code: (note ## comments added to this post) <snip>
3
1711
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 (var_name !=('a' || 'b' || 'c' || 'd' || 'e' || 'f'') ) {
10
8544
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 2 nd file. And I tried the following logic. It might be bit round about way but as a beginner am trying as follows. The column in the 1st file is having data as example NS008_456_R0030_3008 The 2nd file data is as follows: + test ...
0
9705
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
10323
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
10074
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
9138
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
7613
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
6847
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
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.