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 ?
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;
}
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.