UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D.

Slides:



Advertisements
Similar presentations
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Advertisements

UNIT IV.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Programming with Collections Collections in Java Using Arrays Week 9.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
B.Sc. Multimedia ComputingMultimedia Authoring Introduction to ActionScript 3.0.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
B.Sc. Multimedia ComputingMultimedia Authoring Using Arrays in Flash.
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
UFCFY5-30-1Multimedia Studio Scripting for Interactive Media Declaring and Using Arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
The University of Texas – Pan American
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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 
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
Collections behaviour and implementation of collections of primitives and objects.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter 6: Using Arrays.
Introduction To Programming Information Technology , 1’st Semester
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Arrays Week 2.
Arrays.
Presentation transcript:

UFCFSU-30-13D Technologies for the Web Declaring and Using Arrays in Unity 3D

UFCFS D Technologies for the Web Agenda Why Use an Array Array Declaration in C# in Unity 3D Searching and Sorting an Array Parallel Arrays Arrays of Data Array Access Errors

UFCFS D Technologies for the Web Need to Store Five Numbers Use individual variables for each number stored: int numberOne; int numberTwo; int numberThree; int numberFour; int numberFive; messy - have to process as individual items rather than as a collection of grouped items. Solution - use an Array - contiguous data storage

UFCFS D Technologies for the Web Arrays and Data Storage Elements The value stored at array element number 2 is 14 Arrays store values in ‘elements’ (compartments) i.e. contiguous memory locations

UFCFS D Technologies for the Web Arrays and Data Storage Elements The array has 5 elements 0..4 The value stored at array element number 4 is 23 Array element numbering starts at Thus the size of the array is 5

UFCFS D Technologies for the Web Arrays and Data Types An Array of numbers (integers) Gibson Fender Aria Martin Array of strings Les Paul

UFCFS D Technologies for the Web Declaring Arrays in C# // create a new array int[] numberArray = new int[5]; // store the numbers into the array // numberArray[0] = 15; numberArray[1] = 95; numberArray[2] = 14; numberArray[3] = 70; numberArray[4] = 23;

UFCFS D Technologies for the Web Array Declaration and Assignment in Unity 3d String[] nameArray = new string[5]; void Start () { nameArray[0] = "Hello"; nameArray[1] = "from"; nameArray[2] = "the"; nameArray[3] = "Name"; nameArray[4] = "Array"; }

UFCFS D Technologies for the Web Accessing Array Elements in Unity 3d // iterate through the array for (int index = 0; index <= 4; index++) { Debug.Log(nameArray[index]); }

UFCFS D Technologies for the Web Typical Aspects of using Arrays Search an array for a given value Sort an array in a given order Find the size of the array Use the length method in searching algorithms Compare the contents of one array with another array

UFCFS D Technologies for the Web // total the numbers stored in the number array int index; int sum; sum = 0; for (index = 0; index <= 4; index = index + 1){ sum = sum + numberArray[index] } Debug.Log("The sum of the numbers in the array is " + sum) Summing the Values of an Array

UFCFS D Technologies for the Web Summing the Values of an Array 2 // total the numbers stored in the number array using array // length method int index; int sum; sum = 0; for (index = 0; index <= numberArray.length; index++){ sum = sum + numberArray[index] } Debug.Log("The sum of the numbers in the array is " + sum)

UFCFS D Technologies for the Web Parallel Arrays name John Ben Freda Sue Mike

UFCFS D Technologies for the Web Parallel Array Search name John Ben Freda Sue Mike Search on name Extract on name

UFCFS D Technologies for the Web Parallel Array Search name John Ben Freda Sue Mike Search on name Find occurrences on name of

UFCFS D Technologies for the Web Arrays of Data (sounds) Array of.mp3 audio files footsteps.mp3 shell.mp3 siren.mp3 alarm.mp3 ambience.mp3

UFCFS D Technologies for the Web Array of Data (sounds) footsteps.mp3 shell.mp3 siren.mp3 alarm.mp3 ambience.mp3 Use code to produce random sound effect in 3D environment

UFCFS D Technologies for the Web Example of Accessing Random Array Elements in Unity 3d void Update () { // use Random.Range function that matches array size getRandomName = Random.Range(0.0, 4.0); // convert the decimal random value to an integer index = Mathf.RoundToInt(getRandomName); // use the value to access a random array element Debug.Log(nameArray[index]); }

UFCFS D Technologies for the Web Beware! The classic runtime error is where a program tries to access an array element beyond the array’s size. getRandomName = Random.Range(0.0, 5.0); The array has only five elements, but the code is attempting to access an element number six, which does not exist. The computer speak is the code has ‘fallen off the end of the array’

UFCFS D Technologies for the Web Summary Arrays are a fundamental structured data type in any programming language Arrays store values usually of the same type in a contiguous way which allows the data to be accessed and manipulated as a collection of data Typical array access is to search and sort the contents Associated data may be held in parallel arrays and use cross referencing to extract the required information Algorithms that manipulate arrays must stay within the array bounds or runtime errors will occur causing the program to ‘freeze’ or ‘crash’.