Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming – syllabus knowledge

Similar presentations


Presentation on theme: "Programming – syllabus knowledge"— Presentation transcript:

1 Programming – syllabus knowledge
This covers the knowledge dot points. skillsprogramming still needs to occur for full understanding

2 Characteristics of simple data types:
Integer: often referred to as whole numbers. In pseudocode ‘int’ Real (floating point number): can contain decimal or precise numbers. In pseudocode ‘real’ or sometimes ‘float’. Boolean: data type having only two values. Usually True or False. In pseudocode it is often used in Selection or loop type structures. Character: used to store alphanumeric text such as letters, numbers, spaces, symbols and punctuation. Careful with use of symbols and punctuation as they are often reserved characters within the language that have a meaning of their own.

3 Constants and variables

4 Complex Data structures

5 Characteristics of complex data STructures
String: (pseudocode:str) Strings can be any length. They can contain a variety of characters or can even be empty. In pseudocode and many languages, length can be calculated using the function len() strings can be reversed: reverse() strings can be concatenated: “This is a string variable called”+ “string1” + “and it stores a string”. other functions include: upcase, downcase, capitalize Where appropriate, strings can be converted to integers s_to_i to peform mathematical calculations integers can also be converted to strings to perform string manipulation.

6 Data structures

7 Why use them

8 Complex data types continued
Arrays: one dimensional arrays store values of the same format. They are useful for storing things like, examination scores, temperatures, rainfall, names etc. An array uses a counter and a for or while loop, along the lines of for names < 20 input array[names] end This allows you to input 21 names into the array called array (generally the index starts at 0, this is the accepted standard for many languages and pseudocode, but always check the context carefully) The array is a useful storage structure that is accessible by the index and it decreased the number of variables you would need to use (name1, name2 etc) As a variable, an array can do all the things a variable can do. If the array contains integers, then averages, totals etc can be calculated.

9 Arrays continued Declaring as variable: Variable int: MarksList[marks]; //array of integers If you know how many elements: Markslist[14]; integer //array of integers with 15 elements Initialising Reading Calculations

10 Declaring an array Format for declaring an array: data_type array_name[number of values to hold]; For numerical arrays – if all of the spaces are not initialized, the remaining ones are set to 0 int IntAr[10] = {2, 4, 6, 8}; The number of elements in the array is not always used or known when declaring an array int IntAr[] = {3, 5, 7};

11 Last one in array SET Max to array[0] FOR i = 1 to array length - 1 // -1 used to designate last item in array IF array[i] > Max THEN SET Max to array[i] ENDIF ENDFOR PRINT Max

12 Setting up array

13 Adding element

14 average

15 Len average – good if you don’t know number of elements in array

16 Array search

17 Binary search

18 exercises 1. Declare an array with elements 44, 23, 42, 33, 16, 54, 34, 18, and print them out as follows: [44, 23, 42, 33, 16, 54, 34, 18] 2. Declare an array with elements 44, 23, 42, 33, 16, 54, 34, 18, and print them out as follows: Declare an array with elements 44, 23, 42, 33, 16, 54, 34, 18, and check if the length of the array is even or odd. 4. Declare an array with elements 44, 23, 42, 33, 16, 54, 34, 18, and check if the first element of the array is the same value as the last one. 5. Declare an array with elements 44, 23, 42, 33, 16, 54, 34, 18, and print them out in reverse order as follows: 18

19 variable Local – local in scope Global – global in scope Parameters – can be passed into module and used (value) and then another passed back as a result (reference) to be used elsewhere

20 Naming conventions for variables
Begin with lowercase letter Contains no spaces Additional words begin with capital Unique names within code Consistent use of names Underscores are also good Careful with reserved characters ie #, %, &

21 Control structures Sequence: line by line: Keep statements in sequence and starting at the same point down the lines. Selection: If/Then/Else: Statements should be indented inside the selection structure but not the selection keywords Begion Input (name) X = name Print (x) Print (“Are you enjoying control structures?”) end Loop: Indent all the statements that fall inside the loop but again, not the keywords that trigger the loop if this then that else this end if Do while heather != Name writeln (“your name is not heather? T/F” ) input (name) End while DoUntil… Do For….

22 STatements Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if,

23 Modularisation Modularisation is the process of dividing a problem into separate tasks, each with a single purpose. Top-down design methodology allows the programmer to concentrate on the overall design of the algorithm without getting too involved with the details of the lower- level modules. Benefits of modular design The major benefits of using a modular design are: Ease of understanding - each module should perform just one function. Reusable code - modules used in one algorithm can also be used in other algorithms. Elimination of redundancy - using modules can help to avoid the repetition of writing out the same segment of code more than once. Efficiency of maintenance - each module should be self-contained and have little or no effect on other modules within the algorithm.

