Download presentation
Presentation is loading. Please wait.
Published byまさとし さかいざわ Modified over 6 years ago
1
TM111 Introduction to computing and information technology 1
Block 2 Part 2 Numbers, strings and lists
2
For An Introduction to OUBuild Go to:
3
Introduction In this meeting, you will build on this work, learning how to create increasingly powerful, interesting and varied programs. The main topics you will be introduced to are: • working with numbers in OUBuild • working with lists in OUBuild. • working with strings • working with variables. learn how to problem-solve in the context of programming – that is, how to take an idea for a program and develop it in stages until you have a working program. Programming with numbers Working with numbers is a common programming task, catered for in almost all programming languages. Ex, ticket prices, passwords, speed, ID numbers, PIN, etc.
4
Programming with strings
strings – sequences of characters such as 'Welcome to TM111!'. To a program, strings as input from a user. Ex, Password-checking programs . Programming with lists Lists provide a form of data storage, but, unlike variables, a single list can hold many items of data – many strings and/or numbers – at the same time. Lists enable programs to work with collections of data. See activity 2.1 Prototype programs. A prototype is a small, simplified version of what might eventually be part of a much larger program. Rather than attempting to create a full working system all at once, it is common for parts of it to be developed individually. Each of those parts may be developed in increasingly complex versions, starting with quite simple prototypes of what will eventually be required. to get a better idea of what the program will look like. Work on blocks given in your materials
5
2.1 Numbers 2.1.1 Introduction to numbers in OUBuild
Numerical calculations are at the heart of many powerful and varied programs, from games to ticket price websites to space rocket guidance systems. Ex. Currency exchange Exchanging currency from Kuwaiti KD to Dollars ($) involves the exchange rate, which is the number of KD to ($). So, for example, if the exchange rate is 3.15 and a customer wishes to Exchange 300 KD, then the equivalent amount in Dollars is 300 * plus charge of a commission if not free. See Activity 2.2 in your text. Precedence and nesting When nesting number blocks, it is important to ensure that calculations are carried out in the right order, matching the required arithmetic calculation. Nesting of the number blocks is vital. For example, when performing the calculation / 5, you should first divide 7 by 5 (giving 1.4) and then add that number to 2, giving 3.4 as the result. Nesting of number blocks relates to precedence – that is, to the order in which operations within a calculation are carried out. In the absence of brackets, multiplication and division have precedence over addition and subtraction, meaning that they are carried out first. Ex / is different than (2 + 7) / 5
6
2.1.2 Rounding, constants and comments
the order of precedence is BIDMAS : Brackets, Indices (powers), Division, Multiplication, Addition, Subtraction. See Activity 2.3 (self-assessment) Rounding, constants and comments See video, Visit : Explore activity 2.4 in your text The rules of rounding are: If the next digit along from the decimal place we are rounding to is less than 5, then we round down. If the next digit along is 5 or more, we round up. Round to one decimal place rounding down Round to two decimal places rounding up Round to the nearest whole number rounding up in OUBuild the key block is: round[ ],
7
You can add text comment to your program any where
You can add text comment to your program any where. Text comments within programs, designed only for the human reader. OUBuild ignores them when running the program. Used to provide explanation. Constants stored in a variable, but the value of the variable will not change as the program runs. Constants typically named using capitals and may hold any kind of unchanging data. A constant represents fixed data that is known to the programmer whilst creating the program, and hence that can be built in to the program. Code that is largely self-explanatory – for example, using variables with meaningful names – is sometimes referred to as self-documenting. Comments provide the human reader of code with explanation. They can help readability and for program maintenance. Maintenance is about keeping programs useful and correct as they are used over sometimes long periods of time. Do activity 2.5
8
2.2 Strings In OUBuild the join[ ][ ] block joins two strings, appending the string in its second input box to that in its first to form a new string Ex. if the variable name has value ‘Mohammad', then Join [‘Hello’] [name] reports “Hello Mohammad” Join [12] [34] reports But [12] + [34] reports 46 perform calculations. Do Activity 2.6 password generator Incremental construction can enable multiple strings to be joined without complex nesting or use of extra variables. a general technique that can be used more generally whenever a value can be constructed in stages. Do Activity 2.7 (self-assessment) More string manipulation The letter[ ]of[ ] block reports the character at a particular position in a string. Ex. Letter[1] of [“Welcome to TM111”] reports ‘W’ the character at position 1 is 'W‘. the character at position 8 is Space ‘ ‘ and so on. A string can be visualised as a sequence of numbered slots, each holding a character.
9
The length_of[ ] block reports the length of a string – that is, the number of characters in it. Length of [“Welcome to B TM111”] reports 18 Ex. Suppose msg = “Welcome to TM111” then Letter [ length_of [msg] ] of [msg] reports ‘1’ Ex. Join [ letter [ length_of [msg] - 6] of [msg] ] [ join [ letter [15] of [msg] ] [ letter [1] of [msg] ] ] ????? Do exercise in Activity 2.9
10
2.3 Lists and custom blocks
Data in the form of a number or a non-numerical string can be stored in a variable. At any given time, a single variable can store just one item of data. A list can store many items of data at a time. Most programming languages include similar data structures – that is, structures for storing collections of data. a list may hold any kind of data items can be added to and removed from a list as a program is running Explore Activity 2.10 in your text. Lists are always empty when first created. A list can be visualised as a sequence of slots, each holding an item of data. Slots are numbered, starting from 1. The add[ ]to[ ] block creates a new slot at the end of a list and stores item in it. The length_of[ ] block reports the length, that is, the number of items in a list. add[ ]to[ ] block with nothing entered into the first input box. An empty string is added to the end of the list Figure 1 The same item can be included more than once in a list, stored in different positions. See figure Figure 2.
11
Can you tell what is the delete[all]of[ ] block ?
The item[ ]of[ ] block reports the item at a specified position in a list. enables a program to access an item in a list, given the item’s position. The delete[ ]of[ ] block removes an item at a specified position in a list. Subsequent items are moved down one position to fill the gap and the list length is decreased by 1. The insert[ ]at[ ]of[ ] block inserts an item at a specified position in a list. Subsequent items are moved up one position and the list length is increased by 1. The replace_item[ ]of[ ]with[ ] block replaces an existing item at a specified position in a list. The original item is overwritten and the list length is unchanged. Can you tell what is the delete[all]of[ ] block ? Exercise. Given a list name: Courses as shown in figure L1. Report the following : item[3]of[Courses] insert[TM112]at[4]of[Courses] replace_item[8]of[Courses]with[M105] after running all blocks in the sequence at once what will the list Course looks like? Draw it. Figure L1
12
Strings and lists They have similarities.
A string consists of a sequence of characters; a list consists of a sequence of items. each character or item can be accessed using its position. Both have Length. and some differences You can insert[ ]at[ ]of[ ] to insert an item into a specific position within a list, but there is no block that inserts a character into a string. The designers of a programming language always have to make decisions about what features to build in and what to leave to the programmer. There is no built-in feature for a particular task, then the programmer has to construct their own code for the purpose. Do Activity 2.11 (self-assessment) Do Activity 2.12 (complete it by yourself) Then do Activity 2.13 (self-assessment) Adding visual and audio features. followed by activity 2.14.
13
2.4 From ideas to programs Algorithms and problem-solving
Program specifications typically arise from problems which might be expressed in English such as ‘How do I calculate the price of some theatre tickets?’ or a game or a calculation or any similar enquiries. Problem-solving in the context of programming involves working from a specification to the implementation of a program that does the task required: a solution to the problem. It also involves testing – checking whether a program really does solve the problem as required or has errors (bugs), and debugging – identifying and fixing any bugs Also involve huge teams of people working to solve problems. Algorithms and problem-solving An algorithm can be considered as a step-by-step guide (instructions) to completing a particular task to solve a problem To create a program, we need to be able to set out a step-by-step solution; an algorithm, in fact. However the raw material we start with – the specification – is often quite unstructured. Identifying the steps needed in an algorithm and writing them down clearly is a key skill that needs to be developed, and it requires lots of practice. Taking the problem-solving process in two stages – design and implementation – makes things easier.
14
2.4.1 Problem-solving in action
Designing an algorithm, implementing a program Do Activity 2.15 in your text. Creating an algorithm in simple natural English. Ex. Multiply number of full price tickets by full ticket price. Or: Set total for full price to number of full price tickets multiplied by full ticket price. Look at the program specification and then design your algorithm, make a set of tests intended to uncover any ways in which the program does not meet the specification. In testing, valid data are involved including ‘extreme’ situations such as an input of 0. Acceptance testing is a key stage in commercial software development. A client will not accept new software unless they are convinced that it does the job required. Convincing them involves carrying out sometimes thousands of tests to check every conceivable aspect of the specification, including every possible combination of different kinds of input. We can set this out in a small table showing the inputs with the expected results. The next step is to run the script with each combination of inputs . Then start debugging.
15
2.4.2 Problem-solving: worked example
In trying to trace and fix a bug, it is useful to bear in mind common errors that tend to be made in the sort of programs we are testing such as: incorrect data stored data stored in the wrong variable incorrect variables used in calculations wrong number block used (for example, []+[] instead of []*[]) steps implemented in the wrong order initialisation incorrect or missing incorrect nesting of calculations. Do Activity 2.16 in your text. 2.4.2 Problem-solving: worked example In a particular supermarket, the normal price of a 1.5-litre bottle of cola is £2. However, sometimes there is also a special offer on for the same brand of cola. A program is required to ask the user the conditions of the special offer (the price of the special offer; how many bottles the offer involves; and the volume of each bottle in the offer) and then to tell the user the difference in price per litre (to the nearest penny) between the special offer and the normal offer so that the user can decide which to buy.
16
Next, what more precisely is required?
Design an algorithm What data is needed? Supplied data: the price of the special offer, the number of bottles in the special offer, the volume of each bottle in the special offer – all inputs Fixed data: the normal price, the normal volume Next, what more precisely is required? Calculate the difference (special offer price per litre – normal price per litre), rounded to the nearest penny. Output rounded difference Now write down a first draft of the algorithm by clearly defining the inputs and setting out this outline process, including its results. Input price of special offer Input number of bottles in special offer Input volume of bottle in special offer Calculate the difference (special offer price per litre – normal price per litre), rounded to the nearest penny Different people might do this in slightly different ways, leading to different algorithms. • calculate the special offer price per litre • calculate the normal price per litre • subtract the latter from the former • round the answer to the nearest penny, that is, to two decimal places As in the following Figure:
17
calculate the price per litre of the special offer
Steps of the algorithm Imagine the volume of a bottle were 2 litres and its price £4. I can see that the price per litre would be £2, and I get that by dividing the price by the volume: £4/2 litres = £2 per litre The same process can be used whatever the numbers. I know the price of the normal offer and the volume of the normal offer. So the general algorithm I can apply for the normal offer is: Set price per litre of normal offer to price of normal offer / volume of normal offer calculate the price per litre of the special offer calculate the difference Put it all together
18
Variables, constants and initialisation
Implement Variables, constants and initialisation The normal price and the normal volume are known values: £2 and 1.5 litres, respectively. Treat these as constants and store them in variables NORMAL_PRICE and NORMAL_VOLUME, initialised in the script. use variables: special_price, special_number_of_bottles, special_bottle_volume to store the user inputs and normal_price_per_litre, special_total_volume, special_price_per_litre, difference and rounded_difference to store values needed during the calculation. Continue, as in your text, and Implement the algorithm in OUBuild and Test and debug. See results and record your solution. Do Activity 2.17 (exercise)
19
2.5 Fun and games Good day. Do Activity 2.18 and Activity 2.19
Follow all steps learned in previous section and practice using OUBuild to become familiar to it. Go back to your text and practice Answers to self-assessment activities. Good day.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.