Sunday, September 12, 2010

public class Example3
{
 public static void main (String [] args) 
 {
  int numbers[] = {174,576,3458,245};
  int max = 0,i;
  for (i = 1; i < numbers.length; i++)
   {
    while (numbers[i]>max)
    {
    max = numbers[i];
    }
   }
  System.out.println (max);
 }
}

Assigns the first value to an integer and then goes through the array comparing the first value to the rest of the values. When a larger value is found that value becomes the max integer and is then compared until the largest variable is found. This will terminate as it goes through the array of numbers only once comparing the current max to the rest of the numbers.

1 comment:

  1. The program is correct.
    You have argument about finiteness and correctness.

    Only one small comment: The while is actually just an "if". It works only once.

    -Aviral

    ReplyDelete