Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Test Your Skills: Smalltalk Edition
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon May 21, 2007 11:19 am   Post subject: Test Your Skills: Smalltalk Edition

Write the shortest possible (non-obfuscated) code which takes an array of integers "foo" and finds the first element which is a multiple of both 7 and 2. It should store this element in a variable "bar". If no such element is found, zero should be stored in "bar" and the following message printed as a side-effect.

Quote:
Nothing found, assuming default value.


A Java implementation is provided.

code:
int[] foo = new int[] { 3, 5, 2, 8, 7, 9, 14 };
int result = 0;
boolean resultFound = false;

for (int index = 0; index < foo.length; index++) {
   if (foo[index] % 14 == 0) {
      result = foo[index];
      resultFound = true;
      break;
   }
}

if (!resultFound) {
   System.out.println("Nothing found, assuming default value.");
}
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: