Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Flow Charting, Structured English and PseudoCode
J AVA AND V ARIABLES. O VERVIEW Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 Lecture 32 Handling Selected Topics Again!! Structs and Arrays of Structs Special Lectures.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Searching Damian Gordon. Google PageRank Damian Gordon.
Problem Solving for Programming Session 8 Static Data Structures.
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
Introduction to Pascal The Basics of Program writing.
Python: Print Damian Gordon. Your first Python program When learning a new computer programming language, the first thing typically taught is how to write.
Variables Damian Gordon. Variables We know what a variable is from maths. We’ve all seen this sort of thing in algebra: 2x – 10 = 0 2x = 10 X = 5.
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
Modularisation Damian Gordon. Modularisation Let’s imagine we had code as follows:
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Other Variable Types Dim lab as String makes a box that can store a label tag Dim ColHead As String ColHead = “function” ColHead function Dim lab as Boolean.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Structures or Derived Types. Real Life Objects Often structured –Students, Employees, Cars, Accounts, Cricket matches, flats etc contain heterogeneous.
Variables in VB. What is a variable? ► A named memory location that stores a value.
DATA TYPES, VARIABLES AND CONSTANTS. LEARNING OBJECTIVES  Be able to identify and explain the difference between data and information  Be able to identify,
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.
Data Structures: Multi-Dimensional Arrays Damian Gordon.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Pseudo-code. Pseudo-code Task 1 Preparation Use the following code to declare, initialise and populate an array in a new VB.NET console application:
Python: File Management Damian Gordon. File Management We’ve seen a range of variable types: – Integer Variables – Real Variables – Character Variables.
Chapter 9: Data types and data structures OCR Computing for A Level © Hodder Education 2009.
OCR AS Level F452: Data types and data structures Data types and data structures a. define different data types, eg numeric (integer, real),
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
7 - Programming 7J, K, L, M, N, O – Handling Data.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Pseudocode (Revision)
Data Types and Structures
Linked Lists Damian Gordon.
Starter Question In your jotter write the pseudocode to take in two numbers, add them together then display the answer. Declare variables RECEIVE firstNumber.
Arrays, For loop While loop Do while loop
Decision Structures, String Comparison, Nested Structures
Outline Altering flow of control Boolean expressions
More Loops.
Queues: Implemented using Arrays
Simple Statistics on Arrays
Some Common Issues: Iteration
Sorting Damian Gordon.
The Data Element.
Circular Queues: Implemented using Arrays
C# Revision Cards Data types
The Data Element.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Data Structures: Arrays Damian Gordon

Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each person.

Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each person. E.g. – Integer Age1; – Integer Age2; – Integer Age3; – Integer Age4; – Integer Age5; – etc.

Arrays But if there was a way to collect them all together, and declare a single special variable for all of them, that would be quicker. We can, and the special variable is called an ARRAY.

Arrays We declare an array as follows: Integer Age[40];

Arrays We declare an array as follows: Integer Age[40]; Which means we declare 40 integer variables, all can be accessed using the Age name. ……..…

Arrays We declare an array as follows: Integer Age[40]; Which means we declare 40 integer variables, all can be accessed using the Age name ……..… 38

Arrays ……..…

Arrays ……..… So if I do: PRINT Age[0]; We will get: 44

Arrays ……..… So if I do: PRINT Age[2]; We will get: 42

Arrays ……..… So if I do: PRINT Age[39]; We will get: 82

Arrays ……..… So if I do: PRINT Age[40]; We will get: Array Out of Bounds Exception

Arrays ……..… We notice that Age[5] is blank. If I want to put a value into it (e.g. 54), I do: Age[5] <- 54;

Arrays ……..… We notice that Age[5] is blank. If I want to put a value into it (e.g. 54), I do: Age[5] <- 54;

Arrays We can think of an array as a series of pigeon-holes:

Array

Arrays If we look at our array again: ……..…

Arrays If we wanted to add 1 to everyone’s age: ……..…

Arrays If we wanted to add 1 to everyone’s age: ……..…

