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 ?
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 ?
Short answer for : you cannot avoid comparing the values. To determine both the smallest and largest of three items you need at least three comparisons in the worst case (this is the classical min/max lower bound) — see Selection algorithm — minimum and maximum. That means some pairwise checking is inevitable given only relational tests.
If a library call is acceptable, the standard library gives a concise, correct solution (C++11+):
#include <algorithm>
#include <iostream>
int main() {
int a = 7, b = 2, c = 5;
auto p = std::minmax({a, b, c}); // pair with .first=min and .second=max
std::cout << "min=" << p.first << " max=" << p.second << '\n';
} If you must use only if and relational operators (no else, no logical ops), a simple, safe pattern is to initialize both min and max to one value and update them with separate if checks:
int mn = a, mx = a;
if (b < mn) mn = b;
if (b > mx) mx = b;
if (c < mn) mn = c;
if (c > mx) mx = c; That uses only if and relational operators and is easy to read and maintain (worst-case 4 comparisons). To reach the theoretical minimum of 3 comparisons, compare a and b first, then compare the winner and loser of that pair to c; 's nested-if implements that idea compactly.
Notes and cautions: correctly pointed to library helpers, which handle ties and custom types with comparators. 's counting/decrement trick and 's equality-scanning idea are clever but fragile (they fail or are impractical for negatives or large ranges). 's sort-based suggestion works but is overkill for three values. For general code prefer the library (std::minmax) or the simple update pattern above; both are clear and robust.
Jump to Post— twomers 408You could always have them in an array and use and max_
From site:// min_element/max_element #include <iostream> #include <algorithm> using namespace std; // snip int main () { int myints[] = {3,7,2,5,6,4,9}; // …
Jump to Post— n1337 29Errr...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 …
You could always have them in an array and use and max_
From site:
// 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;
} 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 ?
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
I guess you could use "Bubble-Sort"
n1337 your solution might fit :p thanks
everyone else
> 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:
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;
}
} 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;
} Or without any logical operators:
int min3(int a, int b, int c) {
for (int i = 0;; i++) {
if (!(a - i)) return a;
if (!(b - i)) return b;
if (!(c - i)) return c;
}
return 0;
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.