Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 686 Bytes

exercise-answers.md

File metadata and controls

38 lines (33 loc) · 686 Bytes

CSE 143

Exercise Answers

Week 1

  1. Two possible answers are shown below.

    public void stutter() {
    	for (int i = size - 1; i >= 0; i--) {
    		elementData[2 * i + 1] = elementData[i];
    		elementData[2 * i] = elementData[i];
    	}
    	size *= 2;
    }
    public void stutter() {
    	int index = size - 1;
    	for (int i = size * 2 - 1; i > 0; i-=2) {
    		elementData[i] = elementData[index];
    		elementData[i - 1] = elementData[index];
    		index--;
    	}
    	size *= 2;
    }
  2. One possible answer is shown below.

public void mirror() { for (int i = size; i < size * 2; i++) { elementData[i] = elementData[size * 2 - 1 - i]; } size *= 2; } ```