Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
1 Various Methods of Populating Arrays Randomly generated integers.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Kernighan/Ritchie: Kelley/Pohl:
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
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.
Chapter 7 – Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
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.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Lecture 6: Midterm Review Tami Meredith. Programming Process How do we fill in the yellow box? Text Editor Compiler (javac) Interpreter (JVM: java) User.
Pointers *, &, array similarities, functions, sizeof.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
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.
Lecture 3: Methods, Classes, and Objects Tami Meredith.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIT115: Introduction to Programming
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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];
Lecture 10: Object Oriented Programming Tami Meredith.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays Chapter 7.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
EGR 2261 Unit 10 Two-dimensional Arrays
Introduction to Computer Science / Procedural – 67130
Arrays, For loop While loop Do while loop
Variables ICS2O.
EECE.2160 ECE Application Programming
C++ Array 1.
Presentation transcript:

Lecture 7: Arrays Tami Meredith

Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array

What is data? Data are values that are stored in memory Memory locations are given names Those names are called identifiers A named location is called a variable A type describes the contents of the memory location in general terms so we can make the location the correct size Thus, we have: 1. Memory locations – of some size 2. Types – to tell us things about the value we will store 3. Names – to identify the locations 4. Values – to put in the locations

Variables 1. int i; Defines a section of memory large enough to hold an integer and names that area of memory " i ", the memory contains a random value from whatever it was used for previously 2. i = 5; Stores the value 5 in the area or memory named " i " 3. int i = 5; Combines 1. and 2. A variable holds one, and only one, thing A variable always has a value... if you put nothing there then assume it has a random value

Groups of Variables A block lets us group statements so that they can be used where a single statement is needed An array lets us group variables so that we can store groups of similar values with a single name An array is a set of adjacent (side by side) memory locations We use square brackets, " [ " and " ] " when working with arrays

Aside: Delimiters [ ] Something to do with an array " " A string (sequence of zero or more characters) 'a' A character (a single character, never empty) { } A block (sequence of zero or more statements) /* */ A multi-line comment ( ) A math expression A test in a loop or conditional The parameters of a method call The parameters in a method declaration

Arrays 4 integers stored (somewhere) in memory int a, b, c, d; An array named " e " that stores 4 integers int[] e = new int[4]; a dcb e[0]e[1]e[2]e[3]

Array Syntax: Definition int[] e = new int[4]; type [] name = new type [ number-of-elements ]; type tells us how big each slot needs to be and what it will hold name is what the entire array is called new is a special keyword that means "assign memory" and is always followed by an indicator of how much memory to get (in this case, enough for 4 integers)

Array Syntax: Assignment int[] coords = new int[2]; coords[0] = 3; coords[1] = 5; 0 and 1 are referred to as array indices Arrays are indexed from 0 because the index identifies how many slots we are from the front of the array An index is not a count into the array! (Its actually an offset) Same as Strings (c.f., arrays of characters) coords[0]coords[1] 35

new 1. int x; 2. x = 4; 1. int[] y; 2. y = new int[4]; 3. y[0] = 1; y[1] = 2; y[2] = 4; y[3] = 8; Arrays add the extra step of indicating how many values the array needs to store (and getting the memory for it at that time using new ). x 4 y[0]y[1]y[2]y[3] 1248

new (Part 2) 1. int[] y; 2. y = new int[4]; Step 1 creates a memory slot called y Step 2 creates 4 memory slots called y[0], y[1], y[2], a nd y[3] y stores a. Information to access y[0] to y[3] b. A count of then number of slots created y is secretly an object, it stores a group of data

Indices Strings are indexed from 0 Arrays are indexed from 0 We often have called the counter in a loop as the index value We do this because we frequently use loops to process all the elements (i.e., the value at each index) of arrays and Strings Really, a String is just an array of characters with some special rules

Indices We can use a variable to indicate an index value int x = 0, y = 1; int[] coords = new int[2]; coords[x] = 3; // same as coords[0] coords[y] = 5; // same as coords[1]

Arrays and Loops Arrays work amazingly well with loops! Let us see how to get four integers from a user... int[] vals = new int[4]; int i; Scanner user = new Scanner(System.in); for (i = 0; i <= 4; i++) { vals[i] = user.nextInt(); } /* * Same as: vals[0] = user.nextInt(); * vals[1] = user.nextInt(); * vals[2] = user.nextInt(); * vals[3] = user.nextInt(); */

Exercise Given the array below, write a loop to fill the array with the values 0 to 9 int[] vals = new int[10]; // your loop...

Solution Given the array below, write a loop to fill the array with the values 0 to 9 (see arrayfill.java) int[] vals = new int[10]; int i; // i is traditionally an array index for (i = 0; i < 10; i++) { vals[i] = i; }

Improving our solution Arrays have their length stored Rather than write code that is based on some value that could change, we can tie the loop to the array so that if the array changes, the loop changes int[] vals = new int[10]; int i; for (i = 0; i < vals.length; i++) { vals[i] = i; }

Length For any array called name, name.length returns the length of the array The length is the number of memory slots available, NOT the number of memory slots that have been assigned values int[] vals = new int[4]; vals[0] = vals.length; vals[0] holds the value 4 even though vals[1] to vals[3] have not been used

A tiny quirk Arrays store their length in a variable named length We directly access the variable to obtain the length of the array Strings store their length in a hidden (private) variable We use the length() method to obtain the length of the String This is why we need () with Strings and not arrays Both Strings and arrays are actually objects

A Problem int sum (int[] data) { int i, sum = 0; for (i = 0; i < data.length; i++) { sum = sum + data[i]; } return(sum); } Problem... what happens when data isn't filled? That is, what if some of the locations do not contain valid values? E.g., data[i] does not have a value?

