The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Introduction to arrays Data in economy size packages.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 18, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Chapter 8 Arrays and Strings
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Java Arrays [Lists] in 10 Minutes. Declaring an array (and its types) int[] myIntArray; double[] myDoubleArray; String[] myStrings; //What’s the pattern?
Java Programming: From Problem Analysis to Program Design, 4e
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
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 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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 19, 2005.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
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];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
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.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
ARRAYS.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Programming BCT 1113
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.
EKT150 : Computer Programming
Lecture 9 Objectives Learn about arrays.
CS2011 Introduction to Programming I Arrays (I)
Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Arrays An array is a list of values that can be represented by one variable Members of an array must all have the same data type Each value is stored at a specific, numbered position in the array ♦ the number corresponding to each position is called an index or subscript All arrays have a length ♦ number of elements the array can hold

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Declaring Arrays type[] name; The array (element) data type Empty square brackets The array (variable) name Creates a reference variable called name that can point to an array of type elements.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Declaring Arrays Examples // array of characters char[] characterSet; // array of counters (integers) int[] counter; // array of grades (doubles) double[] grade; counter characterSet grade

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Instantiating Arrays You must instantiate (create) arrays ♦ the size of an array is typically not known before run time name = new type[size]; The array (element) data type The new operator The array (variable) name The assignment operator The number of elements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Instantiating Arrays Examples // instantiate an array of counters counter = new int[5]; // instantiate the array of grades numStudents = 10; grade = new double[numStudents]; counter <= index < size

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Declaration and Instantiation type[] name = new type[size]; DeclarationInstantiation

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Example int[] num = new int[5];

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Array Access Examples averageScore = (score[0]+score[1]+score[2])/3; numStudents = 3; totalScore = 0; for (int i = 0; i < numStudents; i++) { totalScore += score[i]; } averageScore = totalScore/numStudents; double score[] = new score[3]; score[0] = 98.3; score[1] = 57.8; score[2] = 93.4; often use loops for access

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Array Length Arrays have length ♦ an internal variable called length ♦ number of elements in array ♦ access the length variable using the “dot’ notation (arrayname. length ) // loop through the array of test scores sumOfScores = 0; for (int i=0; i<scores.length; i++) { sumOfScores += scores[i]; }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 int counter[] = {0, 0, 0, 0, 0}; char[] characterSet = {‘a’,’b’,’c’}; // etc. Initializing Arrays Array elements are variables too! ♦ if you don’t initialize, the contents are undefined When and how? ♦ if you don’t yet know the size initialize at run time, typically with a loop ♦ if you know how many elements perhaps use an initializer list

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 List the initial value for the elements of an array Items are separated by commas and the list is in braces {} The size of the array is determined by the number of items in the list int[] scores = {87, 98, 45}; Can only be used in the same statement as declaring the array NOT int[] scores; scores = {87, 98, 45}; Initializer Lists

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Array Bounds Arrays have finite size If you access an element outside of the array, you’ll get an ArrayIndexOutOfBounds Exception Example: int grades[] = {99, 98, 95, 96}; System.out.println (grades[4]);

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 int arraySize; System.out.print ("Enter the size of the array:"); arraySize = Integer.parseInt(keyboard.readLine()); int[] list = new int[arraySize]; Specify Array Size During Program Execution Example (Assume that keyboard has already been declared and instantiated.)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 Example for (int ind = 0; ind < sale.length; ind++) { sale[ind] = 10.00; } Initialize Array to Specific Value (10.00) (Assume that sale has already been declared and instantiated.)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Example for (int ind = 0; ind < sale.length; ind++) { sale[ind] = Double.parseDouble(keyboard.readLine()); } (Assume that sale has already been declared and instantiated, and that keyboard has already been declared and instantiated.) Read Data into Array

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Example for (int ind = 0; ind < sale.length; ind++) { System.out.print(sale[ind] + " "); } Print Array (Assume that sale has already been declared and instantiated.)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 Parallel Arrays Arrays are parallel if corresponding components hold related information String[] studentName; double[] studentGPA; For example, studentName and studentGPA are parallel if studentGPA[3] is the GPA of the student with studentName[3].

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 In-Class Exercises 1.Declare an array of integers called numbers 2.Declare and instantiate an array of 26 characters called alphabet

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 In-Class Exercises 3.Declare an array of 5 characters called grades and initialize it with the letters: A, B, C, D, F Hint: type[] name = {initialization list}; 4.Write a loop to print the contents of an array named zipCodes Hint: to access array element name[index]

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 In-Class Exercises 5.Write a loop to change all the values of the integer array numbers to index + 1

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 Exercises 1. Find Sum and Average of Array 2. Determine Largest and Smallest Elements in Array

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 Homework 6 (practice) Read data from file Fill arrays with data Fill objects with data

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 Tomorrow More Arrays ♦ arrays of objects ♦ passing arrays as parameters ♦ searching and sorting Homework 5 due Sunday midnight Bring laptops!!!