-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_12.java
More file actions
46 lines (38 loc) · 1.39 KB
/
Copy pathExercise07_12.java
File metadata and controls
46 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;
/*****************************************************************************
* program puts ten double numbers into an array and passes it to the reverse
* method. The reverse method supports an unknown number of values. The main
* method then prints the results.
*****************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter ten numbers: ");
double[] array = new double[10];
for (int i = 0; i < 10; i++) {
array[i] = input.nextDouble();
}
//reverse the array here:
reverse(array);
//output
System.out.println("The reversal of the input is: ");
for(double i : array)
System.out.print(i + " ");
}
//reverses an array of unknown length
public static void reverse(double[] numbers){
int largerIndex = numbers.length - 1;
int smallerIndex = 0;
for (int i = 0; i < numbers.length/2; i++) {
double temp = numbers[smallerIndex];
numbers[smallerIndex] = numbers[largerIndex];
numbers[largerIndex] = temp;
smallerIndex++;
largerIndex--;
}
}
}