Introduction to Arrays

Slides:



Advertisements
Similar presentations
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Advertisements

Chapter 8: Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
 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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 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.
 Pearson Education, Inc. All rights reserved Arrays.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
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.
Chapter 5 Loops.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Arrays Chapter 7.
CSC111 Quick Revision.
Correctness issues and Loop invariants
2.5 Another Java Application: Adding Integers
Chapter 8 Arrays Objectives
Java Programming: From Problem Analysis to Program Design, 4e
Arrays An Array is an ordered collection of variables
Lists in Python.
Testing change to a linked list
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
CS100J Lecture 11 Previous Lecture This Lecture
python.reset() Also moving to a more reasonable room (CSE 403)
CS2011 Introduction to Programming I Arrays (I)
Arrays Chapter 7.
int [] scores = new int [10];
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
CS150 Introduction to Computer Science 1
CSC 142 Arrays [Reading: chapter 12].
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
CS150 Introduction to Computer Science 1
Introduction to Arrays
Linear and Binary Search
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
(We’ll spend a few lectures on iteration)
CS100A Sections Dec Loop Invariant Review C Review and Example
Arrays Introduction to Arrays Reading for this Lecture:
CS100A Lect. 12, 8 Oct More About Arrays
loops revisited I/O arrays
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

Introduction to Arrays CS100A Lect. 11, 6 Oct. 1998 Introduction to Arrays Array declaration and allocation Subscripting Use of arrays to store collections of data Reading in Holmes for arrays: Chapter 5 (p. 147) We’ll spend several lectures using arrays. CS100A, Lecture 11, 6 October 1998

Data processing problem Input: zero or more grades in the range 0..100, preceded by the number of grades Sample input: 5 90 85 93 40 89 Task: Read grades and print information about them: Print grades in print grades in the reverse of the order given. e.g 89 40 93 85 90 Print grades in increasing order. e.g. 40 85 89 90 93 Print histogram. Thoughts: 0. Need to read in all the values before doing any processing. 1. Need as many variables as elements of the array. 2. Impossible with knowledge of Java that we now have --what if there are hundreds of grades to deal with? Need a different variable for each one? CS100A, Lecture 11, 6 October 1998

The ARRAY helps solve the problem g g[0] is 90 g[1] is 85 g.length is the number of array g[2] is 93 elements in array g g[3] is 40 g.length = 5 g[4] is 89 in “g[4]”, 4 is the “subscript” or “index” h.length is the number of array elements in array h h.length = 206 90 85 93 40 89 0 1 2 3 4 90 85 93 40 89 … 60 32 0 1 2 3 4 … 204 205 h CS100A, Lecture 11, 6 October 1998

Conventions g h i j k Represents 3 array segments or sections: segment number of values in it empty when g[h..i-1] i-h h = i g[i..j] j+1-i j = i-1 g[j+1..k-1] k-(j+1) j = k h i j k CS100A, Lecture 11, 6 October 1998

Declaration of a variable that can contain an array int g; g float averages; averages Face faces; face Declaring a variable does not “allocate” or create the array of elements; it only declares a variable that can contain a reference to an array of elements. An array is much like a class in that an array variable contains a reference to an array. null null null CS100A, Lecture 11, 6 October 1998

Allocating an array of elements g = new int [5]; g Face f = new Face[206]; f What does execution of f[3]= new Face(…); do? Given allocated array f, we can reference f[0], f[1], … f[205]. But also use any expression for the subscript: f[i], f[2*i], etc. 0 1 2 3 4 null null null null null ... null null 0 1 2 3 4 … 204 205 CS100A, Lecture 11, 6 October 1998

Read in grades, print in reverse order // Read in a list of integer grades, preceded by the // number of grades, and print in them in reverse order public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int n= in.readInt(); // number of grades int[ ] g= new int [n]; // g[0..n-1] are the grades // Read in the grades int i= 0; // Inv: i grades have been read in and are in g[0..i-1] while (i != g.length) { g[i]= in.readInt( ); i= i+1; } // Print grades in reverse order int k= n; // Inv: grades in g[k..length-1] have been printed while (k != 0) { System.out.println(g[k]); k= k-1; CS100A, Lecture 11, 6 October 1998

Program scheme to print “histogram” of grades // Read in a list of grades in the range 0..100, preceded // by the number of grades, and // print out how many times each grade appears public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will contain the no. // of times grade f appears // Initialize frequencies f[0..100] to 0. // Read in the grades and make up array f. // Print the grades and their frequencies (print only // the grades in 0..100 that appeared at least once in // the input) } CS100A, Lecture 11, 6 October 1998

Program to print “histogram” of grades // Read in a list of grades in the range 0..100, preceded // by the number of grades, and // print out how many times each grade appears public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will contain the no. // of times grade f appears // Initialize frequencies f[k] to 0. int k= 0; // Inv: Each element of f[0..k-1] is 0 while (k != f.length) {f[k]= 0; k= k+1;} (continued on next slide) CS100A, Lecture 11, 6 October 1998

Program to print “histogram” of grades (continued) // Read in the grades and make up array f int i= 0; // Inv: i grades have been read in and // each f[k], for 0<=k<=100, contains // the no. of times grade k was read in while (i != f.length) { int grade= in.readInt( ); f[ grade]= f[grade] + 1; i= i+1; } // Print the grades and their frequency (print only the // grades in 0..100 that appeared at least once in the // input) // Inv: the grades 0..i-1 that occurred at least // once have been printed if (f[i] != 0) System.out.println(“grade: ” + i + “ frequency: ” + f[i]); i= i+1; CS100A, Lecture 11, 6 October 1998

Is a string a word a palindrome? A palindrome is a word that reads the same backwards and forwards. The empty string A AA ABA NOON The following palindromes if blanks and punctuation are ignored able was I ere I saw elba a man a plan a canal panama Look at the web site for this lecture to see the longest palindrome that we know of --is it loooooooooooong! CS100A, Lecture 11, 6 October 1998

0 i j length-1 x rev. of x // Return the value of the statement // “array b is a palindrome” static public bool isPalindrome(char[ ] b) { int i= 0; int j= b.length; // Invariant: b is a palindrome iff b[i..j-1] is. In other // words, b[j..length-1] is the reverse of b[0..i-1] while ( j-i >1) { j= j-1; if (b[i] != b[j]) return false; i= i+1; } // {b[i..j-1] has 0 or 1 elements, so it’s a palindrome} return true; 0 i j length-1 x rev. of x CS100A, Lecture 11, 6 October 1998