Scale Mutator Program
Scale Mutator Program. Array Manipulation. FunNotesSHELL.java. Each scale is an array of integers Representing pitch for each note in the scale Write methods to mutate the scales into a more interesting order: Reversed Scrambled (randomized) Shuffled Sorted
Scale Mutator Program
E N D
Presentation Transcript
Scale Mutator Program Array Manipulation
FunNotesSHELL.java • Each scale is an array of integers • Representing pitch for each note in the scale • Write methods to mutate the scales into a more interesting order: • Reversed • Scrambled (randomized) • Shuffled • Sorted • Sequenced (as sets of triads)
Methods to Write • void swap(int[] list, int a, int b) • void selSort(int[] list) selSort describes a Selection Sort. Look them up in the book. We will dig into the details later.
int[] scramble(int[]list) • Given an array of ints called list, return a new array of the same size and data but with the order of the data randomized. • Easy ideas?
int[] scramble(int[]list) • Given an array of ints called list, return a new array of the same size and data but with the order of the data randomized. • Easy ideas? • You have a swap method that will swap the values at two indexes. • Pick two random indexes from the array and swap them. Repeat 10,000 times.
int[] scramble(int[]list) • Given an array of ints called list, return a new array of the same size and data but with the order of the data randomized. • Another idea: • Pick a random element from list that is not -1 • Store that element in the next available index of an answer array • Replace the element in list with -1 • Repeat until the answer array is full
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] scramble(int[]list) list r ansi
int[] reverse(int[]list) • Given an array of ints called list, return a new array of the same size and data but with the order of the data reversed. • Easy ideas?
int[] reverse(int[]list) • Given an array of ints called list, return a new array of the same size and data but with the order of the data reversed. • Easy ideas? • Traverse through list from the last index to the first, copying each element into increasing indexes of the return array.
int[] makePalindrome(int[]list) • Given an array of ints called list, return a new array almost twice the size with all of the elements in order followed by all the elements in reverse order. • The last element of list should not appear twice. int [] list = {1,2,3,4}; int [] list2 = makePalindrome(list); //list2 should contain 1,2,3,4,3,2,1 Easy ideas?
int[] makePalindrome(int[]list) • Given an array of ints called list, return a new array almost twice the size with all of the elements in order followed by all the elements in reverse order. • The last element of list should not appear twice. int [] list = {1,2,3,4}; int [] list2 = makePalindrome(list); //list2 should contain 1,2,3,4,3,2,1 Easy ideas? You already have a reverse method. Why not use it?
int[] shuffle(int[]list) • Given an array of ints called list, return a new array of the same size, in the order of 1stelem, last element, 2nd element, 2nd to last element, 3rd element, 3rd to last element, etc. int [] list = {1,2,3,4,5,6,7,8}; int [] list2 = shuffle(list); //list2 should contain 1,8,2,7,3,6,4,5 Easy ideas?
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list a b ansi
int[] shuffle(int[]list) list ab ansi
int[] shuffle(int[]list) list ab ansi
int[] shuffle(int[]a, int[]b) • Given 2 arrays of ints called a and b, return a new array of all the combined elements of a and b, in the order of 1stelem from a, 1st element from b, 2nd element from a , 2nd element from b, 3rd element from a, 3rd element from b, etc. int [] list1 = {1,2,3,4}; int [] list2 = {5,6,7,8}; int [] list3 = shuffle(list1, list2); //list2 should contain 1,5,2,6,3,7,4,8
int[] mixWithNote(int[] list, int note) • Given an array of ints called list, and a single value called note, return a new array of all the elements of list alternating with note: 1stelem from list, note, 2nd element from list, note, 3rd element from list , note, etc. int [] list1 = {1,2,3,4}; int [] list2 = mixWithNote(list1, 9); //list2 should contain 1,9,2,9,3,9,4,9
int[] triads(int[] list) • Given an array of ints called list, return a new array of all the elements of list in groups of 3 consecutive elements: 1stelem from list, 2nd element from list, 3rd element from list , 2nd element from list, 3rd element from list, 4th element from list, 3rd element from list, 4th element from list, 5th element from list, etc. int [] list1 = {1,2,3,4,5,6}; int [] list2 = triads(list1); //list2 should contain 1,2,3,2,3,4,3,4,5,4,5,6 Given list1 is size n, how big should list2 be?
int [] list1 = {1,2,3};//size int [] list2 = triads(list1); //1,2,3: size 3 int [] list1 = {1,2,3,4};//size 4 int [] list2 = triads(list1); //1,2,3,2,3,4: size 6 int [] list1 = {1,2,3,4,5};//size 5 int [] list2 = triads(list1); //1,2,3,2,3,4,3,4,5: size 9 int [] list1 = {1,2,3,4,5,6}; //size 6 int [] list2 = triads(list1);//1,2,3,2,3,4,3,4,5,4,5,6: size 12
int[] triads(int[]list) list a ansi
int[] triads(int[]list) list a ansi
int[] triads(int[]list) list a ansi
int[] triads(int[]list) list a ansi
int[] triads(int[]list) list a ansi