• C

Conditional Statements Structure Question

Hello Experts,

I'm trying to help my daughter with her C homeowork.  I'm a longtime developer but have not worked in C in decades.  She has the following problem:

The mathematical operation min(x,y) can be represented by the conditional
   expression

     (x < y) ? x : y

   In similar fashion, using only conditional expressions, describe the
   mathematical operations:

   min(x, y, z)             and          max(x,y,z,w)

I understand how the min(x,y) translate into (x < y) ? x : y
 and that (x < y) ? x : y  is tha same as If x < y then x else y
but am drawing a blank as to how to code the two problems.

Can anyone help?

Thanks,
Steve
WaterstoneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avinthmCommented:
min(x,y,z)
(x<y) ? ((x < z) ? x : z) : ((y < z) ? y : z) ;

i hope this works....
the same way you can write to max(x,y,z,w)


ikeworkCommented:
hi Waterstone,

as you wrote:

min2(x,y)   (x < y) ? x : y;

so

min3(x,y,z)   min2( min2( x, y ), z );

and

max4( x, y, z, w )    max2( max2( max2( x, y ), z ), w );


hope it helps
ike

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunnycoderCommented:
#include <stdio.h>

int main()
{
      int i=10,j=3,k=12,l=9;
      int max,min;

      min = (i<j)?((i<k)?i:k):((j<k)?j:k);

      max = (j>i)?((j>k)?(j>l?j:l):(k>l?k:l)):((i>k)?(i>l?i:l):(k>l?k:l));

      printf("min %d max %d",min,max);
}

Cheers!
sunnycoder
deepu chandranCommented:
u can write the solution for finding minimam of N number,
first u hav to pass the numbers into the array
pass the array and the no. arg over the function named min
the function is like this,
int min(int arr[20],int noarg)  //arr[20]  is the array which are having no.//noarg  is the no. arguments
{
   int i,min=arr[0];
    for(i=1;i<noarg;i++)
    {
      min=(min<arr[i])?min:arr[i]);
    }
   return min;
}
PaulCaswellCommented:
Hi Waterstone,

They're probably looking for something like:

#define Min(x,y) ((x)<(y)?(x):(y))
#define Min3(x,y,z) (Min(x,y)<(z)?Min(x,y):(z))
...

Paul
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.