forked from jkimrusd/ArrayExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
327 lines (275 loc) · 9.51 KB
/
Main.java
File metadata and controls
327 lines (275 loc) · 9.51 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Example for Arrays
*/
import java.util.Scanner;
class Main
{
/*
* This one will have public static void main
*/
public static void main( String[] args)
{
/*
* SET UP
* We are going to ask the user to input an integer
* and we will call it
n
*/
Scanner s = new Scanner(System.in);
System.out.print(" Please type in an integer :");
int n = s.nextInt();
/*
* Task 1. Create an array of integers from 0 to n-1
* call this array ar1. Print it out below.
*/
System.out.println("*** PRINTING ar1 *");
int[] ar1;
ar1 = new int [n];
for (int i=0; i<ar1.length; i++)
ar1[i]=i;
for (int i=0; i<ar1.length; i++)
System.out.println("ar1["+i+"] = "+ar1[i] );
/*
* Task 2. Create a new array called ar2.
* Copy the elements of ar1 into ar2.
*/
int[] ar2;
ar2 = new int [n];
for (int i=0; i<ar2.length; i++)
ar2[i]=i;
for (int i=0; i<ar2.length; i++)
System.out.println("ar2["+i+"] = "+ar2[i] );
/*
* Task 3. Add 1 to each element in ar1. Print it out below
*/
System.out.println("** Stask 3 **");
int value;
for (int i=0 ; i<ar1.length ; i++)
{
ar1[i] = ar1[i] +1;
}
for (int i=0 ; i<ar1.length ; i++)
System.out.println("ar1[" + i + "] = "+ar1[i]);
/*
* task 4. Create a new array called ar3.
* Copy the elements of ar1 into ar3. Then do it again
* For example
* ar1: 1 2 3
* ar3: 1 2 3 0 1 2 3
*/
System.out.println("** test 4 **");
int[] ar3 = new int[ ar1.length *2 ];
for (int i=0 ; i< ar1.length ; i++)
{
ar3[i]=ar1[i];
}
for (int i=0 ; i<ar1.length ; i++)
{
ar3[i+ar1.length]=ar1[i];
}
for (int i=0 ; i<ar3.length ; i++)
System.out.println("ar3[" + i + "] = "+ar3[i]);
/*
* Task 5. Switch the first and last element of ar1.
* print out the new ar1. Then switch them back
*/
System.out.println("** Task 5**");
// Ye olde switcheroo
value = ar1[0]; // backup ar1[0] into a bitbucket
ar1[0] = ar1[ar1.length-1]; // copy last into ar1[0].
ar1[ar1.length-1]=value; // copy old value of ar1[0] into last
// Print out the array
for (int i=0 ; i<ar1.length ; i++)
System.out.println("ar1[" + i + "] = "+ar1[i]);
// The Restoration of the rightful heir
value = ar1[0];
ar1[0] = ar1[ar1.length-1];
ar1[ar1.length-1] = value;
/*
* Task 6A. Print the 2nd to (n-1)th elements of ar1
* Task 6B: Print out just the odd numbers in ar1
* Task 6C: Print out the elements of ar1 when
* the indices are multiples of 3
*
*/
System.out.println(" ** Task 6A **");
// print 2nd to (n-1)th element
for (int i=1 ; i<ar1.length-1 ; i++)
System.out.println(ar1[i]);
System.out.println(" ** Task 6B **");
// print odd numbers in ar1
// AKA if ar1[i] is odd, print it out.
for (int i=0 ; i<ar1.length ; i++)
if (ar1[i]%2==1)
System.out.println(ar1[i]);
System.out.println(" ** Task 6C **");
// print elements if it is a multiple of 3
// AKA if ar1[i] is divisble by 3, print it out.
// AKA if ar1[2] has a zero as the remainer when divided by 3
for (int i=0 ; i<ar1.length ; i++)
if (ar1[i]%3==0)
System.out.println(ar1[i]);
/*
* Task 7. For each element in ar1,
* If the element is even: leave alone
* if the element is odd, multiply by 10
* print out the new ar1
* Example
* ar[0]=10
* ar[1]=2
* ar[2]=30
* ar[3]=4
*/
System.out.println("** Task 7 **");
for (int i=0 ; i<ar1.length ; i++)
{
if ( ar1[i]%2==1)
ar1[i] *=10;
}
for (int i=0 ; i<ar1.length ; i++)
System.out.println(ar1[i]);
/*
* Task 8
* Create an array called ar2odds
* If the index of ar2 is odd, copy it to ar2odds.
* If not, do not
* ar2[0]=0
* ar2[1]=1 -> ar2odds[0]=1
* ar2[2]=2
* ar2[3]=3 -> ar2odds[1]=3
*/
System.out.println(" ** Task 8 **");
int k=0;
int[] ar2odds = new int[ar2.length]; // too big right now.
for ( int i=0 ; i<ar2.length ; i++)
{
System.out.println(ar2[i]); // Just print it out to see
if (i%2==1)
{
ar2odds[k]=ar2[i];
k++;
}
}
for (int i=0 ; i<k ; i++)
System.out.println(ar2odds[i]);
/*
* Task 9. In the array ar2, count how many odd numbers you
* have. Then create an ew array called ar4. Copy just the odd
* numbers from ar1 into ar4. Print ar4
*/
int l4=0;
for (int i=0 ; i<ar2.length ; i++)
{
if ( ar2[i]%2==1)
l4++;
}
int[] ar4 = new int[l4];
k=0;
for (int i=0 ; i<ar2.length ; i++)
{
if (ar2[i]%2==1)
{
ar4[k]=ar2[i];
k++;
}
}
for (int i=0 ; i<k ; i++)
System.out.println(ar4[i]);
/*
* Task 10. Shift the elements of ar4 right by 1
* For example
* old ar4: 1 3 5 7 9
* new ar4 9 1 3 5 7
*/
System.out.println("** Task 10 ** ");
System.out.println("Original ar4");
for (int i=0 ; i<ar4.length ; i++)
System.out.println(ar4[i]);
int temp10=ar4[ar4.length-1];
for (int i=ar4.length-1 ; i>0 ; i--)
ar4[i]=ar4[i-1];
ar4[0]=temp10;
System.out.println("New ar4");
for (int i=0 ; i<ar4.length ; i++);
System.out.println(ar4[0]);
/*
* Task 11. Reverse the order of elements in ar2
*/
System.out.println("** Task 11 **");
for (int i=0 ; i<ar2.length ; i++)
System.out.println("ar2["+i+"] = "+ar2[i]);
int ar2half = ar2.length/2;
System.out.println(ar2half);
int task11Temp;
for (int i=0 ; i<ar2half ; i++)
{
task11Temp = ar2[i];
System.out.println(" "+i+":"+(ar2.length-i));
ar2[i] = ar2[ar2.length-1-i];
ar2[ar2.length-1-i]=task11Temp;
}
for (int i=0 ; i<ar2.length ; i++)
System.out.println("ar2["+i+"] = "+ar2[i]);
/*
* Task 12:
* Create an array of Strings called ar5.
*
* Each element is a word of the following phrase
*
*
* Four score and seven years ago our fathers brought forth on
* this continent a new nation
*
*. ar5[0] = "Four"
* ar5[1] = "score"
*
* Create another array of ints called ar6. Write a for loop that
* will iterate through each element in ar5 and the length of the
* word is the element in ar6.
*
* ar5[0]= "Four" ar6[0]=4
* ar5[1]="score" ar6[1]=5
* ar5[2]="and" ar6[2]=3
*
* Count how many words have more than 5 letters.
*/
/*
* Task 13
* Create an array called monsterArray of 5 Monsters.
* The name of the monsters are
* "Cookie"
* "Grover"
* "Oscar the Grouch"
* "Elmo"
* "Rosita"
*
* Print out their names
* Use a for loop to print out the names of monster that start with
* a vowel
*/
/*
* Task 14
* Create an array of integers from 3 to 94 and call it arx
* Create an array of the indices of arx when the item is
* divisible by 3. Call this arindex
* arx[0]=94
* arx[1]=95
* arx[2]=96 // this is divisible. Index is 2
* arx[3]=97
* arx[4]=98
* arx[5]=99 // this is divisible Index is 5
*
* So arindex[0]=2
* arindex[1]=5
*/
/*
* Create an arrary called "fb" and calculate the
* first 10 fibonacci sequence. You start with
* fb[0]=1
* fb[1]=1
* fb[2]=fb[0]+fb[1]
* fb[3]=fb[1]+fb[2]
* fb[4]=fb[2]+fb[3]
*/
}
}