1 Lecture 14. 2 Today’s topic Arrays Reading for this Lecture: –Chaper 11.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to Programming
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
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.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Loops and Iteration for Statements, while Statements and do-while Statements.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
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.
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.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester,
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
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 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
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.
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];
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Day 26 Arrays Episode 2. Array Lengths To determine the length of an array, use the command: array_Name.length.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Lecture Notes – Week 3 Lecture-2
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.
CS2011 Introduction to Programming I Arrays (I)
Arrays and Array Lists CS 21a.
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

1 Lecture 14

2 Today’s topic Arrays Reading for this Lecture: –Chaper 11

3 Introduction to Arrays What is Arrays –Arrays will contain a list of values of same type –It is very useful to have an array when a program manages a large number of information when a set of values will be processed in a loop

4 Introduction to Arrays For example, when a program manages information of 100 students (e.g. student ID) in a loop For each loop, we want to access the data for each student But we don’t want to declare them as individual variables, e.g. 100 integer variables like: int stdID1,stdID2,stdID3,stdID4,stdID5,…; inefficient

5 Introduction to Arrays Without arrays we would need to do something like this (NOTE: Don’t do it this way!): int stdID0, stdID1, stdID2, stdID3, stdID4, … ; for (int i = 0; i < 100; i++) { if(i == 0) { System.out.println( stdId0 ); } else if(i == 1) { System.out.println( stdId1 ); } else if(i == 2) { System.out.println( stdId2 ); } } We have to repeatedly write same statements for all variables (i.e. all student IDs) …

6 Introduction to Arrays In Java, an array is an object We can declare an array as follows: e.g. int [] nums = new int [5]; –Specify data type for an variable values –[ ] indicates that following variable is an array –Name of array variable –Use new operator to create an object (instantiation) –Specify how many values will be contained in this array

7 Introduction to Arrays int [] nums = new int [5]; This statement declares an array named nums, which contains 5 values of type int A single integer value can be selected using an integer (usually called index) inside the [ ] In this example, the valid array index values are 0 ~ 4 (not 1 ~ 5) nums e.g. nums[0]nums[3]

8 Initialization of Arrays (1) An array should be initialized so that each element contains a specific value: // array declaration with initialization int [] nums = {2, 4, 6, 8, 10}; nums For example, nums[0] is 2 nums[1] is 4 index

9 Initialization of Arrays (2) An array should be initialized so that each element contains a specific value: // array declaration without initialization int [] nums = new int [5]; nums[0] = 1; nums[1] = 3; nums[2] = 5; nums[3] = 7; nums[4] = 9; nums index

10 Arrays and Loops Array will be efficiently used in loops // Example in the previous slide int stdID0, stdID1, stdID2, stdID3, stdID4; for (int i = 0; i < 5; i++) { if( i == 0 ) { System.out.println(“Student0 ID is ” + stdID0); } else if( i == 1 ) { System.out.println(“Student1 ID is ” + stdID1); } else if( i == 2 ) { System.out.println(“Student2 ID is ” + stdID2); } // two more students here ….. }

11 Arrays and Loops Now, we can coordinate the processing of one value with the execution of one pass through a loop using an index variable int MAX = 5; int [ ] stdID = {32, 42, 90, 25, 10}; for (int i = 0; i < MAX; i++) { // use i as array index variable System.out.println(“Student” + i + “ ID is ” + stdID[i]); } int [ ] stdID = new int [5]; stdID[0] = 32; stdID[1] = 42; stdID[2] = 90; stdID[3] = 25; stdID[4] = 10;

12 Alternative Loop Control Condition Arrays are objects Each array has an attribute “length” –we can get a value equal to the length of that array e.g. nums.length is equal to MAX: int MAX = 5; // symbolic constant int [ ] nums = new int [MAX]; for (i = 0; i < nums.length; i++) { // use i as array index variable in Java statements using nums[i]; }

13 Exercise!!!

14 (1)Declare an array of integer (2)Store (assign) seven multiply of 3 in the array (3)Print out them 3, 6, 9, 12, 15, 18, 21  array (4)Print out the array elements in reverse order