Week 7 - Monday CS 121.

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Advertisements

Week 6: Arrays 1.  Loops are great  But, without a way to talk about a group of values, we can’t get the full potential out of a loop  Enter: the array.
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.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
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.
CS107 References and Arrays By Chris Pable Spring 2009.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
int [] scores = new int [10];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Processing Sequences of Elements
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.
Array in C# Array in C# RIHS Arshad Khan
Week 2 - Wednesday CS 121.
Arrays in 60 seconds err.. minutes
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Introduction to Computer Science / Procedural – 67130
Foundations of Programming: Arrays
Week 7 - Wednesday CS 121.
[Array, Array, Array, Array]
Week 15 – Wednesday CS 121.
Week 9 - Monday CS 121.
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs Chapter 7
One-Dimensional Array Introduction Lesson xx
Building Java Programs
Building Java Programs
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.
CS-161 Computer Programming Lecture 14: Arrays I
int [] scores = new int [10];
INC 161 , CPE 100 Computer Programming
Building Java Programs
Defining methods and more arrays
Arrays .
Arrays, Part 1 of 2 Topics Definition of a Data Structure
python.reset() Also moving to a more reasonable room (CSE 403)
Starting Out with Programming Logic & Design
[Array, Array, Array, Array]
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
int [] scores = new int [10];
CS 200 Arrays Jim Williams, PhD.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
Programming Control Structures with JavaScript Part 2
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Loops and Arrays in JavaScript
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
How do you do the following?
Presentation transcript:

Week 7 - Monday CS 121

Last time What did we talk about last time? Loop examples

Questions?

Project 2

Arrays

What good is a loop without something to loop over? Loops are great But, without a way to talk about a list of variables, we can’t get the full potential out of a loop Enter: the array

Definition of an array An array is a homogeneous, static data structure Homogeneous means that everything in the array is the same type: int, double, String, etc. Static (in this case) means that the size of the array is fixed when you create it

Where have you seen arrays before? The args variable passed into the main() method is an array of type String This array has a set number of Strings (maybe zero) that you can use as input to your program Now, we are giving you the ability to create and manipulate your own arrays

Array Syntax

Declaration of an array To declare an array of a specified type with a given name: Example with a list of type int: Just like any variable declaration, but with [] type[] name; int[] list;

Instantiation of an array When you declare an array, you are only creating a variable that can hold an array At first, it holds nothing, also know as null To use it, you have to create an array, supplying a specific size: This code creates an array of 100 ints int[] list; list = new int[100];

Accessing elements of an array You can access an element of an array by indexing into it, using square brackets and a number Once you have indexed into an array, that variable behaves exactly like any other variable of that type You can read values from it and store values into it Indexing starts at 0 and stops at 1 less than the length list[9] = 142; System.out.println(list[9]);

Length of an array When you instantiate an array, you specify the length Sometimes (like in the case of args) you are given an array of unknown length You can use its length member to find out int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42

Automatic initialization When you create an int, double, char, or boolean array, the array is automatically filled with certain values For other types, including Strings, each index in the array must be filled explicitly Type Value int double 0.0 char '\0' boolean false

Explicit initialization Explicit initialization can be done with a list: Or, a loop could be used to set all the values: String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; String[] numbers = new String[100]; for(int i = 0; i < numbers.length; i++) numbers[i] = "" + (i + 1);

Array Memory

Memory An array takes up the size of each element times the length of the array Each array starts at some point in computer memory The index used for the array is actually an offset from that starting point That’s why the first element is at index 0

A look at memory We can imagine that we have an array of type int of length 10 Java decides what address in memory is going to be used, but let’s say it starts at 524 Addresses 12 43 -9 6 789 -23 23 10 0 1 2 3 4 5 6 7 8 9 524 528 532 536 540 544 548 552 556 560 Indexes

Connection to for-loops

for loops + arrays = power Arrays are a fixed size list of a single kind of data A for loop is ideal for iterating over every item and performing some operation for loops and arrays will crop up again and again Of course, a while loop can be used with an array, but it rarely is

for loop for summing an array Imagine that we have an array of ints called list Let’s use a for loop to sum up those ints Super easy, just like before We don’t even need to know how big the array is ahead of time int sum = 0; for( int i = 0; i < list.length; i++ ) sum += list[i];

Array example Ask the user how many words he or she wants to enter Make an array of String values of that length Read in all of the words and store them in the array Print all the words back out

Statistics Recently, we showed you how to add a set of numbers together as they were input by a user Although this is a useful technique, not every operation is possible We can find the sum or the average of the numbers because we only need to see the numbers once What operation needs to see the numbers more than once?

Variance Variance is a measurement of how spread out numbers are from their mean To calculate it, you have to calculate the mean first The formula is: Where N is the number of elements, xi is the ith element, and is the mean

Code for variance Given an array of doubles called numbers, here’s the code for finding their variance double average = 0; double variance = 0; double temp; for( int i = 0; i < numbers.length; i++ ) average += numbers[i]; average /= numbers.length; for( int i = 0; i < numbers.length; i++ ) { temp = numbers[i] – average; variance += temp*temp; } variance /= numbers.length;

Mid-Semester Evaluations

Upcoming

Next time… More on arrays Representing sound inside of a computer StdAudio

Reminders Keep reading Chapter 6 of the textbook Finish Project 2 Due tonight before midnight!