Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Make a program using Java that asks the user to input an integer. That integer makes and prints out an evenly spaced, size by size

Make a program using Java that asks the user to input an integer. That integer makes and prints out an evenly spaced, size by size 2D array (If odd numbers like 7 is used, only columns 0-6 should be filled with numbers so there can be a middle index). The array must be filled with random numbers less than 100. Then, using recursion find a "peak" using the following algorithm.

1. Find the largest element in the middle column (ex: 7 is input ---> first rec. call ---> array index starts at left 0 right 7 ---> 0 left 6 right so there is a middle index)

2. Check to see if a value that is to its immediate left, right, up, or down is larger than itself. (ex:second rec call---> value to left is bigger ---> left is 0, right is 3)

3. If it is, move to that number and repeat until no numbers are directly next to a bigger number.

4. print out the peak (ex: 91 is a peak at row 0, col 2)

//Following may be useful

image text in transcribed

image text in transcribed

Generalize this algorithm and implement the recursive method: public static int[] findPeakRec(int[][] data, int left, int right) Hint: You may also find it helpful to write helper method(s). For example, you may want: public static int largest (int[] [] data, int col) // (returns the index (row) of the largest element in the given column in the data table) Your main should look something like this /I code to read in size from user int[] data -getRandomDataTable (size)// helper method // assume data is the example array from above printDataTable (data) // helper method // Call recursive method to find peak int[] peakCoords - findPeakRec (data, 0, size); // Get the peak value from the data using the coords returned int peakVal-data [peakCoords [0]] [peakCoords [1]1; /I output the peak and its location System.out.println (peakVal " is a peak at row "+ peakCoords [0] + ", col"+ peakCoords [1]) Generalize this algorithm and implement the recursive method: public static int[] findPeakRec(int[][] data, int left, int right) Hint: You may also find it helpful to write helper method(s). For example, you may want: public static int largest (int[] [] data, int col) // (returns the index (row) of the largest element in the given column in the data table) Your main should look something like this /I code to read in size from user int[] data -getRandomDataTable (size)// helper method // assume data is the example array from above printDataTable (data) // helper method // Call recursive method to find peak int[] peakCoords - findPeakRec (data, 0, size); // Get the peak value from the data using the coords returned int peakVal-data [peakCoords [0]] [peakCoords [1]1; /I output the peak and its location System.out.println (peakVal " is a peak at row "+ peakCoords [0] + ", col"+ peakCoords [1])

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

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

3. Comment on how diversity and equality should be managed.

Answered: 1 week ago

Question

describe the legislation that addresses workplace equality

Answered: 1 week ago