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.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional 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.
CS150 Introduction to Computer Science 1
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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[]
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
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,
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
ISBN Chapter 7 Expressions and Assignment Statements.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
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];
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Introduction to programming in java Lecture 21 Arrays – Part 1.
UCT Department of Computer Science Computer Science 1015F Iteration
Introduction to Arrays
OPERATORS (2) CSC 111.
Lecture Notes – Week 3 Lecture-2
Arrays .
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Introduction to Arrays
CS100J Lecture 13 Previous Lecture Java constructs
CS100J Lecture 15 Previous Lecture This Lecture Sorting
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

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 n classes, class variables, and instance variables n This Lecture –Java Constructs n arrays and indexing n increment and decrement n for-statements –Reading: n Lewis and Loftus, Sections 3.8 and n Savitch, Sections 6.1 – 6.3

CS 100Lecture 112 Motivating Example n Given a list of 0 or more grades (between 0 and 100) followed by -1, print a histogram of the grades, i.e., a table of grades and their frequencies. gradefrequency …… n Is there a program pattern that applies? n What must be done for each grade? n What must be done before reading any grades? n What must be done after reading all grades? n How many separate counters are needed?

CS 100Lecture 113 A Relevant Pattern where  : /* Initialize 101 counters to 0. */  /* Increment counter for grade. */  /* Print histogram. */ /* “Process” grades until (but not including) a stopping signal of -1. */  grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) {  grade = in.readInt(); grade = in.readInt(); }  int grade; // the grade being processed.

CS 100Lecture 114 A Tedious Solution int grade; // the grade being processed. // For i in , freqi is # of grades of i. int freq0, freq1,..., freq100; int freq0, freq1,..., freq100; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ /* Initialize 101 counters to 0. */  freq0 = 0; freq1 = 0;...; freq100 = 0; grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) { while (grade != -1 ) { /* Increment counter for grade. */ /* Increment counter for grade. */ if ( grade == 0 ) freq0 = freq0 + 1; else if ( grade == 1 ) freq1 = freq1 + 1; else... if ( grade == 100 ) freq100 = freq ; grade = in.readInt(); grade = in.readInt(); } /* Print histogram. */ /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println(”Grade Frequency”); System.out.println( 0 + ” ” + freq0 ); System.out.println( 0 + ” ” + freq0 ); System.out.println( 1 + ” ” + freq1 ); System.out.println( 1 + ” ” + freq1 );... System.out.println( ” ” + freq100 ); System.out.println( ” ” + freq100 );

CS 100Lecture 115 An Elegant Solution int grade; // the grade being processed. // For i in , freq[i] is # of grades of i. int[] freq = new int[101]; int[] freq = new int[101]; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ /* Initialize 101 counters to 0. */  grade = 0; while ( grade<=100 ) { while ( grade<=100 ) { freq[grade] = 0; grade = grade + 1; } grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) { while (grade != -1 ) { /* Increment counter for grade. */ /* Increment counter for grade. */ freq[grade] = freq[grade] + 1; grade = in.readInt(); grade = in.readInt(); } /* Print histogram. */ /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println(”Grade Frequency”); grade = 0; grade = 0; while ( grade <= 100 ){ while ( grade <= 100 ){ System.out.println( System.out.println( grade + ” ” + freq[grade] grade + ” ” + freq[grade] ); ); grade = grade + 1; }

CS 100Lecture 116 Trace Input Data grade freq...

CS 100Lecture 117 Array Declaration and Construction n An array is an object that consists of a collection of variables (its elements) all with the same type, e.g., array-of-int objects. n The declaration: int[] identifier ; int[] identifier ; declares identifier, a variable that can contain a reference to an array-of-int object. n The constructor invocation: new int[ length ] constructs an array-of-int object that consists of length int variables with subscripts 0 through length -1 n Thus, the initialized declaration int[] identifier = new int[ length ]; creates the following structure: identifier 0 length-1...

CS 100Lecture 118 The Index Operator [] n The phrase identifier [ expression ] identifier [ expression ] provides indexed access to an element of the array referred to by identifier, namely, access to the element with subscript equal to the value of expression. n Examples: freq[1+2+3] = 17; // set freq[6] to 17. grade = 6; freq [grade] = 0; // set freq[6] to 0. // Increment freq[6] by 1. freq [grade] = freq[grade] + 1; freq [grade] = freq[grade] + 1;

CS 100Lecture 119 Subscripts The phrase identifier[expression] is like identifier.field-name except that the “field name” is an integer computed by evaluating the expression. The phrase identifier[expression] is like identifier.field-name except that the “field name” is an integer computed by evaluating the expression. n Subscripts can be specified by arbitrary expressions. The value of the subscript is obtained by evaluating the expression. n The value of the subscript must be in the range 0 through one less than the number of elements in the array. n Do not confuse the subscript of an array element with the value contained in the array element.

CS 100Lecture 1110 Some Shorthand n Increment and Decrement Operations variable ++ variable ++ is equivalent to variable = variable + 1 variable = variable + 1Similarly, variable -- is equivalent to variable = variable - 1 n Example: freq[grade]++ ;

CS 100Lecture 1111 More Shorthand n The statement for (initialization; condition; increment) for (initialization; condition; increment) statement statement is shorthand for initialization; while ( condition ) { statement ; increment ; } Example: for (grade=0; grade<=100; grade++) freq[grade] = 0;

CS 100Lecture 1112 An Elegant Solution, revisited int grade; // the grade being processed. // For i in , freq[i] is # of grades of i. int[] freq = new int[101]; int[] freq = new int[101]; /* “Process” grades until (but not including) a stopping signal of -1. */ /* Initialize 101 counters to 0. */ /* Initialize 101 counters to 0. */ for ( grade = 0; grade<=100; grade++ ) for ( grade = 0; grade<=100; grade++ ) freq[grade] = 0; grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) { while (grade != -1 ) { /* Increment counter for grade. */ /* Increment counter for grade. */freq[grade]++; grade = in.readInt(); grade = in.readInt(); } /* Print histogram. */ /* Print histogram. */ System.out.println(”Grade Frequency”); System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100; grade++ ) for ( grade = 0; grade<=100; grade++ ) System.out.println( System.out.println( grade + ” ” + freq[grade] grade + ” ” + freq[grade] ); );