* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download p1-programming-constructs-05-12-16
Survey
Document related concepts
Concurrency control wikipedia , lookup
Relational algebra wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Tandem Computers wikipedia , lookup
Functional Database Model wikipedia , lookup
Ingres (database) wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Database model wikipedia , lookup
Transcript
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 x : 15 avg : 84.35 ch : ‘A’ • Structured data type => a data element contains a collection of data values scores : 85 79 92 57 68 80 name : ‘C’ ‘L’ ‘Y’ ‘D’ ‘E’ 4 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, – 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 0 Votes 1 2 3 Index 12 4 5 0 34 23 43 21 35 22 1 2 3 4 5 6 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: 0 1 R o w s 2 chessboard = table[8] [8] 3 chessboard = table[8, 8] 4 5 chessboard[8][8] 6 chessboard[8, 8] 7 0 1 2 3 4 Columns 5 6 7 Which Way to label? Which way should be label/reference a two dimensional array? R o w s 0 7 1 6 2 OR 3 4 5 6 7 R o w s 5 4 3 2 1 0 0 1 2 3 4 5 Columns 6 7 0 1 2 3 4 5 Columns Which square is Chessboard [4, 4] is referring to? 6 7 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 : – 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 fourthgeneration (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 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. Not all versions are case sensitive! • Syntax statements, words or phrases are always in lower case; keywords are in uppercase. 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 Columns describe one characteristic of the entity Name Address Jane Doe 123 Main Street John Smith 456 Second Street Mary Poe 789 Third Ave Rows describe the Occurrence of an Entity 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 INSERT using the keyword Variable INSERT INTO publishers VALUES (‘0010’, ‘pragmatics’, ‘4 4th Ln’, ‘chicago’, ‘il’) Keyword pub_id pub_name address state 0736 0010 New Age Books Pragmatics st Street 11 4 4th Ln MA IL 0987 0736 Binnet & Hardley New Age Books nd Street 21 2st Street 1 DC MA 1120 0987 Algodata Infosys Binnet & Hardley rd Street 32 3nd 2 Street CA DC 1120 Algodata Infosys 3 3rd Street CA Table is updated with new information 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) Using SQL 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 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 MI SELECT auth_name, auth_city FROM publishers auth_name auth_city Jane Doe Dearborn John Smith Taylor Using SQL 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 MI Using SQL To Update information in a database use the UPDATE keyword UPDATE authors SET auth_name=‘hello’ auth_id auth_name auth_city auth_state Hello Doe 123456789 Jane Dearborn MI Hello Smith 000000001 John Taylor MI Sets all auth_name fields to hello Using SQL To change a table in a database use ALTER TABLE. ADD adds a characteristic. ALTER TABLE authors ADD birth_date datetime null auth_id auth_name Type Initializer auth_city auth_state birth_date 123456789 Jane Doe Dearborn MI . 000000001 John Smith Taylor MI . ADD puts a new column in the table called birth_date Using SQL To delete a column or row, use the keyword DROP ALTER TABLE authors DROP birth_date auth_id auth_name auth_city auth_state auth_state 123456789 Jane Doe Dearborn MI . 000000001 John Smith Taylor MI . DROP removed the birth_date characteristic from the table Using SQL 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 MI DROP removed the database and returned the memory to system Time Left: Activity: w3Schools’ SQL Tutorial Work through w3Schools’ SQL tutorial SQL Learning Mat SQL Workbook SC : Creating and Manipulating SQL Databases Challenge Time Left: Activity: SQL with Khan Academy Work through Creating SQL databases with Khan Academy SQL Learning Mat SQL Workbook SC : Creating and Manipulating SQL Databases Challenge Time Left: Activity: SQL with Access Explore using SQL with Northwest Trader SQL Learning Mat SQL Workbook SC : Using SQL with Access Challenge 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 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) ---------------------------------------------------------------------------------------------------------- ① currentPlayer = playerOne move(currentPlayer) currentPlayer = playerTwo move(currentPlayer) ③ ② ④ • 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). Time Left: Task : Tic-Tac-Toe Design Identify the subroutines that would be used to design a program to play tictac-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) SC : Identify the subroutines requited to play Tic-Tac-Toc (O-X-O) Any questions?