Array Data Structure and Processing Arrays

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 9, 2014 Carolyn Seaman Susan Martin University of Maryland, Baltimore.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Eric Roberts and Jerry Cain
UMBC CMSC 104 – Section 01, Fall 2016
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own.
Repeating Code Multiple Times
Manipulating Pictures, Arrays, and Loops part 2
Calendar 2017.
Subroutines in Computer Programming
Before and After.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Based on lecture notes from Dr. Deepak Kumar of Brynmawr
Arrays, For loop While loop Do while loop
Objectives Create an array Work with array properties and methods
Code Elements and Processing Coordinate System
Based on lecture notes from Dr. Deepak Kumar of Brynmawr
Chapter 6 Arrays Solution Opening Problem
Starting JavaProgramming
Introduction to Python
Calendar of 2012 ESL Lesson on Months.
A Class Calendar in PowerPoint
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Calendar 2012 Example text here.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
Chapter 10 Algorithms.
CS-161 Computer Programming Lecture 14: Arrays I
Review for Final Exam.
Chapter 10 Algorithms.
More programming with "Processing"
Code Elements and Processing Coordinate System
` Structured Programming & Flowchart
Starting Out with Programming Logic & Design
slides courtesy of Eric Roberts
MY FUNDAY.
SEPTEMBER ½ Day Start Unit 1
Review for Final Exam.
SEPTEMBER 2014 Unit 1 Unit 1 Continued Unit 1 Continued
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own.
Unit 1: Quick Quizzes After 5,13
February , 2009 CSE 113 B.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
SEPTEMBER 2014 Unit 1 Unit 1 Unit 1 Unit 1 Quick Quiz 9,16, 21 Unit 1
Data Structures & Algorithms
SEPTEMBER ½ Day Unit PLC
Based on lecture notes from Dr. Deepak Kumar of Brynmawr
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
LCC 6310 Computation as an Expressive Medium
Manipulating Pictures, Arrays, and Loops
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
| January Sunday Monday Tuesday Wednesday Thursday Friday
IAT 800 Foundations of Computational Art and Design
LCC 6310 Computation as an Expressive Medium
January Monday Tuesday Wednesday Thursday Friday Saturday Sunday 30 31
Presentation transcript:

Array Data Structure and Processing Arrays Chapter 5 B. Ramamurthy 9/20/2018

Arrays We have seen int and float String colorName = “Olive green”; Pimage animal; Now lets say we want a collection of items of the same type. You use an array. Lets review some basic concepts that will help us in solving more complex problems than Lab1 and Lab2. 9/20/2018

Memory Game: A closed board and an open board size n =4, number of tiles = n X n = 16 Theme: baby animals… can be anything Question: how many pairs of pictures? 9/20/2018

Analysis and Design Lets analyze the problem Need to display blank board Initialize board to some representation of the pictures: lets use number pairs (0,0), (1,1), (2,2)…(7,7) in the case where n = 4, number of tiles = 16, there are 8 pairs of pictures Let the pictures be pic0, pic1, pci2,..pic7 Lets identify the data structures and design the algorithm before development of the code. 9/20/2018

Abstraction of the board Random placement Initialize -1 2 1 7 5 6 4 3 Abstraction of the board Random placement 9/20/2018

Algorithm Initialize board Display blank board Setup random number for the tiles for the pictures User selects tile 1: openTile1  row1, col1, tileNum1 User selects tile 2: openTile2row2, col2, tileNum2 Match the pair of tiles opened: matchPair() If match, Increment number of correct, If all tiles done, display number of tries Else no match, close tiles. 9/20/2018

Functional Decomposition Processing functions: setup, draw, mousePressed void initializeBoard(int n) void findRandomPair(int j) void openTile1() void openTile2() void matchPair() Void closeTiles() 9/20/2018

Arrays Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar 9/20/2018

Sequencing Refers to sequential execution of a program’s statements do this; then do this; and then do this; etc. size(200,200); background(255); stroke(128); rect(20, 20, 40, 40); 9/20/2018

Function Application Control transfers to the function when invoked Control returns to the statement following upon return void draw() { // Draw a house at 50, 250 in 200x200 pixels house(50, 250, 200, 200); house(20, 100, 50, 50); house(230, 100, 50, 75); } // draw() void house(int houseX, int houseY, int houseWidth, int houseHeight) { // Draw a house at <houseX, houseY> (bottom left corner) // with width houseWidth and height houseHeight … } // house() 9/20/2018

Function Application Control transfers to the function when invoked Control returns to the statement following upon return void draw() { // Draw a house at 50, 250 in 200x200 pixels house(50, 250, 200, 200); house(20, 100, 50, 50); house(230, 100, 50, 75); } // draw() 50 250 200 200 void house(int houseX, int houseY, int houseWidth, int houseHeight) { // Draw a house at <houseX, houseY> (bottom left corner) // with width houseWidth and height houseHeight … } // house() Parameter Transfer 9/20/2018

Repetition Enables repetitive execution of statement blocks lather rinse repeat void draw() { do this; then this; and then this; etc. } // draw() Repeat frameRate times/second Default frameRate = 60 9/20/2018

Loops: Controlled Repetition While Loop while (<condition>) { stuff to repeat } Do-While Loop do { stuff to repeat } while (<condition>) For Loop for (<init>; <condition>; <update>) { stuff to repeat } 9/20/2018

Writing Conditions in Processing Boolean expressions can be written using boolean operators. Here are some simple expressions… < less than 5 < 3 <= less than/equal to x <= y == equal to x == (y+j) != not equal to x != y > greater than x > y >= greather than/equal to x >= y 9/20/2018

Logical Operations Combine two or more simple boolean expressions using logical operators: && and (x < y) && (y < z) || or (x < y) || (x < z) ! not ! (x < y) A B A && B A || B !A false true 9/20/2018

Loops: Critical Components Loop initialization Things to do to set up the repetition Loop Termination Condition When to terminate the loop Loop Body The stuff to be repeated Loop update For the next repetition/iteration 9/20/2018

Key Computing Ideas The computer follows a program’s instructions. There are four modes: Sequencing All statements are executed in sequence Function Application Control transfers to the function when invoked Control returns to the statement following upon return Repetition Enables repetitive execution of statement blocks Selection Enables choice among a block of statements All computer algorithms/programs utilize these modes. 9/20/2018

Selection: If Statement if ( <condition> ) { do this } if ( <condition> ) { do this } else { do that } if ( <condition> ) { do this } else if ( <condition> ) { do that } else if (…) { … } else { whatever it is you wanna do } At most ONE block is selected and executed. 9/20/2018

Variables int x = 0; float delta = 0.483; color darkOliveGreen = color(85, 107, 47); String colorName = "Dark Olive Green"; PImage castle = loadImage("myCastle.jpg"); 9/20/2018

float petroleum = 40.0; float coal = 23.0; float naturalGas = 22.0; A Set of Sample Values Petroleum Coal Natural Gas Nuclear Renewable Hydropower 40.0 23.0 22.0 8.0 4.0 3.0 Declaration float petroleum = 40.0; float coal = 23.0; float naturalGas = 22.0; float nuclear = 8.0; float renewable = 4.0; float hydropower = 3.0; float[] consumption; consumption = new float[6]; Creation index 1 2 3 4 5 44.0 23.0 22.0 8.0 4.0 3.0 consumption 9/20/2018

A Set of Sample Values //Declare and create an array with size 6 float[] consumption = new float[6]; //store values consumption[0] = 40.0; consumption[1] = 23.0; consumption[2] = 22.0; consumption[3] = 8.0; consumption[4] = 4.0; consumption[5] = 3.0; Fixed size 9/20/2018

A Set of Sample Values //Define, create and initialize the data in an array float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0}; 9/20/2018

Arrays // An array to hold the names of all the days in a week String[] weekDays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; // two arrays, each containing high and low temperature values float[] highTemps, lowTemps; int[] count; // an array of integers PImage[] photos; // an array of photos // An array to hold the names of months in a year String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // The colors in a rainbow color[] rainbow = {color(255, 0, 0), color(255, 127, 0), color(255, 255, 0), color (0, 255, 0), color (0, 0, 255), color (111, 0, 255), color (143, 0, 255)}; 9/20/2018

Indexing, Size and Loops int[] n = new int[1000]; for (int i=0; i < n.length; i++) { n[i] = i; } int[] n = new int[1000]; for (int i= n.length-1; i>=0; i--) { n[i] = i; } 9/20/2018

for-each Loop Syntax Example for (variable : arrayName) { // do something with the value of variable } Example String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; for(String str : energySource) { println(str); } 9/20/2018

Example: A Simple Bar Graph String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0}; void setup() { size(400, 400); smooth(); } // setup() void draw() { // set up plot dimensions relative to screen size float x = width*0.1; float y = height*0.9; float delta = width*0.8/consumption.length; float w = delta*0.8; background(255); for (float value : consumption) { // draw the bar for value // first compute the height of the bar relative to sketch window float h = map(value, 0, 100, 0, height); fill(0); rect(x, y-h, w, h); x = x + delta; } } // draw() 9/20/2018

Array Operations String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0}; 9/20/2018

Printing println(consumption.length); println(consumption); println(energySource); 6 [0] 40.0 [1] 23.0 [2] 22.0 [3] 8.0 [4] 4.0 [5] 3.0 [0] Petroleum [1] Coal [2] Natural Gas [3] Nuclear [4] Renewable [5] Hydropower 9/20/2018

Try it Given the following arrays, String[] energySource = {"Petroleum", "Coal", "Natural Gas", "Nuclear", "Renewable", "Hydropower"}; float[] consumption = {40.0, 23.0, 22.0, 8.0, 4.0, 3.0}; write commands to print the values from energySource and consumption in the format shown here: Petroleum, 40.0 Coal, 23.0 Natural Gas, 22.0 Nuclear, 8.0 Renewable, 4.0 Hydropower, 3.0 9/20/2018

Min, Max and Soring float smallest = min(consumption); float largest = max(consumption); println(sort(consumption)); println(sort(energySource)); 9/20/2018

Other Array Operation Reverse the ordering of elements in an array Expand the size of the array append(), expand() Shorten it shorten() Concatenate or split arrays concat(), subset(), splice() Copy the contents of an array arrayCopy() 9/20/2018