Design Programming DECO2011 Rob Saunders web: http://www.arch.usyd.edu.au/~rob e-mail: rob@arch.usyd.edu.au office: Room 274, Wilkinson Building Data-Structures, Arrays and Loops Data-Structures Data-structure is a general term used to describe different ways of organising data in a computer’s memory so that they can be accessed or manipulated easily. Some examples of common data-structures include arrays, lists, sets, maps and trees. Arrays An array is a way to store multiple items of data in memory so that they are accessed using a single variable Each item of data in an array is given an index that can be used to refer to it. Arrays can be used whenever a program needs to store multiple copies of same type of data in one place. Declaring an Array Declaring a variable that points to an array is similar to declaring variables that point to primitive data types. The type of the data to be stored in the array is simply followed by square brackets. int[] arrayOfInts; Allocating an Array Before an array can be used the memory that it needs to store all the data has to be allocated. Memory is allocated using the “new” operator. To allocate memory for an array the new operator needs to know the type of number of data items that will be stored. Allocating an Array An array is allocated by following the new operator by the type of data followed by the size of the array in square brackets: arrayOfInts = new int[42]; It is also possible to use integer variable types to calculate the size of an array: arrayOfInts = new int[x + 5]; Declaring and Allocating Arrays It is also possible to declare and allocate an array in a single statement: int[] arrayOfInts = new int[42]; Unlike primitive data types however, this does not initialise the values of the array. The program has to do this later by setting the value of each data item. Initialising Static Arrays It is possible to initialise the values stored in an array don’t need to change using a special type of array allocation: arrayOfPrimes = {1, 2, 3, 5, 7}; This allocates an array big enough to hold five integers and initialises their values to the first five prime numbers. Using Arrays To access an item we use the variable name followed by the index in square brackets. char[] vowels = new char[5]; stuff[0] = ‘a’; stuff[1] = ‘e’; stuff[2] = ‘i’; stuff[3] = ‘o’; stuff[4] = ‘u’; Notice how the indices start at 0. An array with five items of data has indices 0,1,2,3,4. Iteration Stepping through each item of data in an array and performing some operation is called iteration. For example, we might want to iterate through an array of integers and add one to each of the values. Iteration (the hard way) Here’s one way we might add one to every integer in an array: stuff[0] = stuff[0] + 1; stuff[1] = stuff[1] + 1; stuff[2] = stuff[2] + 1; stuff[3] = stuff[3] + 1; stuff[4] = stuff[0] + 1; We can avoid repeating all these statements by using a loop. Loops There are three types of loop statements: • while loops • do-while loops • for loops While Loops Just like conditional statements (e.g. if/else) while loops use Boolean tests to control how a program executes some code. The Boolean test must evaluate to be true for the statements inside the curly brackets to be executed. While Loops The difference between an if statement and a while statement is that the statements inside the curly brackets continue to be executed as long as the test is true. A Simple While Loop In this example the statements inside the curly brackets will be executed as long as the value of x is less than 10, i.e. when x = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. int x = 0; while (x < 10) { println(x); x++; } What was that “++”? A program often needs to increase (or decrease) the value of an integer variable by one, especially in loops. So there is a quick way to write this: x++ is the same as writing x = x + 1 x-- is the same as writing x = x - 1 Other Quick Math Operators Java/Processing also includes shorthand ways to write simple addition, subtraction, mutiplication and division statements: x += 2 is the same as writing x = x + 2 x *= 3 is the same as writing x = x * 3 A While Loop Problem What is wrong with the following code? int x = 10; while (x < 10) { println(x); x--; } Do ... While Loops A do-while loop is similar to a while loop. The difference is that the test is performed after the code inside the curly brackets is executed rather than before. This means that the code is always executed at least once. Spot the Difference Here are two very similar loops, what will the output be from these two programs? int x = 0; do { println(x); } while (x > 1) int x = 0; while (x > 1) { println(x); } While Do ... While For Loops A typical for loop looks something like this: for ( int i = 0; i < 10; i++) { ... } It is made up of three parts: initialisation, test and operation. For Loop Initialisation In the initialisation part of a for loop, a variable is usually initialised for use within the rest of the loop, e.g. as a counter. for ( int i = 0; i < 10; i++) { ... } The initialisation is only executed once at the beginning of the loop. For Loop Boolean Test In the test part of a for loop, some Boolean condition is tested just like in an if or while statement. for ( int i = 0; i < 10; i++) { ... } The test is executed once at the beginning of each loop, just like in a while loop. For Loop Operation In the operation part of a for loop, a variable is often incremented. for ( int i = 0; i < 10; i++) { ... } The operation is executed once at the end of every loop. A Simple For Loop Here’s how we would write a for loop to print all the numbers between 0 and 9: for (int x = 0; x < 10; x++) { println(x); } Compare this code to the while loop and you’ll see all the same bits are there, just in slightly different places. Example 1: A Simple For Loop We can use a for loop to draw a row of boxes very easily: size(200, 200); int y = 20; for (int x = 20; x < width; x += 20) { rect(x-5, y-5, 10, 10); } Example 2: A Double For Loop • We can use two for loops (one inside the other) to draw a grid of boxes: size (200, 200); for (int y = 20; y < height; y += 20) { for (int x = 20; x < width; x += 20) { rect(x-5, y-5, 10, 10); } } Example 3: Using Random in a Loop We can use the random() function to draw a grid of boxes with random colours: size (200, 200); colorMode(HSB, 360, 100, 100); for (int y = 20; y < height; y += 20) { for (int x = 20; x < width; x += 20) { fill(random(360), 100, 100); rect(x-5, y-5, 10, 10); } } Transformations Transforming Drawings • Processing supports the transformation of drawing commands, e.g. translation, rotation and scaling, to make it easier to create programs that draw complex shapes. Transformation Commands translate(x, y) Translates subsequent drawing commands by (x, y) rotate(angle) Rotates subsequent drawing commands by angle scale(xs, ys) Scales subsequent drawing commands xs and ys Example 4: Rotating a Drawing fill(0); noStroke(); rotate(-0.4); rect(25, 25, 50, 50); Why did it rotate by so much? Programming languages like Java measure angles in radians rather than degrees. 2*PI radians = 360 degrees Processing provides an easy way to convert between degrees and radians: radians(x) converts degrees to radians degrees(x) converts radians to degrees Example 5: Rotating a Drawing using Degrees fill(255); noStroke(); rotate(radians(-23)); rect(25, 25, 50, 50); Changing the Origin of a Rotation Rotations are always relative to the current origin for drawing operations We can change the origin by using the translate() functions Example 6: Rotating Around the Center fill(255); noStroke(); translate(width/2, height/2); rotate(radians(-23)); rect(-25, -25, 50, 50); Example 7: Loops and Rotations size(300, 300); noFill(); smooth(); stroke(255, 255, 255, 127); rectMode(CENTER); translate(width/2, height/2); for (int r = 150; r > 0; r -= 2) { rect(0, 0, r, r); translate(0, 2); rotate(radians(-5)); } Example 7: Loops and Rotations Lab Assignment Lab Assignment Use loops to develop a grid of small random shapes/drawings. Use loops and transformations to develop complex static drawings. (Yes. You can use both of these techniques to develop your coursework assignment but start on something small to practice.)