The basics of one dimensional arrays.
Learning to use arrays, basic finger exercises.
More exercises to teach you arrays and loops.
Two dimensional arrays can represent temperatures across the states, locations of buildings, or hospital X-rays.
Exercises to bring back to your mind basic facts about arrays.
1D arrays
Make create a two dimensional array of a given width and height filled in a snake pattern with 0 and 1 as described below. The snake pattern starts with a 1, then has longer and longer sequences of zeros separated by ones: 0 zeros, one, 1 zero, one, 2 zeroes, one, 3 zeroes, one, etc. 11010010001000010000010000001 The array is filled in by rows, first going to the right, then going to the left, in the pattern shown by these “ascii arrows”:
Count how many times “dolphin” appears in a string. Except don’t count the word “dolphin” if it comes immediately after “shark “. public static int cdol(String str) Examples: cdol("dolphin dolphin dolphin") => 3 cdol("dolphin shark dolphin") => 1
(similarEnds) An array has similar N-ends if the front and back N numbers are either in the reversed order. Return the largest N value for which an array has the same N-ends. N is a positive integer. Give -1 if the ends are not similar at all. Examples: similarEnds([20,30,40]) => -1. similarEnds([20,30,4,5,30,20]) => 2. similarEnds([20,30,40,50,50,40,30,20]) => 8. The repeating segments overlap. similarEnds([10,20,30,20,10]) => 5.
Array List exercises. 2019-01-16: throddPigs, hasPieee, ?
(back13) Take each multiple of 13 that appears in the array and change all of the numbers before it to be that multiple of 13, until you come to another multiple of 13. Examples: back13([11,12,13,14]) => [13,13,13,14] back13([40,39,50,52,60]) => [39,39,52,52,60] back13([21,23,25,26,90,65,17]) => [26,26,26,26,65,65,17]
An isthmus location contains a non-zero number with the same number to the left and right, and has zeros above and below. In the example below, the middle 5 is an isthmus location. 0 5 5 5 0 Given 2d array of ints, count the number of isthmus locations that contain odd numbers. Test code
Chapter 12, exercises 23, 24, and 25. Write tests to prove that your work is correct. Those exercises, restated here, are: Exercise 23: Rotate public static int[] rotateLeft(int[] a): Rotate {0,1,2,3} to {1,2,3,0}. public static int[] rotateRight(int[] a): Rotate {0,1,2,3} to {3,0,1,2}. public static int[] rotate(int[] a, int amount): Rotate by amount to the right, so to the left when amount is negative. Exercise 24: Add Digits Add two positive numbers given by arrays representing the individual digits.
Class JavaMethods GitHub -> Chomp project directory -> CharMatrix class. The CharMatrix code can be copy-and-pasted into Repl.it or DrJava. Make sure you test your code as you go, so you know what the problems are. There is a tester file in the Chomp project directory that works with DrJava. Issues Using DrJava: how do you run the tests? Answer: Make sure the file CharMatrixTester.java is in your project.