int [] scores = new int [10];

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Chapter 7 – Arrays.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 Modelling 1-D Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing Array.
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.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
A First Book of ANSI C Fourth Edition
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
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.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Mathematical Calculations in Java Mrs. G. Chapman.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Week 6 - Friday.  What did we talk about last time?  Loop examples.
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.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to programming in java Lecture 21 Arrays – Part 1.
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.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Arrays.
Multiple variables can be created in one declaration
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.
int [] scores = new int [10];
Building Java Programs
python.reset() Also moving to a more reasonable room (CSE 403)
int [] scores = new int [10];
Dr. Sampath Jayarathna Cal Poly Pomona
Suggested self-checks: Section 7.11 #1-11
ArrayList Day 3 Be able to read AP Mult Choice questions tied to ArrayLists Be able to write a program that uses ArrayLists that analyzes numbers.
Unit 3: Variables in Java
Dry Run Fix it Write a program
Random Numbers while loop
int [] scores = new int [10];
How do you do the following?
Week 7 - Monday CS 121.
Presentation transcript:

int [] scores = new int [10]; for (int i=0; i<scores.length; i++) { System.out.println(scores[i]); } scores.length AP Java for (int s : scores) { System.out.println(s); } int [ ] scores = { 10, 50, 20 }; 10-22-2015 More on Arrays int [] scores = new int [10];

Today Review Arrays Declaration Inputting Values Processing Outputting For..each loop Programming

Program Input: 10 scores (integers) Output: The high score The low score The average The number of scores within 3 of the average Push: Show the numbers in order low to high Input: 10 scores and calculate the standard deviation

Standard Deviation Example. Find the standard deviation of 4, 9, 11, 12, 17, 5, 8, 12, 14 First work out the mean (Average, xbar,…): (4 + 9 + 11 + 12 + 17 + 5 + 8 + 12 + 14)/9 = 10.222 Now, subtract the mean individually from each of the numbers in the question and square the result. This is equivalent to the (x - xbar)² step. x refers to the values in the question. x               4        9        11       12       17       5       8       12      14 (x - xbar)²     38.7   1.49    0.60    3.16    45.9   27.3   4.94   3.16   14.3 Now add up these results (this is the 'sigma' in the formula): 139.55 Divide by n-1. n is the number of values, so in this case n-1 is 8: 139.55 / 8 = 17.44 And finally, square root this: 4.18

Some Vocabulary Array: A numbered sequence of items, which are all of the same type. Index: The position of the item. In Java they are numbered 0, 1, 2, … (n-1) where n is the number of elements in the array. Base type: the type of elements in the array. Can have arrays of primitives, classes, or interfaces

Java arrays are Objects Created using new The array variable does not hold the array, it holds the reference to the array. (It is a pointer) It is a pointer that is defined in the array declaration. As a reference, it can have a value of null. (Like nil in Pascal.)

Declaration: Two steps. Or one step int [] list = new list [5]; int [] list; Creates a variable of type int[] (something that can point to an array of ints.) The variable now has a value of null. list = new int [5]; Points list to an array with 5 spaces for ints. The length of the array is an instance variable. You can get the length by using the method. list.length. (Generically it is arrayname.length)

int [] list = new int [5]; (5) list.length list[0] list[1] list[2] list.length list[0] list[1] list[2] list[3] list[4] Note: In Java, a newly created array is automatically filled with: 0 for numbers false for boolean Unicode number zero for char null for objects.

Two ways to declare arrays int [] list; list = new int [5]; OR int [] list = new int[5]; //Same as the two lines above

Declaring and initializing int [] list = { 1, 4, 9, 16 }; The elements using to initialize the array can be constants, variables, or expressions as long as the type matches. This can only be done in the declaration. However you can do the following later in the program list = new int[ ] { 4, 3, 2 , 12};

Some code examples // do any necessary initialization for (int i = 0; i < A.length; i++) { . . . // process A[i] } double sum; // The sum of the numbers in A. sum = 0; // Start with 0. sum += A[i]; // add A[i] to the sum, for // i = 0, 1, ..., A.length - 1

“For Each” Loop Introduced in Java 5 Works both with standard arrays and ArrayLists Convenient for traversing for (int s : scores) { System.out.println(s); } An iterator is an object that helps to traverse a collection. It has a method next that supplies the next element of the collection. A “For each” loop is a shortcut for an iterator.

“For Each” Loop: Example 1 So s will store the value of the current address of the array. The type of the elements int [ ] scores = { ... }; ... int sum = 0; for (int s : scores) { sum += s; } The name of the array. Basically the same as: for (int i = 0; i < scores.length; i++) { int s = scores[i]; sum += s; } Thus the “for each” loop has been added to Java 5 simply for convenience. Read “for each integer s in scores…”

“For Each” Loop (cont’d) You cannot add or remove elements within a “for each” loop. You cannot change elements of primitive data types or references to objects within a “for each” loop. For an array or ArrayList of objects, a “for each” loop supplies a reference to an element. You can change the object it refers to (unless it is immutable), but you cannot change the reference itself.

Copying arrays list.length (5) list[0] 1 list[1] 4 list[2] 9 list[3] 16 25 What would the following do? list = new int[] { 1, 4, 9, 16, 25 }; int [] b; b = list; How can you copy all of the elements? b = new int[5]; for (int i = 0; i < list.length; i++) b[i] = list[i]; // Copy each item from list to B.

Array Program 2 (10/22/2015) Complete the following Array Program 2 (10/22/2015) Complete the following. There are three Array Programs. Write a program that will roll a pair of six-sided dice 1000 times and count and display how often each roll occurs. Also show which roll occurs the most often and which occurs the least often. Push: Compare the results to what should happen statistically. Push: Display the results in a graph. Create a random compliment generator using an array to store the compliments. Use a loop (so the user can be complimented often) have the computer display one of at least 5 random compliments. Push: Look up a Chatbot to include interaction with the user. Ask questions about them so you can give better compliments.

3) Smooth Operator An audio signal is sometimes stored as a list of int values. The values represent the intensity of the signal at successive time intervals. Of course, in a program the signal is represented with an array. Often a small amount of noise is included in the signal. Noise is usually small, momentary changes in the signal level. An example is the "static" that is heard in addition to the signal in AM radio. Smoothing a signal removes some of the noise and improves the perceptual quality of the signal. This exercise is to smooth the values in an integer array. Say that the original values are in the array "signal". Compute the smoothed array by doing this: Each value smooth[N] is the average of three values: signal[N-1], signal[N], and signal[N+1]. For the first element of smooth, average the first two elements of signal. For the last element of smooth, average the last two elements of signal. Use integer arithmetic for this so that the values in smooth are integers. Example In interpreting the results, remember that integer division discards the remainder. It does not compute a rounded value. Here is a sample run of the program: signal: 1 5 4 5 7 6 8 6 5 4 5 4 smooth: 3 3 4 5 6 7 6 6 5 4 4 4

Push: Modify it so it will smooth 5 values. public class Smooth { public static void main ( String[] args ) int[] signal = {5, 5, 4, 5, 6, 6, 7, 6, 5, 4, 1, 4}; int[] smooth // compute the smoothed value for each cell of the array smooth smooth[0] = smooth[ signal.length-1 ] = for ( ) { } // write out the input for ( int j=0; j < smooth.length; j++) { } // write out the result } Partial Code Push: Modify it so it will smooth 5 values. Push: Let the user select the number of values it will smooth. Push: Modify it so that it will ignore ‘spikes’ by throwing out the highest and lowest readings.