0
 #include <iostream>

using namespace std;

int main()
{   int a,b,c,Highest;
     cout<<"Enter three numbers:";
     cin>>a>>b>>c;
       Highest=(a>b&&a>c?a:b>c?b:c);
       cout<<"Highest is:"<<Highest;

    return 0;
}

I know how to find out largest of 3 numbers by using conditional operator.BUt How to find out highest of 4 numbers by using conditional operator in c+ ??? anyone can help me out ??

Rashed Sami
  • 141
  • 1
  • 4
  • 12
  • 3
    Using the ternary operator for this will make code unreadable, but if you have to use it (as part of whatever assignment you have) just use more of it. – Some programmer dude Nov 14 '16 at 07:08
  • you mean something ugly like: a>b&&a>c&&a>d?a:b>c&&b>d?b:c>d?c:d good gods! – Djee Nov 14 '16 at 07:08
  • 1
    `std::max(a, std::max(b, std::max(c, d)));` – Arunmu Nov 14 '16 at 07:09
  • 1
    @arunmu It is a smart method but not the conditional operator. – Sean83 Nov 14 '16 at 07:10
  • You can make it a little more readable by using intermediate results and simpler ternary operations like `a > b ? a : b`. Then use the intermediate results the same way, until you have a single max value. And remember that the number of expressions can be shortened because if `a > b && b > c` then that implies `a > c` is true as well. – Some programmer dude Nov 14 '16 at 07:15

4 Answers4

1

To me the question isn't great, as a way to when learn how to do this.

I would instead ask how to find the highest of three numbers - using the knowledge of how to find the highest of two numbers, and then build on that.

As @Some-programmer-dude indicate this will use intermediate results:

  int highest = a>b ? a : b;
  highest = highest>c ? highest : c;
  highest = highest>d ? highest : d;

or if you want parallelism:

int hFirstPart = a>b ? a : b;
int hSecondPart = c>d ? c : d;
int highest = hFirstPart>hSecondPart? hFirstPart : hSecondPart;
Hans Olsson
  • 11,123
  • 15
  • 38
0

Place them into a vector and use std::max_element:

std::vector<int> vec;
int tmp;
for(int i = 0; i < 3; ++i)
{
    std::cin >> tmp;
    vec.push_back(tmp);
}
int max = *std::max_element(vec.begin(), vec.end());
std::cout << max << std::endl;
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
0
 Highest=(a>b&&a>c&&a>d?a:b>c&&b>d?b:c>d&c:d);

This code is pretty unreadable so better to expand it using a set of consequent if clauses.

Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
-3

Below code will work for any number of elements, just replace SIZE with the desired number of elements.

#include <iostream>

using namespace std;

#define SIZE 4

int GetMax(int[], int, int, int);

int main()
{

   int a[SIZE];
   int i=0;

   while(i < SIZE)
   {
       cout << "Enter input " << i+1 << " : ";
       cin >> a[i++];
   }

   cout << "Highest = " << GetMax(a, 0, SIZE, a[0]) << endl;

   return 0;
}

int GetMax(int a[], int i, int n, int max)
{
    return ( i < n-1) ? GetMax(a, i+1, n, (a[i] > max ? a[i] : max)) :  a[i] > max ? a[i] : max;
}
paper.plane
  • 1,201
  • 10
  • 17