CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
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.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
Introduction to arrays Data in economy size packages.
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.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
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.
Introduction to arrays. Array Homogeneous collection of components stored in adjacent memory locations –All elements share same data type –Entire collection.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Chapter 9 Introduction to Arrays
Arrays.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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 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 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
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,
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
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.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
CSE 1201 Object Oriented Programming ArrayList 1.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
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.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
BIT115: Introduction to Programming
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
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];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Introduction to programming in java Lecture 05 Review of 1 st four lectures and Practice Questions.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Arrays
Introduction to programming in java
SELECTION STATEMENTS (1)
OPERATORS (1) CSC 111.
CS100J Lecture 11 Previous Lecture This Lecture
Arrays in Java.
Introduction to Arrays
CS100J Lecture 13 Previous Lecture Java constructs
Arrays.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS100A Lect. 12, 8 Oct More About Arrays
loops revisited I/O arrays
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting Use of arrays to store collections of data Reading in Lewis/Loftus 6.1 ( )

CS100A, Fall 1997, Lecture 112 New Problem Input data: Zero or more exam grades in the range 0 to 100, followed by a –1. Requested output: A histogram showing, for each possible score, the number of students receiving that score. What is the general form of the solution? What must be done: Before reading any grades? Each time a grade is read? After all grades have been read? How many variables are needed to keep track of the histogram data? grade frequency …

CS100A, Fall 1997, Lecture 113 A Tedious Solution public static void main(String args[]) { int grade; // current grade int freq0; // # students with grade = 0 int freq1; // # students with grade = 1 … int freq98; // # students with grade = 98 int freq99; // # students with grade = 99 int freq100; // # students with grade = 100 // set histogram variables to 0 freq0 = 0; freq1 = 0; freq2 = 0; … freq98 = 0; freq99 = 0; freq100 = 0;

CS100A, Fall 1997, Lecture 114 A Tedious Solution (cont.) // read grades and record them in histogram grade = in.readInt( ); // inv: frequency of grades processed so far // recorded in freq0, …, freq100. while (grade != -1) { // record current grade in histogram if (grade == 0) freq0 = freq0 + 1; else if (grade == 1) freq1 = freq1 + 1; … else if (grade == 98) freq98 = freq98 + 1; else if (grade == 99) freq99 = freq99 + 1; else if (grade==100) freq100=freq100+1; // read next grade grade = in.readInt( ); }

CS100A, Fall 1997, Lecture 115 A Tedious Solution (cont.) // print histogram System.out.println(“grade frequency”); System.out.println( 0 + “ ” + freq0); System.out.println( 1 + “ ” + freq1); … System.out.println( 98 + “ ” + freq98); System.out.println( 99 + “ ” + freq99); System.out.println(100 + “ ”+freq100); }

CS100A, Fall 1997, Lecture 116 Arrays of Variables A much better solution is to use an array of integer variables to store the frequencies. The declaration int[ ] freq; declares a variable of type int[ ] (int array). It does not actually allocate the array. The assignment freq = new int[101]; allocates an array containing 101 int variables. Graphically … freq freq is called an array. The variable freq[expression] refers to one of the variables freq[0], freq[1], …, freq[100], depending on the value of expression.

CS100A, Fall 1997, Lecture 117 A Better Solution public static void main(String args[]) { int grade; // current grade int[ ] freq; // freq[i] is the number of // students receiving grade i int k; // allocate histogram array and initialize // entries to 0 freq = new int[101]; k = 0; // inv: freq[0..k-1] = 0 while (k <= 100) { freq[k] = 0; k = k + 1; }

CS100A, Fall 1997, Lecture 118 A Better Solution (cont) // read grades and record them in histogram grade = in.readInt( ); // inv: freq contains frequencies of grades // previously processed while (grade != -1) { // record current grade in histogram freq[grade] = freq[grade] + 1; // input next grade grade = in.readInt( ); } // print histogram System.out.println(“grade frequency”); k = 0; // inv: freq[0..k-1] has been printed while (k <= 100) { System.out.println( k + “ ” + freq[k]); k = k + 1; }

CS100A, Fall 1997, Lecture 119 Trace Test input data: … –1 Variables: grade … freq k

CS100A, Fall 1997, Lecture 1110 Notation for Array Sections When talking about algorithms that use arrays, it is often convenient to be able to talk about a section of an array. We use the notation A[i..j] to refer to the section of the array containing the variables A[i], A[i+1], …, A[j-1], A[j] This is not Java notation — in a Java program, array elements may only be accessed one at a time with a single subscript. We use the convention that, if i > j, the array section A[i..j] is empty, i.e., it contains no elements and has length 0.

CS100A, Fall 1997, Lecture 1111 New Problem Input data (as before): Zero or more grades in the range 0 to 100, followed by a –1. Example: –1 Requested output: The input grades printed in the reverse of the order they were read. Example: The usual questions apply: What is the general form of the solution? What must be done: Before reading any grades? Each time a grade is read? After all grades have been read? How large an array is needed to hold the data?

CS100A, Fall 1997, Lecture 1112 Solution: Store Input in an Array We store the input in an integer array in the order it was read. Logically associated with the array is an integer variable that contains the number of grades stored so far. We also pick an arbitrary number, MaxGrades, as an upper limit on the number of grades that can be stored … MaxGrades grades nGrades Invariant (general rule): Grades are stored in grades[0..nGrades-1] with grades[0] being the first grade, grades[1] the second, etc. nGrades <= MaxGrades. If nGrades = MaxGrades, the array is “full” and no more data can be stored.

CS100A, Fall 1997, Lecture 1113 Solution // read a list of grades and print them in // reverse order public static void main(String args[]) { int MaxGrades = 10; // Maximum # grades // that can be stored // grades are stored in grades[0..nGrades-1] // with grades[0] being the first grade read, // grades[1] being the second, etc. int [ ] grades = new int[MaxGrades]; int nGrades; // # grades stored int grade; // current grade from input int k; // initialize list of grades to empty nGrades = 0;

CS100A, Fall 1997, Lecture 1114 Solution (cont.) // Read grades from input and store them. // inv: grades[0..nGrades-1] are the grades // read so far. grade = in.readInt(); while (grade != -1) { grades[nGrades] = grade; nGrades = nGrades + 1; grade = in.readInt(); } // print the grades in reverse order k = nGrades -1; // inv: grades[k+1..nGrades-1] has been // printed in reverse order while (k >= 0) { System.out.print(grades[k] + " "); k = k - 1; } System.out.println(); }

CS100A, Fall 1997, Lecture 1115 Trace Test input data: Variables … MaxGrades grades nGrades grade k

CS100A, Fall 1997, Lecture 1116 Rules When an array is allocated int[ ] A = new int[size]; size may be any integer expression. When using A[exp] to refer to an element, the subscript exp may be any integer expression. The elements in an array are numbered A[0], A[1], …, A[size-1], where size is the number of elements in the array. In a reference to an array element A[exp], it is an error if exp<0 or expsize. Be sure to be careful to distinguish the subscript (or location) of an array element from the data that is actually stored in that location.

CS100A, Fall 1997, Lecture 1117 A Note for C/C++ Programmers In C or C++, the brackets in an array declaration are written after the variable name (which is most confusing), not as part of the type. Java also allows this, so the declaration int[ ] A = new int[size]; could have been written like this: int A[ ] = new int[size]; We will always use the first form to emphasize that the brackets, which indicate an array, are part of the type of the variable.