•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,627 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,378 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 330 | Replies: 8
![]() |
•
•
Join Date: Nov 2007
Posts: 46
Reputation:
Rep Power: 1
Solved Threads: 2
how do you find the smallest and largest number of 3 numbers
without using logical operators and else statement only if and relational operator ?
is there any other way without checking every variable against each other using if statements ?
without using logical operators and else statement only if and relational operator ?
is there any other way without checking every variable against each other using if statements ?
You could always have them in an array and use min_element and max_element
From site:
From site:
cpp Syntax (Toggle Plain Text)
// min_element/max_element #include <iostream> #include <algorithm> using namespace std; // snip int main () { int myints[] = {3,7,2,5,6,4,9}; // using default comparison: cout << "The smallest element is " << *min_element(myints,myints+7) << endl; cout << "The largest element is " << *max_element(myints,myints+7) << endl; // Snip return 0; }
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
I am the Walrus!
•
•
Join Date: Nov 2007
Posts: 46
Reputation:
Rep Power: 1
Solved Threads: 2
i know there are other ways but can you tell me is there any other way to find it using relational operators and if statement only not else statement without checking each number in if statement against each other ?
•
•
Join Date: May 2008
Posts: 84
Reputation:
Rep Power: 1
Solved Threads: 7
Errr...well, if you are dealing with positive integers only, you could construct a loop, then on each iteration through the loop, subtract one from each of the numbers, and the first one to reach 0 is the smallest; the last one to reach 0 is the largest...
hahaha it models the recursive definition of a natural number, but don't do this...lol
hahaha it models the recursive definition of a natural number, but don't do this...lol
•
•
Join Date: Apr 2008
Location: Newcastle, UK.
Posts: 170
Reputation:
Rep Power: 1
Solved Threads: 4
> using relational operators and if statement only not else statement without
> checking each number in if statement against each other
You've put in too many restrictions. You have to test each number against the other at some point to find the maximum. Using only relational operators and if without else can be done, but as soon as you said "without checking each number in if statement against each other", you make the problem impossible to solve. This is what Edward thought you wanted, but now I'm not sure:
> checking each number in if statement against each other
You've put in too many restrictions. You have to test each number against the other at some point to find the maximum. Using only relational operators and if without else can be done, but as soon as you said "without checking each number in if statement against each other", you make the problem impossible to solve. This is what Edward thought you wanted, but now I'm not sure:
int MaxOf3(int a, int b, int c)
{
if (a >= b) {
if (a >= c)
return a;
if (c > a)
return c;
}
if (b > a) {
if (b > c)
return b;
if (c > b)
return c;
}
} Last edited by Radical Edward : May 12th, 2008 at 8:08 am.
If at first you don't succeed, keep on sucking until you do succeed.
Heres a way of doing it without < > <= >=
#include<iostream>
using namespace std;
int min3(int a, int b, int c) {
for (int i = 0;; i++) {
if (i == a) return a;
if (i == b) return b;
if (i == c) return c;
}
return 0;
}
int main() {
cout << min3(77,24,879); // output 24
cin.ignore();
return 0;
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Pls Help JAVA Program Headaches Not Giving me the right output for user input (Java)
- C++ second largest problem (C++)
- Pleassssssssssssse Someone Help Me!! (C++)
- I am very new to C++ and don't know what i am doing wrong (C++)
- Listing Integers in Numerical Order (C)
- ordered number (C++)
- subracting (C++)
- a little help (C++)
- Y would something compile from a hard drive fine but from on a disk? (Java)
- Im new to this site and c++ (C++)
Other Threads in the C++ Forum
- Previous Thread: single wod count
- Next Thread: can someone explain about this error



Linear Mode