简体   繁体   中英

It is nor max/min code how do i figure out this code?

get a program that gets (three integers ) and display the value that is neither max nor min?

I tried to think of a way to get a number between max and min. My input is x,y,z which are (three integers ) how to get this number which is neither max nor min?

int x,y,z,max,min,between;
cout<<"enter 3 integer values \n";
cin>>x>>y>>z,between;

if (x>y&&x>z)
    x=max;
else if (x<y&&x<z)
    x=min;
else if(y>z&&y>x)
    y=max;
else if (y<z&&y<x)
    y=min;
else if(!max==between&& !min==between)
    cout<<"not max or min is "<<between<<endl;

return 0;
}

will I know that this is not the right code but I know that the path is somewhere close.

get a number between max and min. input (three integers )

This is essentially the median of the three numbers.

A simple solution can be to take the elements in an array, sort them and then pick the middle element, like so,

int elements[3] = ... // contains x,y,z
std::sort(std::begin(elements), std::end(elements)); // sorts the elements
elements[1] // pick the middle element

I am assuming that all the numbers are distinct otherwise there is no number which is neither min nor max. For example, of the three numbers are 3, 3 and 3, 3 is both the min and max. There is no solution in such a case.

With this code you compare the first 2 variable to see witch of them is the max value and after that just compare with the other variable

if (x < y) 
{
    if (z < x)  
    {
        return x;
    }
    else if(z < y) {
        return z;
    }
    else {
        return y;
    }

}
else if (y < z) 
{
    if (z < x) {
        return z;
    }
    else {
        return y;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM