Hi
I need your advice and opinion. So please help me. In the CODE 1 the largest of three numbers is found and the second CODE 2 also involves the same problem except that now we have four numbers. Don't you think the logic used in CODE 2 more simple and understandable? Any advice. I'm grateful for your guidance and time. (Sorry, if there is problem with the indentation)
CODE 1:
Code:
// program to find the largest of three numbers; assuming all the numbers are distinct
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float a, b, c;
cout << "Enter the three numbers" << endl;
cout << "Enter a = ";
cin >> a;
cout << "Enter b = ";
cin >> b;
cout << "Enter c = ";
cin >> c;
if ( (a > b)&&(b > c) )
cout << "a is the largest" << endl;
else if ( (c > b)&&( a > c) )
cout << "a is the largest" << endl;
else if ( (b > a)&&( b > c) )
cout << "b is the largest" << endl;
else if ( (c > a)&&( c > b) )
cout << "c is the largest" << endl;
getch();
}
CODE 2:
Code:
// program to find the largest of four numbers; assuming all the numbers are distinct
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
int main()
{
system("cls");
float a, b, c, d;
cout << "Enter the numbers below" << endl;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "d = ";
cin >> d;
{
if ( (a > b) && (a > c) && (a > d) )
cout << "a is the largest" << endl;
else if ( (b > c) && (b > d) ) /* no need to check against "a" */
cout << "b is the largest" << endl;
else if ( (c > d) )
cout << "c is the largest" << endl;
else if ( (d > c) )
cout << "d is the largest" << endl;
else
cout << "Input error" << endl;
}
system("pause");
}