Arrays We could do it like this: PROGRAM Add1ToAge: Age[0] <- Age[0] + 1; Age[1] <- Age[1] + 1; Age[2] <- Age[2] + 1; Age[3] <- Age[3] + 1; Age[4] <- Age[4] + 1; Age[5] <- Age[5] + 1; ……………………………………………………… Age[38] <- Age[38] + 1; Age[39] <- Age[39] + 1; END.

Arrays An easier way of doing it is: PROGRAM Add1ToAge: N <- 0; WHILE (N != 40) DO Age[N] <- Age[N] + 1; N <- N + 1; ENDWHILE; END.

Arrays Or: PROGRAM Add1ToAge: FOR N IN 0 TO 39 DO Age[N] <- Age[N] + 1; ENDFOR; END.

Arrays If we want to add up all the values in the array:

Arrays If we want to add up all the values in the array: PROGRAM TotalOfArray: integer Total <- 0; FOR N IN 0 TO 39 DO Total <- Total + Age[N]; ENDFOR; END.

Arrays So the average age is:

Arrays So the average age is: PROGRAM AverageOfArray: integer Total <- 0; FOR N IN 0 TO 39 DO Total <- Total + Age[N]; ENDFOR; PRINT Total/40; END.

Arrays We can add another variable: PROGRAM AverageOfArray: integer Total <- 0; integer ArraySize <- 40; FOR N IN 0 TO 39 DO Total <- Total + Age[N]; ENDFOR; PRINT Total/40; END.

Arrays We can add another variable: PROGRAM AverageOfArray: integer Total <- 0; integer ArraySize <- 40; FOR N IN 0 TO 39 DO Total <- Total + Age[N]; ENDFOR; PRINT Total/ArraySize; END.

Arrays We can add another variable: PROGRAM AverageOfArray: integer Total <- 0; integer ArraySize <- 40; FOR N IN 0 TO ArraySize-1 DO Total <- Total + Age[N]; ENDFOR; PRINT Total/ArraySize; END.

So now if the Array size changes, we just need to change the value of one variable (ArraySize). PROGRAM AverageOfArray: integer Total <- 0; integer ArraySize <- 40; FOR N IN 0 TO ArraySize-1 DO Total <- Total + Age[N]; ENDFOR; PRINT Total/ArraySize; END. Arrays

We can also have an array of real numbers:

Arrays We can also have an array of real numbers:

What if we wanted to check who has a balance less than zero : PROGRAM LessThanZeroBalance: integer ArraySize <- 8; FOR N IN 0 TO ArraySize-1 DO IF BankBalance[N] < 0 THEN PRINT “User” N “is in debt”; ENDIF; ENDFOR; END. Arrays

We can also have an array of characters:

Arrays We can also have an array of characters: GATTCCAAG ……..… A

What if we wanted to count all the ‘G’ in the Gene Array: Arrays GATTCCAAG ……..… A

What if we wanted to count all the ‘G’ in the Gene Array: PROGRAM AverageOfArray: integer ArraySize <- 40; integer G-Count <- 0; FOR N IN 0 TO ArraySize-1 DO IF Gene[N] = ‘G’ THEN G-Count <- G-Count + 1; ENDIF; ENDFOR; PRINT “The total G count is:” G-Count; END. Arrays

What if we wanted to count all the ‘A’ in the Gene Array: PROGRAM AverageOfArray: integer ArraySize <- 40; integer A-Count <- 0; FOR N IN 0 TO ArraySize-1 DO IF Gene[N] = ‘A’ THEN A-Count <- A-Count + 1; ENDIF; ENDFOR; PRINT “The total A count is:” A-Count; END. Arrays

We can also have an array of strings:

Arrays We can also have an array of strings: Dog 0 Cat 1 Dog 2 Bird Fish Cat 34567

Arrays We can also have an array of booleans:

Arrays We can also have an array of booleans: TRUE 0 1 FALSE 2 TRUE FALSETRUE FALSE 34567

etc.