0% found this document useful (0 votes)
10 views

20201218 Computing Programming Basics Computing

The document covers programming basics, including data types, constants, functions, arrays, and file manipulation. It explains how to use built-in functions for data conversion, rounding, and generating random numbers, as well as the structure and benefits of arrays and records. Additionally, it provides algorithms for reading from and writing to text files, demonstrating how to manage student names and marks in a program.

Uploaded by

Harrison Grover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

20201218 Computing Programming Basics Computing

The document covers programming basics, including data types, constants, functions, arrays, and file manipulation. It explains how to use built-in functions for data conversion, rounding, and generating random numbers, as well as the structure and benefits of arrays and records. Additionally, it provides algorithms for reading from and writing to text files, demonstrating how to manage student names and marks in a program.

Uploaded by

Harrison Grover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Basics

Data types and operations


As well as variables, you can define constants in a program. Constants are
typically shown in uppercase. Constants always stay the same

High-level programming languages have built-in functions which are part


of the language. Examples of functions (written in pseudocode) :

 STRING_TO_INT(s) converts a string (variable s) to an integer


 STRING_TO_REAL(s) converts a string (variable s) to a number
with a decimal point
 INT_TO_STRING(x) converts an integer x to a string
 REAL_TO_STRING(x) converts a real number to a string
 CHAR_TO_CODE(‘a’) evaluates to 97, using ASCII
 CODE_TO_CHAR(97) evaluates to ‘a’ using ASCII

Substring means get the characters in


between. (e.g. in between position 2
and 5)

You should use comments in your


programs:
 to describe the purpose of the program
 to state the author of the program
 explain what the code does

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

The output of this would be 8.33.

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]

Index (aka subscript) – a single element or data item in an array

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

Suppose you wanted to hold in a program, 5 marks for each of 3 students

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.

Records and files


A data structure is a collection of data types such as integer, real, Boolean, char or string.

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.

You can declare a record with FilmID, Title and


year released like this:
Struct films
{
Public string Title
Public int yearreleased
Public int FilmID
}
A file is a collection of records. With text files, the only data type is “string”.
The simplest type of file is a text file

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

You might also like