CP1020 - Principles of Programming Steve Garner and Ian Coulson CP1020 - Week 8 Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Lecture Roger Sutton CO331 Visual Programming 13: Multi-dimensional Arrays 1.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
CP Week 10 Modularising programs using Procedures.
CP Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice.
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
CP Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand.
CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CP Week 9 Working with arrays. What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA.
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
CP Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Games and Simulations O-O Programming in Java The Walker School
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Java Unit 9: Arrays Declaring and Processing Arrays.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
IE 212: Computational Methods for Industrial Engineering
Multi-Dimensional Arrays
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.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Chapter 8: Arrays.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
Foundation Studies Course M.Montebello Arrays Foundation Studies Course.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Arrays: Part Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
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;
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Collections
Computer Programming BCT 1113
VBScript Session 2 Dani Vainstein.
Introduction To Programming Information Technology , 1’st Semester
Arrays.
VBScript Session 2 Dani Vainstein.
EXCEL Creating An Array In VBA
Presentation transcript:

CP Principles of Programming Steve Garner and Ian Coulson CP Week 8 Arrays

CP Principles of Programming Steve Garner and Ian Coulson Aims and Objectives zUnderstand what an array is zBe able to decide when to use an array zBe able declare an array zBe able to write data into and retrieve data from an array

CP Principles of Programming Steve Garner and Ian Coulson The Problem with Variables zA variable can contain data relating to a single characteristic of an object sSurname = "John Smith" iEmployeeNumber = z...but in most cases we want to refer to collections of data, e.g. all employees in the company

CP Principles of Programming Steve Garner and Ian Coulson The problem For instance we want to store in variables a collection of names: Fred;Julie; Kim; John; Chris. This would require 5 dim statements with 5 variable names! The problem increases with increasing data to store.

CP Principles of Programming Steve Garner and Ian Coulson The solution! zAn array is like having a variable with more than one compartment to store one piece of data in each. asName(1) = “Fred” asName(2) = “Julie” asName(3) = “Kim” asName(4) = “John” asName(5) = “Chris” (1) Fred (3) Kim (2) Julie (4) John (5) Chris asName Array name element number data stored in that element

CP Principles of Programming Steve Garner and Ian Coulson Arrays Hold Collections of Variables zThe elements in an array variable can only be of the same data type. The size of the array has to be set when the array variable is declared yactually you can re-dimension arrays, zEach element in the array has a unique address - it’s name plus it’s element number

CP Principles of Programming Steve Garner and Ian Coulson Defining an Array We use the standard keywords used to declare variables: Dim We need to say what the size of the array is when we declare it Dim aiCounters(14) As Integer Dim asNames(5) As String

CP Principles of Programming Steve Garner and Ian Coulson Accessing an Array zWe need to be able to address the individual elements in an array zWe use the array name and the element number to access it Dim asName(5) As String asName(2) = “Pete” asName(3) = “Lucy” INPUT “enter a name “; asName(4) (1) “Jane” (3) “Lucy” (2) “Pete” (4) “Dave” (5) “Ian” asName

CP Principles of Programming Steve Garner and Ian Coulson How big is an array? Dim sNames(2) As String will create an array that has 3 elements sName(0) sName(1) sName(2) Be careful! By default QBasic sets the lowest array element as 0, so in our case this means we have three elements Lowest element

CP Principles of Programming Steve Garner and Ian Coulson Array Bounds & Option Base zThe bounds are the ‘size’ of an array zArrays have a lower address or lower bound, and an upper address or upper bound zWe can alter the default lower bound by using Option Base

CP Principles of Programming Steve Garner and Ian Coulson Option base zOption Base can be set to either 0 or 1 yOption Base 0 ‘sets the lower bound to 0 yOption Base 1 ‘sets the lower bound to 1 zDim asName(5) As String ‘ Option Base 1 ‘ Option Base 0 asName

CP Principles of Programming Steve Garner and Ian Coulson Using Arrays in Applications zWe can use variables and expressions to determine the value of an array element zthis is illustrated in this simple programme which stores up to 5 names then prints them out in reverse order

CP Principles of Programming Steve Garner and Ian Coulson The programme Dim asNames(4) As String Dim iCount As Integer For iCount = 0 To 4 Input “Enter a name >”; asNames(iCount) Next iCount For iCount = 4 To 0 Step -1 Print asNames(iCount) Next iCount

CP Principles of Programming Steve Garner and Ian Coulson Explicitly Setting Array Bounds zWe can just declare how many elements we want in our array and QBasic will set them up using the Option Base setting: Dim asName(5) As String Option Base 1 asName zWe can however state explicitly the lower and upper bounds that we want for the array Dim asName(5 To 9) As String

CP Principles of Programming Steve Garner and Ian Coulson Declaring Multi-Dimension Arrays Consider a chess board, it has 64 squares, how would we define an array variable to hold the pieces that are on the board? Dim asBoard(64) As String ‘assumes option base 1 That isn't very obvious is it? Where on the board is asBoard(12), is it in the middle or on an edge?

CP Principles of Programming Steve Garner and Ian Coulson zA better solution would be to declare it as a multidimensional array Dim asBoard(8,8) As String ‘assumes option base 1 or explicitly Dim asBoard(1 To 8, 1 To 8) As String

CP Principles of Programming Steve Garner and Ian Coulson Using Multidimensional Arrays zUsing our declaration Dim asBoard(1 To 8, 1 To 8) As String zWe have a much clearer idea of where on the board the array variable points to

CP Principles of Programming Steve Garner and Ian Coulson Where To Use Multidimensional Arrays zQbasic will allow us multiple dimensions! What about a three dimensional chess board? Dim asBoard(1 To 8, 1 To 8, 1 To 8) As String asBoard(1,4,3) = “White Knight”

CP Principles of Programming Steve Garner and Ian Coulson Multidimensional arrays zWe would advise you not to use too many dimensions, otherwise you will become confused zBest idea is only to use multidimensional arrays where they map clearly to the real-world

CP Principles of Programming Steve Garner and Ian Coulson Clearing Arrays zTo clear a complete array you can use the Erase command: Erase asNames zThis resets all fields to their ‘null’ values E.g. all numerical values are set to 0 and strings to be empty

CP Principles of Programming Steve Garner and Ian Coulson Finding Array Boundaries zTo find the bounds of a single dimension array we can use : iLowerBound = LBound( asNames ) iUpperBound = UBound( asNames ) zTo find multi-dimensional bounds: UpperBound = UBound( asAddress, 2 )

CP Principles of Programming Steve Garner and Ian Coulson Summary zBe able to decide when to use an array zBe able to set up an array zBe able to write data to and retrieve data from an array

CP Principles of Programming Steve Garner and Ian Coulson Review questions 1 write a Dim statement to create an array to store the names of 100 famous footballers 2 write the statement that would assign the name “Michael Owen” into the 10th element of your array 3 The name Alan Shearer is stored in the 71st element, write a statement to print this out Return to view another lecture