Sunday, December 20, 2009

Maximum value 1st, 2nd

Maximum value can with method as this code:

public class Array
{
public void getMax( double ar[] )
{
double max1 = ar[0]; // Assume the first
int ZERO = 0; // Variable to store inside it the index of the max value to set it to zero.

for( int i = 0; i <>= max1)
{
max1 = ar[i];
ZERO = i;
}
}

ar[ZERO] = 0; // Set the index contains the 1st max to ZERO.
double max2 = ar[0]; // element in the array

for( int j = 0; j <>= max2 )
{
max2 = ar[j];
ZERO = j;
}
}

System.out.println("The 1st maximum element in the array is: " + max1 + ", the 2nd is: " + max2);
}

public static void main(String[] args)
{
// Creating an object from the class Array to be able to use its methods.
Array myArray = new Array();
// Creating an array of type double.
double a[] = {6.6,2.2, 3.4, 5.5, 5.5, 5.6};

myArray.getMax( a ); // Calling the method that'll find the 1st max, 2nd max.
}

}


This code can increase with Array 3nd etc.

3 comments:

  1. wah saya gak ngerti e mas itu code2 nya, tp baguslah utk share buat orang lain yg mencari kode tsb, selamat hari ibu

    ReplyDelete
  2. @nineta,.. selamat hari ibu juag
    @raajfresh,...
    blog ini sebetulnya buat sharing yang suka coding,..selamat hari ibu juga

    ReplyDelete