ARR - Traversing Arrays (Lesson)

APCompSci_LessonTopBanner.png

Traversing Arrays

Introduction

Working with large amounts of data makes programs more useful and interesting. Lots of data opens lots of opportunities! However, how do you deal with all that data? The more information that we use the more painstaking the process to get through it all. How can we manage big data but still break it down to individual elements when necessary?

Enter our amazing loops! With for and while loops we can stop off to visit each value of an array but still work with the entire set of data. Read on to learn how…

Looping through an Array

Click below to begin the Looping through an Array activity.

 

Try It.png Read through the Sound Free Response Question from the 2011 AP CS exam:

FRQ_TraversingArrays.png FRQ2_TraversingArrays.png 

For this free response question your job is to write the limitAmplitude method. Here are some hints to get you started:

  • Create a counter to keep track of the numbers of amplitude changes.
  • Write a loop that traverse samples.
  • Within the loop check to see if each samples element is less than -limit
    • if it is, change that element of the array to -limit and increase the counter
  • Within the loop check to see if each samples element is greater than limit
    • if it is, change that element of the array to limit and increase the counter
  • Return the counter

Here is one possible way to write this method:

public int limitAmplitude (int limit) {
int numChanged = 0;
for (int i = 0;  i < this.samples.length; i++)  {
if (this.samples [i] < -limit)  {
this.samples [i] = -limit;
numChanged++;
}
if (this.samples [i] > limit) {
this.samples [i] = limit;
numChanged++;
}
}
return numchanged;
}
 

ArrayIndexOutOfBoundsException (Error)

When working with arrays it is not uncommon to get an out of bounds error. This means that your code was reaching outside the bounds (or index) of your array.

For example:

double score [ ] = new double [ 5 ];
score [2] = 23.2;
score [3] = 20.1;
score [5] = 22.3;  / / Error! There is no index 5! 

BookIcon.png Click on "Runestone Academy" below to open the required reading that is listed.

 

BookIcon.png Access your text for this class.

  • Read 5 Steps to a 5:
  • Concept 2

Practice

PracticeIcon.png Practice-It! Practice

1. Go to the PracticeIt website Links to an external site..

2. Log into account.

3. Click on Start Practicing!

4. Go to the most recent edition.

5. Click on Chapter 7: Arrays

6. Complete Self-Check: 7.4

7. Complete Exercises: 7.1 – 7.6, 7.8 – 7.9, 7.11 – 7.16, & 7.18

PracticeIcon.png CodingBat Practice

1. Go to the CodingBat website Links to an external site..

2. Create an account (this will allow you to save your work.)

3. Check with your teacher to see if your account should be shared with him/her.

4. Make sure that you are on the Java tab (not Python).

5. Select Array – 1.

6. Complete the Array-1 exercises for free response practice.

7. Select Array – 2.

8. Complete the Array-2 exercises for free response practice.

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS