VCE IT Theory Slideshows By Mark Kelly Vceit.com Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming Logic and Design Sixth Edition
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
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.
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
Iteration Conditional Loops Counted Loops. Charting the Flow of Control We’ve used flow charts to visualise the flow of control. The simplest form is.
Games and Simulations O-O Programming in Java The Walker School
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Programming Logic and Design, Second Edition, Comprehensive
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Lists in Python.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
VCE IT Theory Slideshows By Mark Kelly vceit.com Data Types 1 a.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
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.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
6.2 For…Next Loops General Form of a For…Next Loop
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
VCE IT Theory Slideshows by Mark Kelly study design By Mark Kelly, vceit.com, Begin.
VCE IT Theory Slideshows By Mark Kelly vceit.com Version 2 – updated for 2016 Data Types 1 a.
VCE IT Theory Slideshows by Mark Kelly study design By Mark Kelly, vceit.com, Begin.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
VCE IT Theory Slideshows by Mark Kelly study design By Mark Kelly, vceit.com, Begin.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Learning Javascript From Mr Saem
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
VCE IT Theory Slideshows
VCE IT Theory Slideshows
Loops BIS1523 – Lecture 10.
While Loops BIS1523 – Lecture 12.
Arrays & Functions Lesson xx
Arrays, For loop While loop Do while loop
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Introduction to Problem Solving and Control Statements
VCE IT Theory Slideshows
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Programming Logic and Design Fifth Edition, Comprehensive
Conditional Loops Counted Loops
Presentation transcript:

VCE IT Theory Slideshows By Mark Kelly Vceit.com Arrays

Variables Arrays – 1 and 2 dimensional VB objects – textbox, listbox etc Data Storage Structures

Variables 1 variable contains one piece of data Accessed by individual name Not good when you need to group large quantities of related data Cannot work on a group of variables in one operation.

Variables You’d need code like this: total_cost = fuel_expense + rent_costs + food_costs + insurance + repair_costs A lot of typing! Hard to manage 1000 costs…

Arrays A storage structure with many storage locations addressable by index number E.g. COSTS(12) Defines an array called COSTS with 12 ‘slots’ COST(1), COST(2), COST(3)… COST(12)

Zero-based Arrays Other languages might start numbering at zero COST(0), COST(1), COST(2)… COST(11)

Declaring Arrays In BASIC, arrays are declared (created, instantiated) using the DIM (short for DIMENSION) statement Includes the name of the array, and its size, e.g. DIM COST(12) AS SINGLE

Array data types Can create arrays of any legal data type DIM ClassSize as INTEGER = 28 DIM BIRTHDATES(ClassSize) as DATE DIM GNAMES(ClassSize) as STRING DIM FEES(ClassSize) as SINGLE

Using arrays total_cost = COST[1] + COST[2] + COST[3] + COST[4] Not much better than using individual variables… But teamed with loops, the power of arrays is unleashed! Because the name of the storage structure (COST) remains the same, only the index needs to be specified…

Add up costs with a loop Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 You could work with 12 or a million items with the same number of lines of code!

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Initialise counter value

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Initialise total value

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 START THE LOOP

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Replace value of TOTAL with the value of total plus COST array’s slot 1. Equal to TOTAL ← TOTAL + COST[1]

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Increment counter – now = 2

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Counter = 2 so it is less than or equal to 12, so it loops back to the LOOP statement

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Second time around

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Replace value of TOTAL with the value of total plus COST array’s slot 2. Equal to TOTAL ← TOTAL + COST[2]

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Increment counter again, now = 3

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Counter = 3 still less than 12 so do it all again… until counter>12

And so on… Time passes, the counter is incremented to 12

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Counter = 12, so it’s still within the looping range

It loops one more time and finally counter gets incremented to 13.

Counter ← 1 total ← 0 LOOP TOTAL ← TOTAL + COST[counter] Counter ← Counter + 1 WHILE counter <= 12 Counter = 13 so it fails the WHILE test and drops down to the following line of code.

Or, more elegantly (in VB) DIM counter AS INTEGER = 0 Total = 0 FOR counter = 1 TO 12 Total = Total + COST(counter) NEXT

Array dimensions The COST array above is an example of a one dimensional array. Arrays, however, can have two, three or more dimensions.

