Question
This exercise is just to help get you further used to working with methods. You have basically done this task before (in the Min Swap
This exercise is just to help get you further used to working with methods.
You have basically done this task before (in the Min Swap exercise - in fact that's a little harder), but now we're just going to wrap the whole thing up in a method.
Your task is to complete the method
public static int min(int[] data)
This method takes an array as a parameter (of any possible length positive length - so no empty arrays to deal with), and should return the minimum element in it. That's all. Do not just use the min method in Arrays, actually do the work.
Your skeleton code also has a main method which you can use for your own testing, but is not part of the marking.
import java.util.Arrays;
public class Min { //Complete this method public static int min(int[] data) { int min_idx = 0; for (int i = 1; i < data.length; i++) { if(data[i] < data[min_idx]){ min_idx = i; } }
return Integer.MAX_VALUE; //This is just here as a placeholder }
private static void main(String[] args) { int[] testData = {7, 3, 2, 5, 4, 1, 8};
System.out.println("The minimum value in " + Arrays.toString(testData) + " is " + min(testData)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started