manzoor 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 ?

twomers 408

You could always have them in an array and use min_element and max_element
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;
}``````

manzoor 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 ?

n1337 29

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

Black Magic 15

I guess you could use "Bubble-Sort"

manzoor 2

n1337 your solution might fit :p thanks
everyone else

Radical Edward 301

> 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;
}
}``````

William Hemsworth 1,339

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;
}``````

William Hemsworth 1,339

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;
}``````