Solution1 Keep track of the number of used locations in a separate variable (e.g., size in this example) Can waste space and requires extra data management int sum (int[] data, int size) { int i, sum = 0; for (i = 0; i < size; i++) { sum = sum + data[i]; } return(sum); }

Solution2 Resize the array so that no empty locations exist Very inefficient, not supported in Java Must make a new array of the correct (smaller) size and copy values to it from the old array int size; // number of values used in data int[] dataCopy = new int[size]; int i; for (i = 0; i < size; i++) { dataCopy[i] = data[i]; } Now we can use dataCopy in place of data and dataCopy.length is correct – length and size(number of used elements) is the same

Powers of 2: An example powers[n] = 2 n powers = 1, 2, 4, 8, 16,..., 2 31 int[] powers = new int[32]; int i; powers[0] = 1; // 2^0 = 1 for (i = 1; i < 32; i++) { powers[i] = 2 * powers[i-1]; }

More Improvements Literals are generally a bad thing Named constants are seen as better More readable Only defined once – change the definition and you change all the uses of the constant Named constants usually have their NAME IN CAPS

Example public class powers { public static final int COUNT = 32; public static void main (String[] args) { int[] pow2 = new int[COUNT]; int i; pow2[0] = 1; // 2^0 = 1 for (i = 1; i < COUNT; i++) { pow2[i] = pow2[i-1] + pow2[i-1]; } } // end main() } // end class powers

Classes and Static Classes group methods and data In class powers, we had a method, main, and some data, count stati c means there is only one copy – we need the class to only have one copy of main because we can't have two starting points! since main is static, it can only use other (external) things that are static – that is why COUNT is static as well

Loop Variables Often, we define a variable only to use within loops We can actually "hide" this variable inside the loop by defining it there for (int i = 0; i < data.length; i++) { // i can only be used here } // i cannot be used here (outside the loop) i can only be used inside the loop since it is defined inside the loop i does not exist before, or after, the loop

Slightly Shorter Example public class powers { public static final int COUNT = 8; public static void main (String[] args) { int[] pow2 = new int[COUNT]; pow2[0] = 1; // 2^0 = 1 for (int i = 1; i < COUNT; i++) { pow2[i] = pow2[i-1] + pow2[i-1]; } } // end main() } // end class powers

Initialisation We can initialise an array by assigning it a value int[] colour = { 255, 0, 0 }; Automatically calculates the size we need based on the value Uses curly braces { and }, not brackets [ and ] Think of the initialiser as a "block of values"

Exercise The array hours = {3, 5, 9, 7, 6} stores the number of hours that Julie worked for the 5 days of week. If Julie makes $12.50 an hour, write a program to: 1. Calculate and store in an array, the number of $ Julie made each day. 2. Calculate and print her average daily income.

Solution public class income { public static final double SALARY = 12.50; public static final int DAYSOFWEEK = 5; public static void main (String[] args) { int[] hours = {3, 5, 9, 7, 6}; double[] earned = new double[DAYSOFWEEK]; double sum = 0.0; for (int i = 0; i < DAYSOFWEEK; i++) { earned[i] = ((double) hours[i]) * SALARY; sum += earned[i]; } System.out.println("Average daily income: " + (sum/((double) DAYSOFWEEK))); } // end main() } // end class income

Exercise A tic-tac-toe board has rows and columns Any square on the board can be described as (row, col) Here is the output where all the rows and columns are identified: (1,1), (1,2), (1,3) (2,1), (2,2), (2,3) (3,1), (3,2), (3,3) Write a program using nested loops (one for the rows and one for the columns) that will generate this output

Solution public class tictactoe { public static void main (String[] args) { for (int r = 1; r < 4; r++) { for (int c = 1; c < 4; c++) { System.out.print("("+r+","+c+")"); if (c < 3) System.out.print(", "); } // end c loop System.out.println(""); } // end r loop } // end main() } // end class tictactoe

Two Dimensions Each row on the board could be an array Thus, we could make the board as an array of arrays E.g., one array of length 3, each slot holding another array of length 3 (the rows) One array that holds 3 arrays! 0holds

2 Dimensional Arrays int rows = 3; int cols = 3; int[][] board = new int[rows][cols]; board[0][0]board[0][1]board[0][2] board[1][0]board[1][1]board[1][2] board[2][0]board[2][1]board[2][2]

Accessing with Loops int rows = 3; int cols = 3; int val = 1; int[][] board = new int[rows][cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { board[r][c] = val++; } board[0][0] = 1board[0][1] = 2board[0][2] = 3 board[1][0] = 4board[1][1] = 5board[1][2] = 6 board[2][0] = 7board[2][1] = 8board[2][2] = 9

Another 2 Dimensional Array int rows = 4; int cols = 2; int[][] board = new int[rows][cols]; board[0][0]board[0][1] board[1][0]board[1][1] board[2][0]board[2][1] board[3][0]board[3][1]

Sorting It is convenient to be able to sort an array There is a provided set of methods that sort an array for us These methods are static (rely on no data except that which is given to them) and thus no object needs to be instantiated java.util.Arrays (data); will sort the array named data Works on integers, strings, floats (anything that can be ordered)

Example import java.util.Arrays; public static void fill (int[] data) { for (int i = data.length - 1; i >= 0; i--) { data[i] = i; } Arrays.sort(data); }

Now What? No more additional chapters!! Now we go back and start: 1. Reviewing 2. Covering things we skipped 3. Consolidating concepts Now we can slow down and enjoy the scenery!

To Do Go to the lab and complete Assignment 6 Read Chapter 7 if you have not done so already! Go back and start re-reading Chapters 1-6 until you *REALLY* understand the material in them Practice, Practice, Practice! Program until your eyeballs fall out! (It is covered under your student health plan, I checked).