- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayPractice.java
More file actions
Latest commit
139 lines (127 loc) · 3.23 KB
/
Copy pathArrayPractice.java
File metadata and controls
139 lines (127 loc) · 3.23 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Chapter 7.3
*/
importjava.util.Arrays;
importjava.util.Random;
publicclassArrayPractice
{
publicstaticvoidmain(String[] args)
{
int[] array = {1,2,3,4};
printArray(array);
int[] g = printArrayReverse(array);
System.out.println(Arrays.toString(g));
//Random nums = new Random(30);
int[] scores = randomNumbers(30);
System.out.println(Arrays.toString(scores));
//int a = scores(scores, 90, 100);
//System.out.println(a);
int[] range = newint[10];
for(inti = 0; i < range.length; i++)
{
if(i == 0)
range[i] = inRange(scores, i, i+10);
elseif (i == 9)
range[i] = inRange(scores, i*10, i*10+11);
else
range[i] = inRange(scores, i*10, i*10+10);
}
System.out.println(Arrays.toString(range));
/*How do the above more efficiently
int[] counts = new int[100];
for(int i = 0; i < scores.length; i++)
{
int index = scores[i];
counts[index]++;
}
System.out.println(Arrays.toString(counts));*/
//try to efficiently go through the scores array
//and organize it by grades
int[] counts = newint[10];
for(inti = 0; i < scores.length; i++)
{
intindex = 0;
if (scores[i] == 100)
counts[counts.length-1]++;
else
{
//Int Division(/) doesn't include decimals
//and always rounds the decimals down
index = ((scores[i] % 100)/10);
counts[index]++;
}
}
System.out.println(Arrays.toString(counts));
//Enhanced for Loop
int[] countsMore = newint[101];
for (intscore : scores)
countsMore[score]++;
System.out.println(Arrays.toString(countsMore));
int[] countAgain = newint[10];
for(intassign : scores)
{
//assign replaces writing scores[i]
if (assign == 100)
countAgain[countAgain.length-1]++;
else
countAgain[((assign%100)/10)]++;
}
System.out.println(Arrays.toString(countAgain));
}
publicstaticStringprintArray(int[] a)
{
System.out.print("{");
for(inti = 0; i < a.length; i++)
{
if(i == 0)
System.out.print(a[0]);
else
System.out.print(", " + a[i]);
//For a[4] why does an OutOfBounds err not occur?
//Also why is there not another comma after 4?
//YOUR DUMB! Do it!
}
System.out.println("}");
return"}";
//Why does it not return }?
//RETURN prints nothing
}
//To copy an array from one to another you do it this way
//int[] c = Arrays.copyOf(b, b.length); does it too
publicstaticint[] printArrayReverse(int[] b)
{
int[] c = newint[b.length];
//int j = 0;
for(inti = b.length-1; i >= 0; i--)
{
for (intj = 0; j <= b.length-c.length; j++)
{
c[j] = b[i];
System.out.print(c[j]);
//break;
//j++;
}
}
//System.out.println();
System.out.println(Arrays.toString(c));
returnc;
}
publicstaticint[] randomNumbers(intsize)
{
Randomnumbers = newRandom();
int[] rando = newint[size];
for(inti = 0;i < rando.length; i++)
rando[i] = numbers.nextInt(101);
returnrando;
}
publicstaticintinRange(int[] scores, intlow, inthigh)
{
intcount = 0;
for(inti = 0; i < scores.length; i++)
{
if(scores[i] < high && scores[i] >= low)
count++;
}
returncount;
}
}