forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecrusiveMaxInRange.java
More file actions
64 lines (58 loc) · 1.72 KB
/
RecrusiveMaxInRange.java
File metadata and controls
64 lines (58 loc) · 1.72 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
/**
* Created by media on 2016-07-16.
*/
public class RecrusiveMaxInRange {
public static int readInput(String variableName){
Scanner in = new Scanner(System.in);
boolean okay;
do{
System.out.print("Write " + variableName);
if(in.hasNextInt()){
okay = true;
} else{
okay = false;
}
} while(!okay);
return in.nextInt();
}
public static void initArray(int[] array){
int tmp = 0;
String message = "";
for(int i=0; i<array.length; i++){
message = "element no. " + i + ": ";
tmp = readInput(message);
array[i] = tmp;
}
}
/*
* This method returns max element of array from specified in input ranges
*/
public static int maxInRange(int[] array, int li, int mi){
if(mi == li){
return array[mi];
} else{
if(array[mi]>maxInRange(array,li,mi-1)){
return array[mi];
} else{
return maxInRange(array,li,mi-1);
}
}
}
/*
* This method returns max element of input array
*/
public static double max(int[] array){
double result = (double) maxInRange(array,0,array.length-1);
return result;
}
public static void main(String[] args){
int arraySize = readInput("array size: ");
int[] userArray = new int[arraySize];
initArray(userArray);
//int lowIndex = readInput("lower index: ");
//int highIndex = readInput("higher index: ");
double maxElement = max(userArray);
System.out.printf("Result is: %f", maxElement);
}
}