13 Arrays CE00858-1: Fundamental Programming Techniques June 161.

Slides:



Advertisements
Similar presentations
Chapter 10 Introduction to Arrays
Advertisements

Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
Line Efficiency     Percentage Month Today’s Date
Chapter 10.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
15 Sorting1June Sorting CE : Fundamental Programming Techniques.
18 File handling1June File handling CE : Fundamental Programming Techniques.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
07 Further testing1June Further selection CE : Fundamental Programming Techniques.
11 Methods1June Methods CE : Fundamental Programming Techniques.
Chapter 9 Introduction to Arrays
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
CS0007: Introduction to Computer Programming Introduction to Arrays.
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
Chapter 9: Advanced Array Concepts
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
14 BirthMonth1February BirthMonth CE : Fundamental Programming Techniques.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Jan 2016 Solar Lunar Data.
Q1 Jan Feb Mar ENTER TEXT HERE Notes
CSC 142 Computer Science II
Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
Arrays, For loop While loop Do while loop
Visual Basic .NET BASICS
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Java Programming Loops
Mammoth Caves National Park, Kentucky
2017 Jan Sun Mon Tue Wed Thu Fri Sat
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Calendar Year 2009 Insure Oklahoma Total & Projected Enrollment
Jan Sun Mon Tue Wed Thu Fri Sat
Building Java Programs
Electricity Cost and Use – FY 2016 and FY 2017
Java Programming Loops
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

13 Arrays CE : Fundamental Programming Techniques June 161

13 Arrays Objectives In this lecture, we will introduce the concept of an array introduce the syntax of arrays and the keyword new discuss array initialization write some simple examples using arrays June 162

13 Arrays Single values variables are used to hold data about a single thing: what if we want to store data about lots of items? number of days in each month 20 mark June 163

13 Arrays Many values of same type one declaration for each month code would need to be repeated 12 times program length would be unmanageable implementation would be extremely error-prone testing would take vast amounts of time debugging would be highly time-consuming Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec June 164

13 Arrays Arrays group similar items into a single logical unit consecutive memory locations each holds data of the same type array has a single name upper and lower bounds indicate size each element is identified using array name and index monthDaysArray name Lower boundArray indexmonthDays (8)Upper bound June 165

13 Arrays Array bounds bounds of an array refer to first and last element that can be indexed first item: 0 last item: length – 1 index is checked against the bounds of the array if index is out of range an exception is thrown ArrayIndexOutOfBoundsException if not handled the program will terminate June 166

13 Arrays Array declaration to declare an array, the programmer specifies: the type of data to be stored a name that refers to the whole structure the size of the array June 167 int [ ] monthDays = new int [12]; monthDays

13 Arrays Array declaration and initialisation June 168 int [ ] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; monthDays array can also be initialised when it is created: the type of data to be stored a name that refers to the whole structure the values to be stored

13 Arrays String arrays String [] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; June 169 Jan 0 Feb 1 Mar 2 Apr 3 May 4 June 5 July 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11 monthName arrays can be any type, as long as all elements are the same type:

13 Arrays Accessing individual elements each individual element is uniquely identified by: array name index or position, enclosed by [] first element is in position 0 each element is of type indicated in declaration June 1610 monthDays[1] = monthDays[1] + 1; monthDays

13 Arrays Accessing elements example – DaysInMonth problem: output number of days in month input by user June 1611 analysis: what data is used? monthNum: integer input by user monthDays: integer array set to number of days in each month monthNames: string array set to names of each month what operations are performed? create Scanner initialise monthDay array to number of days in each month initialise monthName array to names of months input monthNum subtract 1 from monthNum to get value in range 0 – 11 output number of days in monthNum location of monthDays

13 Arrays DaysInMonth code Scanner kybd = new Scanner (System.in); int [] monthDay = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String [] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; System.out.print("Which month?"); int monthNum = kybd.nextInt(); monthNum = monthNum – 1; System.out.println("There are " + monthDay[monthNum] + " days in " + monthName[monthNum]); June 1612 DaysInMonth.java

13 Arrays Accessing all elements for loop can be used to step through the items can perform same operation on each location in an array value of loop counter used as index need to start at location 0 and go up to array length array has property called length that holds the length of the array accessed using array name followed by.length June 1613

13 Arrays Accessing all elements – DaysInAllMonths problem: output number of days in each month in turn June 1614 analysis: what data is used? monthDays: integer array set to number of days in each month monthNames: string array set to names of each month what operations are performed? iteration needed as days in each month need to be output what operations are done once before the loop? initialise monthDay array to number of days in each month initialise monthName array to names of months how many times is loop repeated? loop repeated for each month, month = 0 to length of array what operations are repeated inside loop? output number of days in month what operations are done once after the loop? none

13 Arrays DaysInAllMonths code int [] monthDay = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; String [] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}; for (int i = 0; i < monthDay.length; i++) { System.out.println("There are " + monthDay[i] + " days in " + monthName[i]); } June 1615 DaysInAllMonths.java

13 Arrays Reading data into an array - Largest data can be read into an array in a similar way to reading into a variable June 1616 problem: read 10 integers and store them in an array find and output the largest value in the array and its location

13 Arrays Largest analysis analysis : what data is used? numbers: integer array used to store 10 integers input by user largest: integer to hold largest value calculated by program largestLocation: integer to hold the location of the largest value calculated by program what constructs are needed? iteration to read data in second iteration to find largest element and its location selection inside second iteration as largest value needs to be stored what operations are done once before loop to read in data? create Scanner create array to hold 10 integers how many times is loop to read in data repeated? loop repeated 10 times, i = 0 to 9 what operations are repeated inside loop to read in data? read number into next array location June 1617

13 Arrays Largest analysis what operations are done once after loop to read in data? store contents of first array location in largest store position of first array location (ie. 0) in largestLocation how many times is loop to find largest repeated? loop repeated 9 times (do not need to check first location), i = 1 to 9 what operations are repeated inside loop to find largest? check the contents of the array location against largest what operations are done if the contents of the array location are greater than largest? store contents of array location in largest store position of array location (ie. i) in largestLocation what operations are done once after loop to find largest? output largest output largestLocation June 1618

13 Arrays Largest code Scanner kybd = new Scanner(System.in); //create array to hold 10 integers int [ ] numbers = new int [10]; System.out.println("Enter 10 integers: " ); //read 10 integers into array for (int i = 0; i < numbers.length; i++) { numbers[i] = kybd.nextInt(); } //initialise largest value and location to first array element int largest = numbers[0]; int largestLocation = 0; June 1619 Continued on next slide Largest.java

13 Arrays //step through each array location for (int i = 1; i < numbers.length; i++) { //check if array location is new largest if (numbers[i] > largest) { largest = numbers[i]; largestLocation = i; } //output value and location of largest element System.out.println("Largest value: " + largest); System.out.println("Location of largest value: " + largestLocation); June 1620

13 Arrays Summary In this session we have: discussed arrays covered the difference between an array location and its contents seen how arrays are declared and initialised in Java implemented examples that store data in arrays In the next session we will: analyse and implement a program that uses arrays June 1621