20201218 Computing Programming Basics Computing
20201218 Computing Programming Basics Computing
As well as string functions, programming languages have a range of other functions, for example, to find a square
root or round a real number to a given number of decimal places
The function ROUND (x, n) rounds a real number x to n decimal places. For example:
amount 25 / 3
newAmount ROUND (amount, 2)
OUTPUT newAmount
A random number between 0 and 100 can then be generated with a statement such as
Mark = RANDOM_INT (0, 100)
You could simulate the throw of a dice by making a random number between 1 and 6
Arrays
An array is a data structure that allows you to hold several values, all of the same type, with one name.
You can declare a variable with this syntax:
Int variablename[] = new string[number of elements]
If you wanted to store constant values in an array, for example the names of five different resorts with great
beaches, you could write the statement:
string[] resort = {"Margate", "Swansea", "Brighton", "St Ives", "Cromer" }
Benefits of using arrays:
1. code is easier to follow because there are less individual variables which makes it easier to follow and
maintain
2. a group of data items can be easily processed using a for loop
Student1 12 14 8 7 17
Student2 6 8 5 3 13
Student3 16 15 9 10 18
These marks could be put into a two-dimensional array. You can declare a 2d array like this:
int[,] array = new int[3, 5];
Lists are similar to arrays but the difference is they are dynamic - that is, the number of elements in a list can
increase or decrease during execution.
A record is a data structure consisting of a number of fields which can all be of different types
A field is a single item of data in a record.
Writing data to a file means that it will be permanently stored and you can read it, add data to the file, amend it,
sort it or process it in different ways whenever you need to.
Manipulating a file:
A statement to open the file will be required.
The mode also has to be specified – e.g. write to a new file, append data to an existing file or read from an existing
file
The actual file name and path needs to be assigned to the identifier used for the file in the program.
Here is an algorithm which reads a text file of student names and marks and prints each record:
markFile "marks.txt"
OPEN markFile in "READ" mode
markRec markFile.readline
WHILE markRec ≠ “”
Split record into two fields name, mark
OUTPUT (name, mark)
markRec markFile.readline
ENDWHILE
CLOSE markFile
Here is an algorithm which writes a text file of student names and marks:
markFile "marks.txt"
OPEN markFile in "WRITE" mode
OUTPUT ("Input first student name: ")
name USERINPUT
WHILE name ≠ "xxx"
OUTPUT ("Input student mark: ")
mark USERINPUT
WRITE name, mark to markFile
OUTPUT ("Input next student name, xxx to finish: ")
name USERINPUT
ENDWHILE
CLOSE markFile
If there is already data in the file, you can open it in APPEND mode to add data to the end of the file:
OPEN markFile in "APPEND" mode