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 size. That integer makes and prints out an evenly spaced, size by

Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above, below, left and right of it)).

Below is the actual directions.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

//Help would be greatly appreciated. I will definitely up vote if you can help me out. I was able to print out an evenly spaced array with random integers and thought of my base case and such, but I did not know how to implement all the code to make it work.

//comments are always helpful to me

A peak in a square 2D int array is a value that is not smaller than its (at most) four neighbors (north, east, south or west are the only potential neighbors). You must write a program that recursively searches for a peak in a square 2D int array using a recursive algorithm that will be reminiscent of (but substantially different than) Binary search. Your pro ram should first read an nt iom te user. hroughout this discussion, we will call this value size and we will assume that size is always a positive int. Then, you should generate a 2D int array that is size by size, whose elements are randomly generated positive ints that are all less than 100. Then, you should print out the array (formatted with even spacing). Finally, print out the value and location (row, col) of a valid peak returned by the recursive algorithm. You must find a valid peak using the recursive method illustrated by the forthcoming example: Columns are indexed, starting from the left, from 0 to 6. Here is how the recursive algorithm finds a valid peak. Denote left, right and middle with green, red, and yellow, respectively In the first call to the recursive method, left is 0 and right is 7 (note that 7 is not a valid column index, so there are no numbers in the red column in the following array) 55 2014756 78 21 24 3228 42 71 50 91 8261 60 5621 61 51 3648 3339 65 91 58 1720 16 81 65 16 8 4335 30 30 55 46 73 3538 13 48 4 75 Find the largest element in the middle column: 55 2014 7 5678 21 24 31228 42 71 50 91 8261 60 5621 61 51 3648 33 39 65 91 58 1720 16 81 65 16 8 433530 3055 46 73 3538 13 48 4 75 The largest element is 60 and it is NOT a peak because it is less than the value to its left (61). So there must be a peak in the left half of the array, which the algorithm will eventually return (what if the value to the right of 61 was larger?). In the next recursive call, left is 0 and right is 3 55 2014 756 78 21 24 312 28 42 71 50 91 8261 60 5621 61 51 3648 33 39 65 91 58 1720 16 81 65 16 8 4335 30 30 55 46 73 3538 13 |4847 Find the largest element in the middle column, with respect to columns left (inclusive) and right (exclusive). The largest element is 82 and it is still NOT a peak because it is less than the value to its left. So, there must be a peak in the left half of the portion of the array currently being considered (between left (inclusive) and right (exclusive)), which the algorithm will eventually return. In the next recursive cal1, left is 0 and right is 1 55 2014 7 56 78 21 24 312 28 42 71 50 918261 60 56 21 61 51 3648 33 3965 91 58 1720 16 81 65 16 8 4335 30 30 55 46 73 3538 13 4847 e are in the base case, because we are only considering elements in one column (the green column). Therefore, we return the location of the largest element (91) in column left, which is [2, 0]. So the method should return an array with two elements, where the first element is the row, and the second element is the column of the peak found public static int[] findPeakRec (int[] [1 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 (intl] [] 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: // code to read in size from user int[l] data-getRandomDataTable (size) I/ helper method // assume data is the example array from above printDataTable (data): // helper method // Call recursive meth d tfind peak intll peakcoordsfindPeakRec (data, 0, size) // Get the peak value from the data using the coords returned int peakVal-data [peakcoords [0]] [peakcoords [1]1; // Output the peak and its location System.out.println (peakVal " is a peak at row "+ peakCoords [0] + ",col"t peakcoords [1]) // For the data in the example from above, this would print: // 91 is a peak at row 2, col 0

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

Microsoft Outlook 2023

Authors: James Holler

1st Edition

B0BP9P1VWJ, 979-8367217322

More Books

Students also viewed these Databases questions