School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.

Slides:



Advertisements
Similar presentations
8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
Programming with Collections Collections in Java Using Arrays Week 9.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays Chapter 6.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
©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.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Computer Science I Arrays Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
“Form Ever Follows Function” Louis Henri Sullivan
C Arrays.
7 Arrays.
EKT150 : Computer Programming
Object Oriented Programming in java
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Arrays Week 2.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Presentation transcript:

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 2 Arrays (Tables of data) declaration initialisation manipulation arrays as parameters

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 3 Problem You have collected the ages of each student in CMT1000 (1200 people) and want a program to read them into memory and find the average age.

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 4 Read in numbers... String s; s = JOptionPane.showInputDialog( “enter a no:”); int age0= Integer.parseInt(s); s = JOptionPane.showInputDialog( “enter a no:”); int age1= Integer.parseInt(s);. s = JOptionPane.showInputDialog( “enter a no:”); int age1199= Integer.parseInt(s);

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 5 … and find average double ave = (age0 + age1 + age age1199)/ ; System.out.println(“Average is ” + ave);

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 6 Problems? Having lots of separate variables is very tedious It would be better if we could make one declaration –one name –one type –give number of cells

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 7 A new plan: use an array 1. Read each value into a separate entry in an array 2.Initialise a total to 0 3. for i = 0 to 1199 add the ith age to total 4.Output total/1200

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 8 Reading in values... int ages[] = new int[1200]; String s; for (int i = 0; i <= 1199; i++ ) { s = JOptionPane.showInputDialog( “enter a no:”); ages[i]= Integer.parseInt(s); }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 9 …and computing average int total = 0; for (int i = 0; i <= 1199; i++ ) { total += ages[i]; } int ave = total / ; System.out.println(“Average is ” + ave);

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 10 Notes We declare an array reference variable like this int numarr[]; // array of ints char charray[]; // array of chars double decarr[]; //array of doubles To create an array object and store a reference to it in a reference variable: numarr = new int[100];

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 11 Notes When an array is allocated, if it contains numbers, they are initialised to 0. The array subscript type must be integer Array subscripts start at 0 int heights[] = new int[20]; How many elements? Subscript range?

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 12 Length The length of an array A is the value of the expression A.length What is the value of heights.length

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 13 Array reference variables Once an array is allocated, it is fixed in size. However, a reference variable may be set to point to a different array What happens? int A[] = new int[5]; int B[] = new int [10]; A = new int[3]; B = A;

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 14 Initialiser lists What does this do? int ages [] = {19, 27, 18, 18, 95, 14, 20, 19, 22, 37}; Note we don’t need new here - it’s called automatically

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 15 Initialise and print From Fig. 7.4: Deitel and Deitel String output = ""; // Initializer list specifies number of elements // and value for each element. int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; output += "Subscript\tValue\n"; for ( int i = 0; i < n.length; i++ ) output += i + "\t" + n[ i ] + "\n";

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 16 Initialise and print (cont) A neat output technique: JTextArea JTextArea outputArea = new JTextArea( 11, 10 ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array of int Values", JOptionPane.INFORMATION_MESSAGE);

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 17 JTextArea JTextArea outputArea = new JTextArea( 11, 10 ); declares reference and creates JTextArea object with 11 rows and 10 columns of text JTextArea is a GUI component for displaying large amounts of text - if too much for the screen, allows scrolling. Initially contains an empty string; can add (concat) to this string using method append outputArea.append(“blah blah”); But we used the setText method outputArea.setText( output );

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 18 Output We output the text using JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array of int Values", JOptionPane.INFORMATION_MESSAGE); Note that showMessageDialog can take a string or a GUI component as an argument

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 19 Another example From Fig. 7.5 D and D Initialize array to the even integers from 2 to 20 final int ARRAY_SIZE = 10; int n[]; // reference to int array // allocate array n = new int[ ARRAY_SIZE ]; // Set the values in the array for ( int i = 0; i < n.length; i++ ) n[ i ] = * i;

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 20 Student poll Fig 7.9 D and D Survey rates café food quality on scale 1 to 10 Prog must compute the frequency (quantity) of each response

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 21 Initialise int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; int frequency[] = new int[ 11 ]; String output = "";

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 22 Compute frequencies for ( int answer = 0; // initialize answer < responses.length; // condition answer++ ) // increment ++frequency[ responses[ answer ] ];

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 23 Build output string output += "Rating\tFrequency\n"; for ( int rating = 1; rating < frequency.length; rating++ ) output += rating + "\t" + frequency[ rating ] + "\n";

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 24 Do output JTextArea outputArea = new JTextArea( 11,10 ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, "Student Poll Program", JOptionPane.INFORMATION_MESSAGE );

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 25 Reference parameters Primitive type arguments are passed to methods using call by value semantics(see last week’s lecture) Objects cannot be passed as arguments to methods; instead we pass object references, using call by value However, this allows the method to manipulate the object directly; we are effectively ‘passing’ the object using ‘call by reference’ semantics Arrays are treated as objects –efficiency versus side effect elimination

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 26 What happens? public static void modifyArray( int b[] ) { for ( int j = 0; j < b.length; j++ ) b[ j ] *= 2; } public void modifyElement( int e ) { e *= 2; } int a[] = { 1, 2, 3, 4, 5 }; modifyArray( a ); modifyElement( a[ 3 ] );

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 27 Reference vs value The array is passed by reference The primitive-type element is passed by value