Question

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.

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

Columns are indexed, starting from the left, from 0 to 6. Here is how the recursive algorithm finds a valid peak. Denote left

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

public static int[] findPeakRec (int[] [1 data, int left, int right) Hint: You may also find it helpful to write helper metho

// assume data is the example array from above printDataTable (data): // helper method // Call recursive meth d t。find peak i

//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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*; // headers MUST be above the first class

// one class needs to have a main() method
class Main {
// arguments are passed using the text field below this editor
public static void main(String[] args) {
Random r = new Random();
int low = 0;
int high = 100;

Scanner input = new Scanner(System.in);

System.out.println("Please enter size n");
int n = input.nextInt();

int array[][] = new int[n][n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int result = r.nextInt(high - low) + low;
array[i][j] = result;
}
}

int max = 0;
max = findPeak(array, n, n);
System.out.println("Maximum number in the row is " + max);
System.out.println();
}

// A wrapper over findPeakRec()
static int findPeak(int arr[][], int rows, int columns) {
return findPeakRec(arr, rows, columns, columns / 2);
}

static int findMax(int arr[][], int rows, int mid, int max) {
int max_index = 0;

for (int i = 0; i < rows; i++) {
if (max < arr[i][mid]) {
// Saving global maximum and its index
// to check its neighbours
max = arr[i][mid];
max_index = i;
}
}
return max_index;
}

static int findPeakRec(int arr[][], int rows, int columns,
int mid) {
// Evaluating maximum of mid column. Note max is
// passed by reference.
int max = 0;
int max_index = findMax(arr, rows, mid, max);

// If we are on the first or last column,
// max is a peak
if (mid == 0 || mid == columns - 1)
return max;

// If mid column maximum is also peak
if (max >= arr[max_index][mid - 1] &&
max >= arr[max_index][mid + 1])
return max;

// If max is less than its left
if (max < arr[max_index][mid - 1]) {
System.out.println("array is"+arr[max_index][mid - 1]);
System.out.println("rows is"+rows);
System.out.println("columns is"+columns);
System.out.println("mid is"+mid);

return findPeakRec(arr, rows, columns, (mid - mid / 2));
}

// If max is less than its left
// if (max < arr[max_index][mid+1])
return findPeakRec(arr, rows, columns, mid + mid / 2);
}
}

Add a comment
Know the answer?
Add Answer to:
Make a program using Java that asks the user to input an integer "size". That integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • 11) a) Using RSA to encode with p-5,q-7 and c-7, what information do we make publicly...

    11) a) Using RSA to encode with p-5,q-7 and c-7, what information do we make publicly available? b) What is d, the private key? 12) Describe how, using RSA, Bob knows that, as claimed, it's really Alice sending the message. 13) Using non-randomized Select(A, n, i) to find the 4th largest element in array A, with A as I first pivot 8 11 14 09 4 25 33 98 5 22 63 54 2 111 45 23 19 728 16...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • IN JAVA PLEASE!!! :) Multithreading can help in achieving parallelism in computational problems. This makes the...

    IN JAVA PLEASE!!! :) Multithreading can help in achieving parallelism in computational problems. This makes the program’s response to generate output faster. It is achieved by delegating independent tasks within the program to separate threads instead of creating a sequential routine. Consider the following sample double array: 3 11 5 19 1 8 4 16 7 18 17 6 3 23 9 If the problem is to display all the row-sums and all the column-sums, a sequential program would use...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT