Greatest value in a array Learn programming Java

Lesson:

Functions


Exercise:

Greatest value in a array


Objetive:

Create a function which returns the greatest value stored in an array of real numbers which is specified as parameter:

float[] data={1.5f, 0.7f, 8.0f}
float max = Maximum(data);


Code:

public class Main
{
	public static void main(String[] args)
	{
		float[] data = {1.5f, 0.7f, 8.0f};
		float max = Maximum(data);
		System.out.println(max);
	}

	private static float Maximum(float[] list)
	{
		float max = -99999999.00f;
		for (int i = 0; i < list.length; i++)
		{
			if (i == 0)
			{
				max = list[i];
			}
			else
			{
				max = max < list[i] ? list[i] : max;
			}
		}
		return max;
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.