Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.

Slides:



Advertisements
Similar presentations
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,
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 7 Arrays.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Computer Programming Lab(5).
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
 Review  Quick reference Announcement. Chapter 1: Basics.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Top-down programming design 1 Top-down design is a programming style, the mainstay of traditional procedural languages, in which design begins by specifying.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
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];
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Objectives  File I/O: using Scanner with File  Inserting into Partially Filled Array  Deleting from Partially Filled Array  Static methods and variables.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 6 Arrays 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Single Dimensional Arrays
Chapter 7: Single-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Chapter 7 Single-Dimensional Arrays
Chapter 5 Function Basics
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 7 Arrays Data structures Related data items of same type
Chapter 6 Arrays.
Chapter 6 Arrays Solution Opening Problem
Chapter 6 Arrays.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 5 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Presentation transcript:

Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles

Why array?  Store a large number of values during the execution of a program.  What is array? – A fixed-size sequential collection of elements of the same type. Fall 2012 CS2302: Programming Principles 2

Basic Operations  Declaration: – elementType[ ] arrayRefVar;  Example: – double[ ] myList; Fall 2012 CS2302: Programming Principles 3

Basic Operations  Creation: – arrayRefVar = new elementType[arraySize];  Example: – myList = new double[10];  Declaring and Creating in one step: – dobule[ ] myList = new double[10]; Fall 2012 CS2302: Programming Principles 4

Basic Operations  Element identification: – arrayRefVar[index];  Example: – myList[9]; // represents the last element in the array myList.  Index range: – From 0 to arrayRefVar.length - 1 Fall 2012 CS2302: Programming Principles 5

Basic Operations  Assigning values – arrayRefVar[index] = value;  Example: – myList[5] = 34.33; Fall 2012 CS2302: Programming Principles 6

Basic Operations  Array initialization with declaration  Declaring, creating, initializing in one step: – double[ ] myList = {1.9, 2.9, 3.4, 3.5}; Fall 2012 CS2302: Programming Principles 7

Objectives  Pass arrays to methods  Return an Array from a method – Reverse Arrays  Partially Filled Arrays – Insert into a partially filled array Fall 2012 CS2302: Programming Principles 8

Pass by value  Most methods are passed arguments when they are called. An argument may be a constant or a variable.  Math.sqrt(33 )  Math.sqrt(x)  If a variable is passed, the method receives a copy of the variable's value. The value of the original variable cannot be changed within the method.  because the method only has a copy of the value; it does not have access to the original variable.  This process is called pass by value. Fall 2012 CS2302: Programming Principles 9

Pass by Value public class TestPassByValue { /** Main method */ public static void main(String[] args) { // Declare and initialize variables int num1 = 1; int num2 = 2; System.out.println("Before invoking the swap method, num1 is " + num1 + " and num2 is " + num2); // Invoke the swap method to attempt to swap two variables swap(num1, num2); System.out.println("After invoking the swap method, num1 is " + num1 + " and num2 is " + num2); } /** swap method - Swap two variables */ public static void swap(int n1, int n2) { System.out.println("\tInside the swap method"); System.out.println("\t\tBefore swapping n1 is " + n1 + " n2 is " + n2); // Swap n1 with n2 int temp = n1; n1 = n2 n2 = temp; System.out.println("\t\tAfter swapping n1 is " + n1 + " n2 is " + n2); } } Fall 2012 CS2302: Programming Principles 10

Passing Arrays to Methods public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } //Invoke the method int[] list = {3, 1, 2, 6, 4, 2}; //initialize array printArray(list); //Invoke the method printArray(new int[]{3, 1, 2, 6, 4, 2}); Anonymous array Fall 2012 CS2302: Programming Principles 11

public class Test { public static void main(String[] args) { int x = 1; // x represents an int value int[] y = new int[10]; // y represents an array of int values m(x, y); // Invoke m with arguments x and y System.out.println("x is " + x); System.out.println("y[0] is " + y[0]); } public static void m(int number, int[] numbers) { number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] } Simple Example Fall 2012 CS2302: Programming Principles 12

Passing Arrays as Arguments  Objective: Demonstrate differences of passing primitive data type variables and array variables. Fall 2012 CS2302: Programming Principles 13 Run TestPassArray

Objectives  Pass arrays to methods  Return an Array from a method – Reverse Arrays  Partially Filled Arrays – Insert into a partially filled array Fall 2012 CS2302: Programming Principles 14

Objectives  Pass arrays to methods  Return an Array from a method – Reverse Arrays  Partially Filled Arrays – Insert into a partially filled array Fall 2012 CS2302: Programming Principles 15

Ending a Loop with a Sentinel Value Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a special input values is known as a sentinel value. Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. SentinelValue Fall 2012 CS2302: Programming Principles 16 Run