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

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

C Language.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Kernighan/Ritchie: Kelley/Pohl:
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Complexity Analysis (Part I)
Repetition Structures: Nested Loops CSC 1401: Introduction to Programming with Java Week 6 – Lecture 2 Wanda M. Kunkle.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Java vs. You.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Computer Science 210 Computer Organization Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Chapter 9 Introduction to Arrays Fundamentals of Java.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Arrays Chapter 7.
Chapter 7 User-Defined Methods.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Introduction to Programming
EKT150 : Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 7: User-Defined Functions II
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

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

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 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 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 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 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 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 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 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.