001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
package algs13;
import stdlib.*;
@SuppressWarnings("all")
public class XWhatGoesWrongInLoopsAndRecursion {
private Node first = null;
static class Node {
public Node (double item, Node next) { this.item = item; this.next = next; }
double item;
Node next;
}
/*
* numFives returns the number of fives in an array, written with a loop
*/
public static int numFives0 (double[] a) {
int result = 0;
for (int i=0; i<a.length; i++)
if (a[i] == 5.0)
result++;
return result;
}
public static int numFives1 (double[] a) {
double[] list = new double[] { 4, 5, 6, 5, 3 };
int result = 0;
for (int i=0; i<list.length; i++)
if (list[i] == 5.0)
result++;
return result;
}
public static int numFives2 (double[] a) {
int result = 0;
for (int i=0; i<a.length; i++) {
if (a[i] == 5.0)
result++;
else
return result;
return result;
}
StdOut.print (result);
return 0;//added to remove compiler error
}
public static int numFives3 (double[] a) {
int result = 0;
for (int i=0; i<a.length; i++)
if (a[i] == 5.0)
result++;
else
i++;
return result;
}
public static int numFives4 (double[] a) {
int result = 0;
for (int i=0; i<a.length; i++)
if (i == 5.0)
result++;
return result;
}
public static int numFives5 (double[] a) {
int result = 0;
for (int i=0; i<a.length; i++)
//if (a == 5.0)
result++;
return result;
}
public static int numFives6 (double[] a) {
int result = 0;
for (int i=0; 0<a.length; i++)
if (a[i] == 5.0)
result++;
return result;
}
public static int numFives7 (double[] a) {
int result = 0;
for (int i=0; i>0; i++)
if (a[i] == 5.0)
result++;
return result;
}
/*
* numFours returns the number of fours in an array, written recursively
*/
public static int numFours0 (double[] a) {
return numFours0 (a, 0, 0);
}
private static int numFours0 (double[] a, int i, int result) {
if (i>a.length)
return result;
if (a[i] == 4.0)
return numFours0 (a, i+1, result+1);
else
return numFours0 (a, i+1, result);
}
public static int numFours1 (double[] a) {
return numFours1 (a, 0);
}
private static int numFours1 (double[] a, int i) {
if (i>a.length)
return 0;
if (a[i] == 4.0)
return 1 + numFours1 (a, i+1);
else
return numFours1 (a, i+1);
}
public static int numFours00 (double[] a) {
return numFours00 (a, 0, 0);
}
private static int numFours00 (double[] a, int i, int result) {
if (i>a.length)
return result;
if (a[i] == 4.0)
result = result+1;
result = numFours00 (a, i+1, result);
return result;
}
public static int numFours11 (double[] a) {
return numFours11 (a, 0);
}
private static int numFours11 (double[] a, int i) {
if (i>a.length)
return 0;
int result = numFours11 (a, i+1);
if (a[i] == 4.0)
result = result+1;
return result;
}
/*
* numThrees returns the number of threes in a linked list
*/
public int numThrees0 () {
int result = 0;
for (Node x = first; x != null; x = x.next)
if (x.item == 3.0)
result = result + 1;
return result;
}
public int numThrees00 () {
Node x = first;
int result = 0;
while (x != null) {
if (x.item == 3.0)
result = result + 1;
x = x.next;
}
return result;
}
public int numThrees1 () {
Node x = first;
int result = 0;
while (x != null) {
if (x.item == 3.0)
result = result + 1;
x = x.next.next;
}
return result;
}
public int numThrees3 () {
if (first == null) return 0;
int result = 0;
if (first.item == 3.0)
result = result + 1;
Node x = first;
while (x.next != null) {
if (x.next.item == 3.0)
result = result + 1;
x.next = x.next.next;
}
return result;
}
public int numThrees2 () {
if (first == null) return 0;
int result = 0;
if (first.item == 3.0)
result = result + 1;
Node x = first;
while (x.next != null) {
if (x.next.item == 3.0)
result = result + 1;
x = x.next;
}
return result;
}
public int numThrees4 () {
if (first == null) return 0;
int result = 0;
if (first.item == 3.0)
result = result + 1;
Node x = first;
while (x.next != null) {
if (x.item == 3.0)
result = result + 1;
x = x.next;
}
return result;
}
public int numThrees20 () {
if (first.next == null) return 0;
Node x = first;
int result = 0;
while (x.next != null) {
if (x.item != 3.0) x = x.next;
if (x.item == 3.0) {
result = result + 1;
x = x.next;
}
}
if (x.next == null && x.item == 3.0) {
result = result + 1;
}
return result;
}
public static void main (String[] args) {
}
}
|