Heads-Up – Programming Quiz This Week Announcement will be made via E-mail You will be provided with a login to the password-protected website You will.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

One Dimensional Arrays
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Java Unit 9: Arrays Declaring and Processing Arrays.
Introducing Java.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
DAT602 Database Application Development Lecture 5 JAVA Review.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
A Semantic Error in Google last weekend! Someone in Google typed an extra ‘/’ character into their URL List Link to CNN video report posted on Collab.
1 Chapter 4: Arrays and strings Taufik Djatna
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Introduction to Computer Systems and the Java Programming Language.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008.
1.4 Arrays "... By the way, we rank 10 th among the industrialized world in broadband technology and its availability. That’s not good enough for America.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
What does a computer program look like: a general overview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Working with arrays (we will use an array of double as example)
Execution of an instruction
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.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Simple algorithms on an array - compute sum and min.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
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];
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];
Week 6 - Friday.  What did we talk about last time?  Loop examples.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 6 Arrays Solution Opening Problem
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Lecture 18 Arrays and Pointer Arithmetic
3 Control Statements:.
Arrays .
int [] scores = new int [10];
Chapter 6 Arrays.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Chapter 6 Arrays.
1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/6/11 12:33.
Introduction to java Part I By Shenglan Zhang.
Looping and Repetition
Presentation transcript:

Heads-Up – Programming Quiz This Week Announcement will be made via You will be provided with a login to the password-protected website You will have 75 minutes to complete the quiz. will state the deadline Work on the quiz by yourself individually and upload it via Collab. You are NOT allowed to post questions about the quiz on forums or approach the instructor(s) or TAs for help. (Quizzes are like “take- home” exams)

2 Control Flow Summary Control flow. n Sequence of statements that are actually executed in a program. n Conditionals and loops: enables us to choreograph the control flow. n Manipulate small amounts of data. Straight-line programs All statements are executed in the order given. Conditionals Certain statements are executed depending on the values of certain variables. if if-else Loops Certain statements are executed repeatedly until certain conditions are met. while for do-while Control FlowDescriptionExamples

3 Manipulating More Data Goal. 10 variables of the same type. double a0, a1, a2, a3, a4, a5, a6, a7, a8, a9; a0 = 0.0; a1 = 0.0; a2 = 0.0; a3 = 0.0; a4 = 0.0; a5 = 0.0; a6 = 0.0; a7 = 0.0; a9 = 0.0; double x = a4 + a8;

1.4 Arrays

5 Arrays Arrays allow you to store and manipulate huge quantities of data. Program code remains concise. Array. Indexed sequence of values of the same type. Examples. n 52 playing cards in a deck. n 14 thousand undergrads at UVa. n 1 million characters in a book. n 10 million audio samples in an MP3 file. n 4 billion nucleotides in a DNA strand. n 1 trillion webpages crawled by Google! horton0 gurumurthi1 humphrey2 knuth3 billg4 rms5 mst3k6 alvin7 indexvalue Elements

6 Arrays in Java Java has special language support for arrays. n To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. int N = 10; double[] a; // declare the array a = new double[N]; // create the array for (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } HelloWorld.java

7 Arrays in Java Java has special language support for arrays. n To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0. Compact alternative. n Declare, create, and initialize in one statement. n Default initialization: all numbers automatically set to zero. int N = 10; double[] a; // declare the array a = new double[N]; // create the array for (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0 int N = 10; double[] a = new double[N]; // declare, create, init

Initializing and Printing Arrays Alternative way of initializing an array: double[] a = {0.1, 0.2, 0.3}; Printing an array: INCORRECT System.out.print(a); //random looking junk CORRECT System.out.print(a[3]); //print element a[3] CORRECT for(int i=0;i < N;i++) System.out.println(a[i]); //print the entire array WHY DOES ARRAY MANIPULATION INVOLVE MORE WORK? 8

Basic Computer Hardware Central Processing Unit (CPU) Executes the machine- language instructions of the compiled program Memory Provides storage for program data Stores the program and other large data as files Disk 9

Role of Memory Provides storage for the data used by a program n Variables – One storage location per variable n Size of the location depends on the datatype of the variable Arrays n A sequence of variables of the same type n Need to store all the “variables” of the array in memory as a single logical unit 10

Memory Memory is split into equal sized units known as “addresses”. (Address Space) Arrays occupy consecutive addresses in this linear space: int[] a = {5, 6, 10}; Memory Allocation: Java allocates addresses for the array when you call new Key Point: Name of the array indirectly references its starting address. int[] b = new int[3]; b=a; //b references the SAME data in memory. BEWARE! N-2 N N-2 N-1 a[0] a[1] a[2] Addresses

12 Vector Dot Product Dot product. Given two vectors x and y of length n, their dot product is the sum of the products of their corresponding components.

13 Vector Dot Product double[] x = { 0.3, 0.6, 0.1 }; double[] y = { 0.5, 0.1, 0.4 }; double sum = 0.0; for (int i = 0; i < 3; i++) { sum += x[i]*y[i]; // same as sum = sum + x[i]*y[i]; } += -= /= *=

14 Array Processing Code NOT i <= N What Does This Code Do? Dr. Java Demo of Array Reversal

15 An array’s length is fixed Each array keeps track of how many items it contains n In length field. Example: int[] list = { 3, -1, 0, 7, 5 } list.length == 5 // is true n Note use of “.” notation. More on this later, but for now think of it as “something inside of” the thing on the left n Important: an array’s length cannot change (grow, shrink) – Must know how many items will be stored when it’s created! int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; // Find index of first occurrence of target in array int i = 0; while ( i < list.length && list[i] != target ) i = i + 1; if ( i != list.length ) System.out.println("Found at index " + i);

Auto-increment and arrays Auto-increment and auto-decrement operators used with arrays. n ++i means “add one to i’s value and use the updated value” n i++ means “use i’s value now and then add one to the old value” n Can use on line by itself as a single statement – In which case they do exactly the same thing! n Differences between the auto-increment operators are important when used in expressions: y = 0; x = y++; y = 0; z = ++y; 16 int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; int i = 0; while ( i < list.length && list[i++] != target ) ; // empty loop body! if ( i != list.length ) System.out.println("Found at index " + (i-1) ); Y=0; x = y; y = y+1; Y=0; y = y+1; z = y;

Which Version of the Code is Better? 17 int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; // Find index of first occurrence of target in array int i = 0; while ( i < list.length && list[i] != target ) i = i + 1; if ( i != list.length ) System.out.println("Found at index " + i); int target = Integer.parseInt(args[0]); int[] list = { -3, -1, 0, 3, 5, 9 }; int i = 0; while ( i < list.length && list[i++] != target ) ; // empty loop body! if ( i != list.length ) System.out.println("Found at index " + (i-1) );