Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Constructs

Similar presentations


Presentation on theme: "Programming Constructs"— Presentation transcript:

1 Programming Constructs
Andrew Csizmadia Monday 5th December 2016

2 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

3 arrays

4 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 : name : ‘C’ ‘L’ ‘Y’ ‘D’ ‘E’

5 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

6 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

7 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 Individual elements accessed by index (integer Value) indicating relative position in collection

8 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)

9 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.

10 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

11 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?

12 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.

13 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.

14 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.

15 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)

16 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

17 records

18 Records In computer science, a record :
In computer science, a record : contains a collection of data for each entity usually recorded as a row in a table

19 SQL (STRUCTURED Query Language)

20 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.

21 SQL is used for: Data Manipulation Data Definition Data Administration
All are expressed as an SQL statement or command.

22 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!

23 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

24 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 .

25 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

26 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.

27 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

28 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

29 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

30 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

31 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

32 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)

33 Using SQL To insert data in the current table, use the keyword INSERT INTO INSERT INTO authors values(‘ ’, ‘John Smith’) Then issue the statement SELECT * FROM authors auth_id auth_name John Smith

34 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 Jane Doe Dearborn MI John Smith Taylor SELECT auth_name, auth_city FROM publishers auth_name auth_city Jane Doe Dearborn John Smith Taylor

35 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 Jane Doe Dearborn MI John Smith Taylor

36 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 Jane Doe Dearborn MI John Smith Taylor Hello Hello Sets all auth_name fields to hello

37 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 Jane Doe Dearborn MI John Smith Taylor birth_date . ADD puts a new column in the table called birth_date

38 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 Jane Doe Dearborn MI John Smith Taylor auth_state . DROP removed the birth_date characteristic from the table

39 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 Jane Doe Dearborn MI John Smith Taylor DROP removed the database and returned the memory to system

40 Activity: w3Schools’ SQL Tutorial
Work through w3Schools’ SQL tutorial Challenge SQL Learning Mat SQL Workbook SC : Creating and Manipulating SQL Databases

41 Activity: SQL with Khan Academy
Work through Creating SQL databases with Khan Academy Challenge SQL Learning Mat SQL Workbook SC : Creating and Manipulating SQL Databases

42 Activity: SQL with Access
Explore using SQL with Northwest Trader Challenge SQL Learning Mat SQL Workbook SC : Using SQL with Access

43 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.

44 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

45 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.

46 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

47 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) SC : Identify the subroutines requited to play Tic-Tac-Toc (O-X-O)

48 Any questions? Refreshments in diner 4pm

49


Download ppt "Programming Constructs"

Similar presentations


Ads by Google