Function MinMaxArray Learn programming Java

Lesson:

Functions


Exercise:

Function MinMaxArray


Objetive:

Create a function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters:

float[] data={1.5f, 0.7f, 8.0f}
MinMaxArray(data, ref minimum, ref maximum);
(after that call, minimum would contain 0.7, and maximum would contain 8.0)


Code:

public class Main
{
	public static void MinMaxArray(float[] number, float min, float max)
	{
		max = number[0];
		min = number[0];

		for (int i = 1; i < number.length; i++)
		{
			if (number[i] > max)
			{
				max = number[i];
			}
			if (number[i] < min)
			{
				min = number[i];
			}
		}

	}

	public static void main(String[] args)
	{
		float[] data = {3.5f, 0.5f, 9, 0f};
		float min = 0.0f;
		float max = 0.0f;

		MinMaxArray(data, min, max);

		System.out.printf("Minimun: %1$s - Maximun: %2$s" + "\r\n", min, max);
	}
}

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