11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.

Slides:



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

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
True or false A variable of type char can hold the value 301. ( F )
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
1 Fall 2008ACS-1903 Chapter 8: Arrays 8.1 – 8.8 Introduction to Arrays Processing Array Contents Passing Arrays as Arguments to Methods Some Useful Array.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
C++ for Engineers and Scientists Third Edition
Algorithm & Flowchart Credit: Mr Ainullotfi.
1 Chapter 4 Simple Selections and Repetitions INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
A First Book of ANSI C Fourth Edition
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Chapter 8: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
Language Find the latest version of this document at
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
CSC 211 Java I for loops and arrays.
Algorithm & Flowchart.
REPETITION CONTROL STRUCTURE
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Arrays, For loop While loop Do while loop
Introduction to Programming
CNG 140 C Programming (Lecture set 8)
Relational Operators Operator Meaning < Less than > Greater than
Starting Out with Programming Logic & Design
Dr. Sampath Jayarathna Cal Poly Pomona
Introduction to Computer Science
design OO words Debug Variables Data types
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Presentation transcript:

11 PART 2 ARRAYS

22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored in oldValues to the reference variable named newValues. It does not make a copy of the contents of the array referenced by oldValues. int[ ] oldValues = {10, 100, 200, 300}; int[ ] newValues = new int[4]; newValues = oldValues; // Does not make a copy of the contents of the array ref. // by oldValues

33 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables After the following statements execute, we have the situation illustrated below: int[ ] oldValues = {10, 100, 200, 300}; int[ ] newValues = new int[4]; When the assignment statement below executes we will have: newValues = oldValues; // Copies the address in oldValues into newValues [0][1][2][3] [0][1][2][3] address oldValues address newValues [0][1][2][3] address oldValues address newValues

44 PROCESSING ARRAY ELEMENTS Copying The Contents of One Array to Another Array To copy the contents of one array to another you must copy the individual array elements.

55 PROCESSING ARRAY ELEMENTS Copying The Contents of One Array to Another Array To copy the contents of the array referenced by oldValues to the array referenced by newValues we could write: int count; short[ ] oldValues = {10, 100, 200, 300}; short[ ] newValues = new short[4]; // If newValues is large enough to hold the values in oldValues if (newValues.length >= oldValues.length) { for (count = 0; count < oldValues.length; count++) { newValues[count] = oldValues[count]; }

66 SOME USEFUL ARRAY OPERATIONS Comparing Arrays The decision in the segment below does not correctly determine if the contents of the two arrays are the same. char[ ] array1 = {'A', 'B', 'C', 'D', 'A'}; char[ ] array2 = {'A', 'B', 'C', 'D', 'A'}; boolean equal = false; if (array1 == array2) // This is a logical error { equal = true; }

77 SOME USEFUL ARRAY OPERATIONS Comparing Arrays We are comparing the addresses stored in the reference variables array1 and array2. The two arrays are not stored in the same memory location so the conditional expression is false and the value of equal stays at false. char[ ] array1 = {'A', 'B', 'C', 'D', 'A'}; char[ ] array2 = {'A', 'B', 'C', 'D', 'A'}; boolean equal = false; if (array1 == array2) // This is false - the addresses are not equal { equal = true; }

88 SOME USEFUL ARRAY OPERATIONS Comparing Arrays To compare the contents of two arrays, you must compare the individual elements of the arrays. Write a loop that goes through the subscripts of the arrays comparing the elements at the same subscript/offset in the two arrays.

99 SOME USEFUL ARRAY OPERATIONS Comparing Arrays int index; boolean sameSoFar = true; if (firstArray.length != secArray.length) { sameSoFar = false; } else { index = 0; while (sameSoFar && index < firstArray.length) { if (firstArray[index] != secArray[index]) { sameSoFar = false; } index ++; // Incr. the counter used to move through the index }

10 SOME USEFUL ARRAY OPERATIONS Finding the Sum of the Values in a Numeric Array To find the sum of the values in a numeric array, create a loop to cycle through all the elements of the array adding the value in each array element to an accumulator variable that was initialized to zero before the loop.

11 SOME USEFUL ARRAY OPERATIONS Finding the Sum of the Values in a Numeric Array int offset; double sum; for (sum = 0, offset = 0; offset < array.length; offset++) { sum += array[offset]; }

12 SOME USEFUL ARRAY OPERATIONS Finding the Average of the Values in a Numeric Array To find the average of the values in a numeric array, first find the sum of the values in the array. Then divide this sum by the number of elements in the array.

13 SOME USEFUL ARRAY OPERATIONS Finding the Average of the Values in a Numeric Array int offset; double sum; double average; for (sum = 0, offset = 0; offset < array.length; offset++) { sum += array[offset]; } average = sum / array.length;

14 SOME USEFUL ARRAY OPERATIONS Finding the Highest and Lowest Values in a Numeric Array To find the highest value in a numeric array, copy the value of the first element of the array into a variable called highest. This variable holds a copy of the highest value encountered so far in the array. Loop through the subscripts of each of the other elements of the array. Compare each element to the value currently in highest. If the value of the element is higher than the value in highest, replace the value in highest with the value of the element. When the loop is finished, highest contains a copy of the highest value in the array.

15 SOME USEFUL ARRAY OPERATIONS Finding the Highest and Lowest Values in a Numeric Array int subscript; double highest = array[0]; for (subscript = 1; subscript < array.length; subscript++) { if (array[subscript] > highest) { highest = array[subscript]; }

16 SOME USEFUL ARRAY OPERATIONS Finding the Highest and Lowest Values in a Numeric Array The lowest value in an array can be found using a method that is very similar to the one for finding the highest value. Keep a copy of the lowest value encountered so far in the array in a variable, say lowest. Compare the value of each element of the array with the current value of lowest. If the value of the element is less than the value in lowest, replace the value in lowest with the value of the element. When the loop is finished, lowest contains a copy of the lowest value in the array.

Array of strings 17

Array of strings 18 Output: