forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHistogram.java
More file actions
112 lines (103 loc) · 3.2 KB
/
MyHistogram.java
File metadata and controls
112 lines (103 loc) · 3.2 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
/**
* Created by media on 2016-07-10.
*/
public class MyHistogram {
public static int readInput(String paramname){
Scanner in = new Scanner(System.in);
boolean okay;
do{
System.out.print("Write " + paramname);
if(in.hasNextInt()){
okay = true;
}else {
okay = false;
System.err.println("Wrong input value!");
}
}while (!okay);
return in.nextInt();
}
/*
* returns array of random integers with minimal value 1
*/
public static int[] randomArray(int size){
Random random = new Random();
int[] initializedarray = new int[size];
for(int i=0; i<initializedarray.length; i++){
initializedarray[i] = random.nextInt(10);
}
return initializedarray;
}
/*
* displays elements in ranges
*/
public static void printRanges(int[][]inputRanges){
for(int i=0; i<inputRanges.length; i++){
System.out.println(Arrays.toString(inputRanges[i]));
}
}
/*
* returns number of range, puts input value to one from input number of rangess
*/
public static int[][] calculateRanges(int noOfRanges, int elements){
int elementsInRange = (int) elements/noOfRanges;
int ranges[][] = new int[noOfRanges][elementsInRange];
for(int row=0; row<noOfRanges; row++){
for(int col=0; col<elementsInRange; col++){
ranges[row][col] = row*elementsInRange + col;
}
}
printRanges(ranges);
return ranges;
}
/*
* checks if input element is in array
*/
public static boolean inRange(int[] row, int element){
boolean okay = false;
for(int i=0; i<row.length; i++){
if(element == row[i]){
okay = true;
break;
}
}
return okay;
}
/*
* selects range which contains input element
*/
public static int selectRange(int element, int[][] ranges){
int row = 0;
int selectedRange = 0;
while (row<ranges[0].length){
if(inRange(ranges[row], element)){
selectedRange = row;
break;
}
row++;
}
return selectedRange;
}
/*
* displays histogram with values of input array in number of ranges set by second input
*/
public static void histogram(int[] array, int counters){
int[] histogram = new int[counters];
int[][] ranges = calculateRanges(counters,array.length);
for(int score=0; score<array.length; score++){
int tmp_range = selectRange(array[score],ranges);
histogram[tmp_range]++;
}
for(int i=0; i<histogram.length; i++){
System.out.printf("\nValue: %d has %d elements",i,histogram[i]);
}
}
public static void main(String[] args){
int[] scores = randomArray(10);
System.out.println(Arrays.toString(scores));
int noofcounters = readInput("number of counters: ");
histogram(scores,noofcounters);
}
}