THINK FIRST·CODE LATER

← All labs

Reverse an array and search in it

Problem

Write a class ArrayTools with two static methods:

public static void reverse(int[] a)      // reverses the array in place
public static int indexOf(int[] a, int value)   // position of value, or -1

The main method reads a count n, then n integers, then one value to search for. It prints the reversed array on one line after Reversed: , with the numbers separated by a space, and then the position of the value in the original array:

Reversed: 9 7 5 3
Found at: 2

Search in the original array before reversing it. Print Found at: -1 when the value is absent.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-2-data-1.txt

4 3 5 7 9 7
What your program must print for l9-2-data-1.txt
Reversed: 9 7 5 3

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.