Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle."— Presentation transcript:

1 More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle

2 2 Array Routines Last class we looked at routines for manipulating the contents of arrays in different ways. These included: Last class we looked at routines for manipulating the contents of arrays in different ways. These included: Reading in array values Reading in array values Printing array values Printing array values Summing array values Summing array values Averaging array values Averaging array values Now we’re going to explore how to search an array for a specific value. Now we’re going to explore how to search an array for a specific value.

3 3 Searching an Array Given an array grades, how would we go about searching for a specific grade ? Given an array grades, how would we go about searching for a specific grade ? Any suggestions? Any suggestions? For example, what program structures might we use? For example, what program structures might we use? Should we do the searching in the main part of the program or in a method? Should we do the searching in the main part of the program or in a method? Questions to consider: Questions to consider: What are the advantages/disadvantages of each approach? What are the advantages/disadvantages of each approach? Which approach do you think would be considered better programming? Which approach do you think would be considered better programming? If we decide to search for the grade in a method, do we know how to pass an array to a method? If we decide to search for the grade in a method, do we know how to pass an array to a method? In other words, have we learned this yet? In other words, have we learned this yet?

4 4 Passing an Array to a Method Example: Example: // Search array "grades" for the grade entered by the // user; if found, return the number of the element // in which it was found; if not found, return the // final value of "j" (equal to the size of the // array) public static int findGrade(int grades[], int grade) { int j; for (j = 0; j < grades.length; j++) { if (grades[j] == grade) break; // Allows us to "break" out of a // loop prematurely } // end for return j; } // end findGrade // Search array "grades" for the grade entered by the // user; if found, return the number of the element // in which it was found; if not found, return the // final value of "j" (equal to the size of the // array) public static int findGrade(int grades[], int grade) { int j; for (j = 0; j < grades.length; j++) { if (grades[j] == grade) break; // Allows us to "break" out of a // loop prematurely } // end for return j; } // end findGrade Note that it is not necessary to provide the size of the array.

5 5 Sample Program Let’s look at a sample program that demonstrates how to pass an array to a method: Let’s look at a sample program that demonstrates how to pass an array to a method: gradeSearch.java gradeSearch.java gradeSearch.java

6 6 Parallel Arrays Parallel arrays are two or more arrays that store related information. Parallel arrays are two or more arrays that store related information. Since you will be using parallel arrays in your lab on Friday, we’ll now look at a sample program that uses parallel arrays to store degree values and their corresponding radian values: Since you will be using parallel arrays in your lab on Friday, we’ll now look at a sample program that uses parallel arrays to store degree values and their corresponding radian values: degreesToRadians.java degreesToRadians.java degreesToRadians.java

7 7 Passing by Value vs. Passing by Reference What’s the difference? What’s the difference? Passing by value Passing by value The method makes a copy of the value passed to it, which is usually a primitive type such as an int or a float. The method makes a copy of the value passed to it, which is usually a primitive type such as an int or a float. Changes made to the value in the called method are not reflected in the calling method. Changes made to the value in the called method are not reflected in the calling method. Passing by reference Passing by reference The method is provided with the address of the value passed to it, which is usually a complex type such as an array or an object. The method is provided with the address of the value passed to it, which is usually a complex type such as an array or an object. Because the method has been given access to the actual value, changes made to the value in the called method ARE reflected in the calling method. Because the method has been given access to the actual value, changes made to the value in the called method ARE reflected in the calling method.

8 8 Passing by Value vs. Passing by Reference Example: Example: // Search array "grades" for the grade entered by the // user; if found, return the number of the element // in which it was found; if not found, return the // final value of "j" (equal to the size of the // array) public static int findGrade(int grades[], int grade) { int j; for (j = 0; j < grades.length; j++) { if (grades[j] == grade) break; // Allows us to "break" out of a // loop prematurely } // end for return j; } // end findGrade // Search array "grades" for the grade entered by the // user; if found, return the number of the element // in which it was found; if not found, return the // final value of "j" (equal to the size of the // array) public static int findGrade(int grades[], int grade) { int j; for (j = 0; j < grades.length; j++) { if (grades[j] == grade) break; // Allows us to "break" out of a // loop prematurely } // end for return j; } // end findGrade Array “grades” is passed by reference. Integer “grade” is passed by value.

9 9 Passing by Value vs. Passing by Reference Example: Example: // Fill degreeValues with values that are multiples of 30 (0 // through 330); fill corresponding elements of radianValues // with equivalent radian values public static void populateArrays(double degreeValues[], double radianValues[]) { int i; double degrees = 0.0, increment = 30.0; for (i = 0; i < Size; i++) { degreeValues[i] = degrees; radianValues[i] = degrees * ConversionFactor; degrees += increment; } // end for } // end populateArrays // Fill degreeValues with values that are multiples of 30 (0 // through 330); fill corresponding elements of radianValues // with equivalent radian values public static void populateArrays(double degreeValues[], double radianValues[]) { int i; double degrees = 0.0, increment = 30.0; for (i = 0; i < Size; i++) { degreeValues[i] = degrees; radianValues[i] = degrees * ConversionFactor; degrees += increment; } // end for } // end populateArrays Both arrays are passed by reference.


Download ppt "More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle."

Similar presentations


Ads by Google