The Artima Developer Community
Sponsored Link

C# Answers Forum
Write a method header

1 reply on 1 page. Most recent reply: Jun 10, 2003 3:05 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
janece garner

Posts: 1
Nickname: jg
Registered: Jun, 2003

Write a method header Posted: Jun 10, 2003 6:45 AM
Reply to this message Reply
Advertisement
How can I write a method with the header: public static int minimum(int i, int j, int k) that runs the smallest of i, j, and k.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Write a method header Posted: Jun 10, 2003 3:05 PM
Reply to this message Reply
How's this?
public static int minimum( int i, int j, int k )
{
   return i < j ? i < k ? i : k : j < k ? j : k;
}


Probably not the most readable or flexible solution!

This is probably better:
public static int minimum( params int [] numbers )
{
   int lowest = int.MaxValue;
   foreach( int i in numbers )
      if( i < lowest )
         lowest = i;
   return lowest;
}


This one allows you to pass any number of ints, or an array of ints.

You can also look at Math.Min() for more general treatment of decimals.

Flat View: This topic has 1 reply on 1 page
Topic: parsing problem in C++ ( I'm still learning ) Previous Topic   Next Topic Topic: DataSet serialization - WebServices

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use