Programming Constructs Andrew Csizmadia Monday 5th December 2016
Prerequisites for Learners For learners to be successful in using and engaging with programming theory, they will need to be: Familiar with fundamental programming constructs: selection, sequence and iteration Able to successfully read pseudocode Able to successfully write pseudocode Able to successfully debug pseudocode Able to use successfully use Computational Thinking concepts
arrays
Simple vs Structured Data Types Simple data type => data element contains a single value Structured data type => a data element contains a collection of data values x : 15 avg : 84.35 ch : ‘A’ scores : 85 79 92 57 68 80 name : ‘C’ ‘L’ ‘Y’ ‘D’ ‘E’
Big Idea - Array A variable can have a value that combines many values. You can: Extract one value Update part of the variable This idea is essential for representing complex data in programs: a song, an image a map
Big Idea - Arrays An array is a static data structure, Can: Cannot: i.e. its size is fixed Can: Select (i.e. index) one entry Update one entry Cannot: Add an extra entry to the start or end Insert/remove item from the middle
Arrays Arrays are Structured Static Data Types They have a means of accessing individual components using an index Values can be retrieved from and stored in the structure Structure given a single name Votes 12 34 23 43 21 35 22 Index 1 2 3 4 5 6 0 1 2 3 4 5 Individual elements accessed by index (integer Value) indicating relative position in collection
Arrays (1D) - Overview Structure - List Techniques - Create, Initialise, Search, Sort, Find Item, Reverse Implementation: Python (List) Application(s): Searching (Linear, Binary) Sorting (i.e. Quicksort)
Two-Dimensional (2D) Arrays Arrays may have multiple dimensions. Two-dimensional arrays can be visualized as tables with rows and columns. A two dimensional array is really an “array of arrays” where each element of a one-dimensional array contains another array.
Declaring a 2D Array Here is how you would declare a two-dimensional array chessboard with 8 rows and 8 columns: chessboard = table[8] [8] chessboard = table[8, 8] chessboard[8][8] chessboard[8, 8] 1 2 3 4 5 6 7 Rows 1 2 3 4 5 6 7 Columns
Which Way to label? Which way should be label/reference a two dimensional array? 1 2 3 4 5 6 7 7 6 5 4 3 2 1 Rows Rows OR 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Columns Columns Which square is Chessboard [4, 4] is referring to?
Conceptualizing Two-D Arrays int [ ] [ ] table = new int [4][5]; The variable table references an array of four elements. Each of these elements in turn references an array of five integers … table is really an array of arrays.
Conceptualizing Two-D Arrays More specifically, table is an array of four memory locations and in each memory location of that array there is an array that has five memory locations. So table[0] refers to the first row of the 2D array and table[1] refers to the second row, table[2] refers to the third row and table[3] refers to the fourth row. Using the code table[2][3] allows us to work with the memory location in the third row and fourth column named row 2 column 3.
Conceptualizing Two-D Arrays A table of numbers, for instance, can be implemented as a two-dimensional array. The figure shows a two-dimensional array with four rows and five columns that contains some numbers.
Accessing an Element of a Two-D Array Suppose we name the array table; then to indicate an element in table, we specify its row and column position, remembering that index values start at 0: x = table[2][3]; // Set x to 23, the value in (row 2, column 3)
Arrays (2D) - Overview Structure - Table Techniques - Create, Initialise, Search, Sort, Find Item, Reverse Implementation: Python (List of lists) Application(s): Storing a table of data Image representation Game simulation Modelling relationships, networks and maps with graphs Building blocks for other data structures, i.e. trees
records
Records In computer science, a record : http://www.bbc.co.uk/education/guides/zfd2fg8/revision/5 In computer science, a record : contains a collection of data for each entity usually recorded as a row in a table
SQL (STRUCTURED Query Language)
SQL (Structured Query Language) SQL stands for Structured Query Language. It is the most commonly used relational database language today. SQL works with a variety of different fourth-generation (4GL) programming languages, such as Visual Basic, Python.
SQL is used for: Data Manipulation Data Definition Data Administration All are expressed as an SQL statement or command.
SQL SQL Requirements SQL Must be embedded in a programming language, or used with a 4GL like VB or Python SQL is a free form language so there is no limit to the number of words per line or fixed line break. Syntax statements, words or phrases are always in lower case; keywords are in uppercase. Not all versions are case sensitive!
SQL and Relational Database A Fully Relational Database Management System must: Represent all info in database as tables Keep logical representation of data independent from its physical storage characteristics Use one high-level language for structuring, querying, and changing info in the database Support the main relational operations Support alternate ways of looking at data in tables Provide a method for differentiating between unknown values and nulls (zero or blank) Support Mechanisms for integrity, authorization, transactions, and recovery
Design SQL represents all information in the form of tables Supports three relational operations: selection, projection, and join. These are for specifying exactly what data you want to display or use SQL is used for data manipulation, definition and administration .
Table Design Name Address Jane Doe 123 Main Street John Smith SQL Table Design Columns describe one characteristic of the entity Rows describe the Occurrence of an Entity Name Address Jane Doe 123 Main Street John Smith 456 Second Street Mary Poe 789 Third Ave
Data Retrieval (Queries) Queries search the database, fetch info, and display it. This is done using the keyword SELECT SELECT * FROM publishers pub_id pub_name address state 0736 New Age Books 1 1st Street MA 0987 Binnet & Hardley 2 2nd Street DC 1120 Algodata Infosys 3 3rd Street CA The * Operator asks for every column in the table.
Data Retrieval (Queries) Queries can be more specific with a few more lines SELECT * from publishers where state = ‘CA’ pub_id pub_name address state 0736 New Age Books 1 1st Street MA 0987 Binnet & Hardley 2 2nd Street DC 1120 Algodata Infosys 3 3rd Street CA Only publishers in CA are displayed
Data Input Putting data into a table is accomplished using the keyword INSERT Variable INSERT INTO publishers VALUES (‘0010’, ‘pragmatics’, ‘4 4th Ln’, ‘chicago’, ‘il’) Keyword pub_id pub_name address state 0736 New Age Books 1 1st Street MA 0987 Binnet & Hardley 2 2nd Street DC 1120 Algodata Infosys 3 3rd Street CA pub_id pub_name address state 0010 Pragmatics 4 4th Ln IL 0736 New Age Books 1 1st Street MA 0987 Binnet & Hardley 2 2nd Street DC 1120 Algodata Infosys 3 3rd Street CA Table is updated with new information
pub_id pub_name address state 0010 Pragmatics 4 4th Ln IL 0736 New Age Books 1 1st Street MA 0987 Binnet & Hardley 2 2nd Street DC 1120 Algodata Infosys 3 3rd Street CA Types of Tables There are two types of tables which make up a relational database in SQL User Tables: contain information that is the database management system System Tables: contain the database description, kept up to date by DBMS itself Relation Table Tuple Row Attribute Column
Using SQL SQL statements can be embedded into a program (cgi or perl script, Visual Basic, MS Access) OR SQL statements can be entered directly at the command prompt of the SQL software being used (such as mySQL) SQL Database
Using SQL CREATE DATABASE database_name USE database_name To begin, you must first CREATE a database using the following SQL statement: CREATE DATABASE database_name Depending on the version of SQL being used the following statement is needed to begin using the database: USE database_name
Using SQL To create a table in the current database, use the CREATE TABLE keyword CREATE TABLE authors (auth_id int(9) not null, auth_name char(40) not null) auth_id auth_name (9 digit int) (40 char string)
Using SQL To insert data in the current table, use the keyword INSERT INTO INSERT INTO authors values(‘000000001’, ‘John Smith’) Then issue the statement SELECT * FROM authors auth_id auth_name 000000001 John Smith
Using SQL auth_id auth_name auth_city auth_state 123456789 Jane Doe If you only want to display the author’s name and city from the following table: auth_id auth_name auth_city auth_state 123456789 Jane Doe Dearborn MI 000000001 John Smith Taylor SELECT auth_name, auth_city FROM publishers auth_name auth_city Jane Doe Dearborn John Smith Taylor
Using SQL auth_id auth_name auth_city auth_state 123456789 Jane Doe To delete data from a table, use the DELETE statement: DELETE from authors WHERE auth_name=‘John Smith’ auth_id auth_name auth_city auth_state 123456789 Jane Doe Dearborn MI 000000001 John Smith Taylor
Using SQL auth_id auth_name auth_city auth_state 123456789 Jane Doe To Update information in a database use the UPDATE keyword UPDATE authors SET auth_name=‘hello’ auth_id auth_name auth_city auth_state 123456789 Jane Doe Dearborn MI 000000001 John Smith Taylor Hello Hello Sets all auth_name fields to hello
Using SQL auth_id auth_name auth_city auth_state 123456789 Jane Doe To change a table in a database use ALTER TABLE. ADD adds a characteristic. ALTER TABLE authors ADD birth_date datetime null Type Initializer auth_id auth_name auth_city auth_state 123456789 Jane Doe Dearborn MI 000000001 John Smith Taylor birth_date . ADD puts a new column in the table called birth_date
Using SQL auth_id auth_name auth_city auth_state 123456789 Jane Doe To delete a column or row, use the keyword DROP ALTER TABLE authors DROP birth_date auth_id auth_name auth_city auth_state 123456789 Jane Doe Dearborn MI 000000001 John Smith Taylor auth_state . DROP removed the birth_date characteristic from the table
Using SQL auth_id auth_name auth_city auth_state 123456789 Jane Doe The DROP statement is also used to delete an entire database. DROP DATABASE authors auth_id auth_name auth_city auth_state 123456789 Jane Doe Dearborn MI 000000001 John Smith Taylor DROP removed the database and returned the memory to system
Activity: w3Schools’ SQL Tutorial Work through w3Schools’ SQL tutorial Challenge SQL Learning Mat https://www.khanacademy.org/computing/hour-of-code/hour-of-sql/v/welcome-to-sql SQL Workbook SC : Creating and Manipulating SQL Databases
Activity: SQL with Khan Academy Work through Creating SQL databases with Khan Academy Challenge SQL Learning Mat https://www.khanacademy.org/computing/hour-of-code/hour-of-sql/v/welcome-to-sql SQL Workbook SC : Creating and Manipulating SQL Databases
Activity: SQL with Access Explore using SQL with Northwest Trader Challenge SQL Learning Mat https://www.khanacademy.org/computing/hour-of-code/hour-of-sql/v/welcome-to-sql SQL Workbook SC : Using SQL with Access
SQL Summary SQL is a versatile language that can integrate with numerous 4GL languages and applications SQL simplifies data manipulation by reducing the amount of code required. More reliable than creating a database using files with linked-list implementation Determine the best close for your audience and your presentation. Close with a summary; offer options; recommend a strategy; suggest a plan; set a goal. Keep your focus throughout your presentation, and you will more likely achieve your purpose.
Design - Subroutines Subroutines are used to allows us to breakdown (decompose) a complex task into a set of solvable tasks in order to produce a solution to the original task. Subroutine may be : a function which returns value(s) a procedure which does not return a value
Design - Subroutines Using subroutines allows the designer or programmer to focus on one task as a time. Advantages Modular approach Efficient – only need to replace subroutine if change is required.
Subroutines and Parameters move (playerToPlay) ------------------------------------ Value(s) can be passed to a subroutine via parameter(s) to be used in that subroutine Aim – create a generalised subroutine, which perform the same task using different values. (Efficient design, Elegant design). currentPlayer = playerOne move(currentPlayer) currentPlayer = playerTwo ① ② ③ ④
Task : Tic-Tac-Toe Design Identify the subroutines that would be used to design a program to play tic-tac-toe (O-X-O) Challenge(s) Identify the subroutines which will need parameter(s) passed to them Identify the subroutines (functions) which will return parameter(s) https://users.soe.ucsc.edu/~sbrandt/111/code/DESIGN SC : Identify the subroutines requited to play Tic-Tac-Toc (O-X-O)
Any questions? Refreshments in diner 4pm