For example Consider the need to store 3 values for each of 12 months (e.g. rent, phone costs, transport). You need 36 storage locations. If you created 36 discrete (separate) variables, there would be a lot of typing, and you would have very little power to manipulate the data. However, create a two dimensional array like this... COSTS[12,3]

Think of it like a table with 12 rows (or columns) and 3 columns (or rows). Cells in the array are addressed Melways-style by referring to the row/column num.

Like this

More dimensions, more loops! Now you can add the grand total using 2 nested loops - one inside the other. Here I'll use BASIC's FOR..NEXT loop. FOR monthnum = 1 to 12 FOR costnum = 1 to 3 total = total + COST[monthnum, costnum] NEXT costnum NEXT monthnum

The FOR statements each have a loop counter (monthnum and costnum). Each counter starts at the starting value (1) and goes up top the ending value (12 for the month loop and 3 for the cost loop).

So the pair of loops begins. Monthnum is initialised to 1. The very next statement is another FOR so costnum is initialised to 1. The totalcost calculation is carried out, and it plugs in the current values of monthnum and costnum so you get totalcost = totalcost + COST[1, 1] In other words, totalcost = totalcost + 20.

The "NEXT monthnum state" terminates the inner loop. It checks to see if the counter has reached its ending value. If not, it loops back to the matching FOR statement, increments the value of the counter (so costnum now is 2) and repeats, giving... totalcost = totalcost + COST[1, 2] In other words, totalcost = totalcost + 30.

After the third loop of the costnum loop, its counter hits its max value and it drops down to the NEXT statement of the monthnum loop and that loop now terminates, checks whether it has finished, and loops back to start all over again, this time with monthnum = 2. So you can see that for each month that ticks by, three costs are processed.

Eventually, after all 3 costs are added for all 12 months, the monthnum loop finishes and the program continues. If you're still with me, good. We're ready for three dimensional arrays.

Imagine, as before, there are 3 costs over 12 months, but now there are a hundred years... That's 3600 values to process. Not easy with discrete variables. But with loops and an array, it's a doddle. Create the array: DIM COST[100,12,3]

For each of the 100 years, there are 12 months. For each month there are 3 costs. Then the turbo kicks in...

FOR year = 1 to 100 FOR month = 1 to 12 FOR cost = 1 to 3 totalcost += COST[year, month, cost] NEXT cost NEXT month NEXT year

Look at the nested loops like a car's odometer counting kilometers. The innermost dial is spinning the fastest. Every time it hits '9', the next dial ticks over 1. When that dial passes '9', the next dial ticks over 1. That can continue for as many dials (loops) as you care to create (or your brain can handle).

Want an example of a 4 dimensional array? Think of the example above, but now the same data must be stored for 16 different departments in the organisation.

Five dimensional array? Same as above, but now there are branches of the organisation in 33 countries, each with 16 departments, each with 100 years, each with 12 months, each with 3 costs. Now we're up to 2,059,200 values - all controlled with a handful of lines like this...

FOR country = 1 to 33 FOR dept = 1 to 16 FOR year = 1 to 100 FOR month = 1 to 12 FOR cost = 1 to 3 totalcost += COST[country, dept, year, month, cost] NEXT cost NEXT month NEXT year NEXT dept NEXT country

Relax. The study design only mandates the knowledge of two-dimensional arrays. Not only can you manage sheer amounts of data, you can customise your processing. We'll revert to an earlier scenario to make life easier.

Let's say we just wanted the total for year 56. Easy. year = 56 FOR month = 1 to 12 FOR cost = 1 to 3 totalcost += COST[year, month, cost] NEXT cost NEXT month

Or we could loop through in a different direction to find the totals for cost 2 over years 33 to FOR year = 33 to 77 FOR month = 1 to 12 totalcost += COST[year, month, 2] NEXT month NEXT year

(Notice how I used the constant 2 in this case. It's usually not wise, since it's so inflexible: after all, 2 will always be 2. Using a variable lets you change the cost num easily. When the true power of arrays and loops finally shines upon you, it's a bit like discovering girls... the possiblities are nearly endless.

Practice A challenge: how would you represent the following data constructs? - a list of 13 people's incomes? - a chess board? - a deck of playing cards? - rainfall figures from 5 locations in each of 6 states over the past 80 years?

By Mark Kelly vceit.com These slideshows may be freely used, modified or distributed by teachers and students anywhere on the planet (but not elsewhere). They may NOT be sold. They must NOT be redistributed if you modify them. VCE IT THEORY SLIDESHOWS