CP1020 - 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.

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
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.
Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
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 Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
CP Principles of Programming Steve Garner and Ian Coulson CP Week 8 Arrays.
Chapter 7 & 8- Arrays and Strings
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.
PHP : Hypertext Preprocessor
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
08/09/ Arrays Defining, Declaring & Processing.
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
JavaScript Lecture 6 Rachel A Ober
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Array Processing.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Arrays - The Movie A Farelly Brothers Presentation. No. Not Really.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Fill the screen challenge! This is a starter activity and should take 3 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In interactive mode,
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Course A201: Introduction to Programming 09/30/2010.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Week 6.  Assessment 2  Do Loops  Custom Functions.
Chapter 7 P 1 Lists and Loops List boxes and combo boxes List box - list of items - useful for a list which does not change Combo box - list of items -
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Files Tutor: You will need ….
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
Maximum Profit Please use speaker notes for additional information!
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Computer Programming for Engineers
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
GCSE Computing: Programming GCSE Programming Remembering Python.
Multidimensional Arrays Computer and Programming.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Data Structures: Multi-Dimensional Arrays Damian Gordon.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
World of Wokcraft The very best in Single pan cooking themed fantasy gaming!
JavaScript: Functions.
Arrays Declare the Array of 100 elements Notes
Arrays An Array is an ordered collection of variables
Bryan Burlingame 28 November 2018
ARRAYS 2 GCSE COMPUTER SCIENCE.
VB.Net Programming Console Application
Presentation transcript:

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 few examples using multidimensional arrays

Use of a for loop to write to an array Suppose we want to store a series of names in an array John Jill Gordon Louise The design: 1. for 0 to 3 names 2. input name 3. next name number of name

The code DIM asNames(3) AS STRING DIM iCount AS INTEGER FOR iCount = 0 TO 3 NEXT iCount INPUT “ Enter a name > ”; asNames(iCount)

Lets extend this to store the names of four families JohnJillGordonLouise BillSueRitaChris CyrilRanjitCelineKen JunePeterDavidAlison We need a multi-dimensional array to store the family names. Each column belongs to one family member family

The design 1. For families 0 to 3 2. Next family 1.1 for family member 0 to next family member get name

The code DIM asName(3,3) AS STRING DIM iFamName AS INTEGER Dim iFamMember AS INTEGER FOR iFamName = 0 TO 3 NEXT iFamName PRINT “enter the names of this family “ FOR iFamMember = 0 TO 3 NEXT iFamMember INPUT asName(iFamName, iFamMember)

A second example zWe wish to store the positions of Xs on a treasure map. The map is split into a grid of 3 columns and 3 rows. There are 3 treasures hidden. X X X

Treasure map - design 1) Get three treasure co-ordinates 1.1 For iTreasure = 1 to next co-ordinates get row and column co-ordinates store X in the array at those positions

The code DIM asTreasure(1 TO 3, 1 TO 3) AS STRING DIM iCount, iRow, iCol AS INTEGER FOR iCount = 1 TO 3 INPUT “What are your treasure co-ordinates “; iRow; iCol asTreasure(iRow, iCol) = “X” NEXT iCount

The design to display the map zWe would also like to display the map on the screen 2) Display map 2.1 For Row = 1 to For Column = 1 to if array element = X print it else print a blank space next column move to next line 2.2 next row

The code to display the map FOR iRow = 1 TO 3 FOR iCol = 1 TO 3 IF asTreasure(iRow, iCol) = “X” PRINT asTreasure(iRow, iCol) ; ELSE PRINT “ “; ENDIF NEXT iCol PRINT NEXT iRow

Using another array to store corresponding information Suppose we want to store the ages of each family member we could use a second array of the same dimension. We can't use the same array as they are of different data types JohnRita Leeroy asNames aiAge

The design 1. For person 0 to 2 2. Next person 1.1. Get name 1.2. Get age

The code DIM asName(2) AS STRING DIM aiAge(2) AS INTEGER DIM iCount AS INTEGER FOR iCount = 0 to 2 NEXT iCount INPUT “persons name >”; asName(iCount) INPUT “persons age >”; aiAge(iCount)

Choose an array position Suppose we want to store data in a particular place in array. E.g. we want to store our favourite foods in order of preference. The design 1. For 5 foods 1.1 get position 1.2 get name of food 2. next food

The code DIM asFood(4) AS STRING DIM iPosition AS INTEGER DIM iCount AS INTEGER For iCount = 0 to 4 INPUT “The order for your preference 0 to 4”; iPosition INPUT “Your food ”; asFood(iPosition) Next iCount

An improvement What happens if the position you want to write to in the array already has data written in. We must test to see if the array element is empty and then write to it. The design 1 while positions < get name of food 1.2 get position 1.3 if position is empty enter data 2 loop

The code DIM asFood(4) AS STRING DIM iPosition AS INTEGER DIM iCount AS INTEGER DO WHILE iCount <5 INPUT “The order for your preference”; iPosition IF asFood(iPosition) = “” THEN INPUT “Your food ”; asFood(iPosition) iCount = iCount + 1 Else PRINT “this position has already been filled! Try again” END IF LOOP

Summary zWe have looked at one of the most important aspects of arrays - using loops to increment through them

Review questions 1 Write a piece of code that will allow the user to store their five favourite holiday destinations Return to view another lecture