24 The Mainline Since each module performs a single specific task, a mainline module should provide the master control that ties all of the modules together and coordinates their activity. This program mainline should show the main processing functions, and the order in which they are to be performed. It should show the flow of data and the major control structures. The mainline should be easy to read, be of manageable length and show sound logic structure. Generally, you should be able to read a mainline and see exactly what is being done in the program. Modular Algorithm Example Design a program that will prompt for the user for three numbers, accept those numbers, sort them into ascending order and output them to the screen. The program should continue to read numbers until ‘000’ is entered. Applying the top-down design methodology, the major tasks required to complete solve this problem are: Read 3 numbers While numbers NOT '000' Sort 3 numbers Print 3 numbers Read 3 numbers

25 Modularising fibonacci
PROGRAM FibonacciNumbers: Fibonacci Calc Module def CalcFib(): a = int(input("Please input value: ")) //converting str to int FirstNum = 1 SecondNum = 1 print("Running total: ", SecondNum, " ( )") while a != 1: # DO total = SecondNum + FirstNum print("Running total: ", total, " (",FirstNum,"+",SecondNum,")") FirstNum = SecondNum SecondNum = total //what is the scope of ‘total’ a = a - 1 ENDWHILE; # END. Main Program print("Calculating Fibonacci Numbers...") CalcFib()

26 Modularising prime checking
Prime Checking Module def IsItPrime(): a = int(input("Please input value: ")) b = a - 1 IsPrime = True while b != 1: DO if a % b = = 0: THEN IsPrime = False ENDIF; b = b - 1 ENDWHILE; return IsPrime END IsItPrime. Main Program PROGRAM CheckPrime: if IsItPrime() == True: THEN print("Prime number") else: print("Not a prime number") END.

27 bubblesort You need to watch this>>>> AND THIS PROGRAM BetterBubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 for outerindex in range(0,len(Age)): DO for index in range(0,reducingindex): if Age[index+1] < Age[index]: THEN TempValue = Age[index+1] Age[index+1] = Age[index] A ge[index] = TempValue ENDIF; ENDFOR; reducingindex = reducingindex - 1 print(Age) END.

28 Stubs A stub is a small program routine that substitutes for a longer program, possibly to be loaded later or that is located remotely. For example, a program that uses Remote Procedure Calls ( RPC ) is compiled with stubs that substitute for the program that provides a requested procedure. The stub accepts the request and then forwards it (through another program) to the remote procedure. When that procedure has completed its service, it returns the results or other status to the stub which passes it back to the program that made the request. A stub returns a value that is sufficient for testing. The stub does not need to perform the real calculation.

29 Bubble sort/Min/Max PROGRAM MinVal: Age = [44, 23, 42, 33, 18, 54, 34, 16] MinVal = Age[0] for index in range(0,len(Age)): DO if MinVal > Age[index]: THEN MinVal = Age[index] ENDIF; ENDFOR; print(MinVal) # END. PROGRAM MaxVal: MaxVal = Age[0] if MaxVal < Age[index]: MaxVal = Age[index] print(MaxVal)

30 Inter module communication
There are two methods for inter-module communication 1. Variables: Global and local 2. Parameters: actual v formal, value v reference

31 structure

32 Module definition Formal parameters Pass by Reference Parameter Actual parameters Value Parameters

33 functions

34 Why use Functions Can be re-used over and over again To make code easier to read To allow for easier update and maintenance They are efficient It is usually preferable for efficiency of coding to use a function to return a value. If possible, a parameter should be altered within the code and passed back as a value (via a function) rather than an altered parameter.

35 Last word – it is in the UK/WA/HSC syllabus
A function returns a value and a procedure just executes commands. The name function comes from math. It is used to calculate a value based on input. A procedure is a set of command which can be executed in order. In most programming languages, even functions can have a set of commands. Hence the difference is only in the returning a value part. But if you like to keep a function clean, (just look at functional languages), you need to make sure a function does not have a side effect. It comes down to fully fledged/qualified/experienced programmers preference! This depends on the context. In Pascal-like languages, functions and procedures are distinct entities, differing in whether they do or don't return a value. They behave differently wrt. the language syntax (eg. procedure calls form statements; you cannot use a procedure call inside an expression vs. function calls don't form statements, you must use them in other statements). Therefore, Pascal-bred programmers differentiate between those. In C-like languages, and many other contemporary languages, this distinction is gone; in statically typed languages, procedures are just functions with a funny return type. This is probably why they are used interchangeably. In functional languages, there is typically no such thing as a procedure - everything is a function.

