Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions

Question

What is operatiing system?

Answered: 1 week ago

Question

=+j Describe how EU directives impact IHRM.

Answered: 1 week ago

Question

=+and reduction in force, and intellectual property.

Answered: 1 week ago