CS midterm Flashcards
algorithm
- Specific set of instructions for carrying out a procedure or for solving a problem (anything trying to solve)
- Must produce a result
- Must be achievable/possible
- Must be expressed clearly
algorithms typically (qualities)
Algorithms typically… • Make some assumptions • Have multiple solutions • Include decisions • Are expressed in modular pieces
is ordering Chinese take-out an algorithm?
yes
computer algorithm
“An algorithm is a well-ordered collection of unambiguous and effectively computable operations that when executed produces result and halts in a finite amount of time.”
- Algorithms are well ordered
- Have unambiguous operations
- Effectively computable operations
- Produce a result
- Finish in a finite amount of time
programming errors
- Syntax Errors – spelled words wrong
- Runtime Errors – program error that occurs when program is running
- Logic Errors – something wrong with logic, produces the wrong output
specify a location
- point(x, y)
* line x1, y1, x2, y2)
function call statement
- point(40, 77); = statement
- Point = function name
- (40, 77) = arguments
- ; = terminator
Coding Style when Calling Functions
- One space after commas in function arguments
- No space between function name and opening bracket
- No space before the semicolon at the end of a statement
- No space after opening bracket or before closing bracket
- point(10, 20);
shapes
- triangle(x1, y1, x2, y2, x3, y3);
- 6 arguments
- quad(x1, y1, x2, y2, x3, y3, x4, y4);
- 8 arguments
- rect(x, y, width, height)
- ellipse(x, y, width, height)
- point xy in middle
- eg. ellipse(50, 50, 15, 15);
commenting code
- Code comments provide information that is not otherwise available from reading from the code
- Often a message to your “future self”
- // comment
bezier curves
•
bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2)
• eg. bezier(10, 10, 90, 20, 30, 80, 90, 90);
attributes
- Eg. Once you set strokeWeight to 10, everything you draw after will have a strokeWeight of 10 until you change it again
- Order matters (Sequential Control flow)
- CMD + t will format code correctly
• size(); o changes size of drawing • noStroke(); o no border • noFill(); o nothing in the middle
stroke weight
• strokeWeight(weight); o Effects everything that comes after the code • strokeJoin(join); o join = BEVEL, MITER, or ROUND • strokeCap(cap); o cap = SQUARE, PROJECT, or ROUND o Project extends stroke
color
- Additive (“mixing all colours makes white”)
- RGB – used to specify colour on computer displays
- HSB – used to describe colour
- Subtractive (“mixing al colours makes black”);
- CMY/CMYK – common in printing
active code
change things actively (draw)
functions
• Code that is “packaged” so it can be run “by name” • Often performs some computation and returns a value (but not always) • We used functions all the time in Processing… o noFill(); o ellipse(75, 50, 23, 52);
function defintion
• void draw() { • ellipse(moouseX, mouseY, 30, 30); • } o void = function return type o draw = function name o () function parameters o {} code block
active vs static mode
• We have been writing code in static mode:
o Our drawing is only shown when the program finishes
o There is no user input
o We can’t define any functions
• From now on, we’re using active mode:
• “active mode” is also called “dynamic mode”
o People can interact with our program
o Drawings can be “updated” to make animation
o We can define functions
o With setup() and draw() functions
set up
- void setup() {
- size(200, 200);
- backround(255); // white
- noStroke();
- }
static mode
- size(200, 200);
* ellipse(mouseX, mouseY, 30, 30);
active mode
- void setup() {
- size(200, 200);
- }
- void draw() {
- ellipse(mouseX, mouseY, 30, 30);
- }
other functions
frame rate
• frameRate(1); // 1 frame per second o Draws very slow • printLn(“setup”) o Prints lines in the bottom of Processing in Console o Used to find errors
event
- An observable occurance, phenomenon, or an extraordinary occurrence
- A message to notify an application that something happened
- Ex. Keyboard (key press, key release), Mouse Events (button press, button release, moved)
setup and draw are events
• The setup event happens when the program is first run o setup() is a built-in function that represents this event • The draw event happens 60 times per second (by default) o draw() is a built-in function that represents this event