Given the numbers a, b and c. Find the sum of the largest and smallest of these numbers. The algorithm must be optimal and have the smallest possible number of comparisons. I decided it this way:
#include "stdafx.h" #include <iostream> using namespace std; void main() { double a, b, c; cout << " Enter a = "; cin >> a; cout << " Enter b = "; cin >> b; cout << " Enter c = "; cin >> c; double Suma; if (a < b && a < c) if (b > a&&b > c) Suma = a + b; else if (c > a&&c > b) Suma = a + c; if (b < a&&b < c) if (a > b&&a > c) Suma = b + a; else if (c > a&&c > b) Suma = b + c; if (c < a&&c < b) if (a > b&&a > c) Suma = c + a; else if (b > a&&b > c) Suma = c + b; cout << " Suma : " << Suma << endl; } Please write, can you decide better
MAXi(a,b) = (a+b+ABS(ab))/2MAXi(a,b,c)=MAXi(a,MAXi(b,c)). Similarly for the minimum. And fold. - Akina