36 Records – but it looks like a database?

37 Why use database oops….record?
Using variables to store data is not always convenient E.g. PersonAge, PersonHeight, PersonMarried, PersonSurname It’s more convenient in this case to use a structure to hold multiple pieces of related data The structure is a container - think of a suitcase –carry its contents in one hand instead of holding many separate items. Data records are saved to a special file format, called the random file They are not plain text like serial files are, not human readable Since every record is exactly the same length, when records are written end-to- end it’s easy to calculate where any particular record begins.

38 An array (single dimension) of records
BEGIN LoadArrayof Records // from HSBC – NSW syllabus Open ProductFile for input Set i to 1 Read ProdArrayofRecords (i) from ProductFile ‘note the use of the priming read in case the file is empty WHILE not EOF i = i + 1 END WHILE Close ProductFile Display “There are “ i “product records read in from the file” END LoadArrayofRecords To write updated data from the array of records to a sequential file, the following pseudocode is used: BEGIN WriteArrayof Records Open ProductFile for output Write ProductFile from ProdArrayofRecords (i) WHILE i <= NumRecords Display i “product records have been written to the file” END WriteArrayof Records

39 Or the pascal way – much easier
Procedure call Why no counter increment?

40 Open file – close the file
A file contains data which is saved in the hard disk. You can only view a file from the hard disk by using an operating system. A can contain text data which is used by word processors. Many text files are saved in the hard disk with the extensions: *.txt and *.doc. The file with extension of *.txt, means that it is created by the Microsoft Notepad. Whenever you use the Microsoft Notepad and save a text file, this saved file is created and written on to the hard disk. However, some programs have various formats on how to maintain the text - such as justification, font, font colour. So many files contain more data other than text data! The program itself uses various techniques to create a file in such a way that, when it is being read again it can read (using programming skills, etc..) if it is justified, its font style etc.. Remember to open the file and close the file. Exam markers usually look out for this bit Program Lesson8_Program1; Var UserFile : Text; FileName, TFile : String; Begin Writeln('Enter the file name (with its full path) of the text file:'); Readln(FileName); {A .txt file will be assigned to a text variable} Assign(UserFile, FileName + '.txt'); Reset(UserFile); {'Reset(x)' - means open the file x} //or Open(UserFile); Repeat Readln(UserFile,TFile); Writeln(TFile); Until Eof(UserFile); Close(UserFile); Readln; End.

41 Code Types – 2015 Paper

42 Compiler v interpreter
Complier: converts the whole code into machine code before running it. Interpreter: converts the code one instructions at a time, running each instruction before translating the next

43 Errors Syntax: is an error in the grammar of the language you are using. Usually your IDE will offer an interpreter that will allow you to debug where syntax issues are as it goes through the code line by line. Line numbers with errors are provided (frequently the line number provided by the interpreter means there is a problem on the line after. Logic: Harder to pick up. Errors in logic will mean that you do not get the result you were expecting. Best picked up by Desk Checks and trace’s. Eg/ 20/0 will produce a logic error as you cannot divide 20 by 0 Run-time: Frequently a result of not enough memory to run the program. Check memory allocated to the program (in preferences). Check how much RAM your machine has. Also check for OS updates and IDE updates.

44 Trace tables

45 Documentation Examples of External documentation – to help users of system Data Dictionary’s, Variable lists, How to manuals, FAQ’s, Quick reference guides, IPO charts, User Guide, Where is the Requirements Specification? Where is the Impact Analysis Document? Where is the Design Document? Have you documented all the assumptions, limitations properly? Have you done review of all the documents? Did you get sign off on all the documents from all the stakeholders? Examples of Internal Documentation – to help developers/designers of the system Comments, indentation or anything adds to the ability to understand the code and its purpose.

46 Data validation Range checking : setting a range of acceptable values. Type checking : Declaring variable types: ie str, int, real, float, char, bool

47 SDC stages of the software development cycle (SDC)
analyse detailed requirements design data and algorithms code data structures and instructions debug syntax and logic errors test to meet specifications document internally and externally implement and test with live data evaluate performance of the program

48 Some good references Damian Gordon UK Computer Science subject (very similar to WA)


Download ppt "Programming – syllabus knowledge"

Similar presentations


Ads by Google