Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

1.2. Algorithms and programming languages

Instructions contained in a program should realize some task, solve a problem. It is possible of course to write a program consisting of random instructions, but it doesn't make much sense. Thus, a program should be considered from other (then technical, characterized by executing of instructions by the CPU) point of view. Since programs are designed to solve problems, the starting point should be determined by formulating the problem or the task, the way of solving it and steps leading to achievement of the goal. In other words - by formulation of the algorithm that solves the problem or performs the task.

ALGORITHM is a recipe leading to solution of the given problem; set of commands concerning some objects (data) - with determined order of execution. These commands are executed by a device, which in response to signals representing commands reacts in their realization. Device may be represented by a human, computer or other appliance. (source: PWN Encyclopedia)

Algorithms are expressed in many ways: in natural language, graphically using a chart or in so called pseudocode (a language for expressing algorithms, independent of existing, available programming languages). Imagine one wants to buy a computer and needs to configure it to calcualte its price (by adding prices of its parts). The simplest solution realizing the above task is shown below:

  1. Pick a CPU from the price list and store its price.

  1. Pick RAM memory modules from the price list and store their price.

  1. Pick a motherboard from the price list and store its price.

  1. Pick a graphics card from the price list and store its price.

  1. Pick a hard disk from the price list and store its price.

  1. Pick a CDROM or DVD drive from the price list and store its price.

  1. Pick a sound card from the price list and store its price.

  1. Pick other necessary accessories and store their prices.

  1. Sum all the prices up.

T his solution is encoded in natural language. Graphically this algorithm can be represented as shown on the picture. Notice precisely specified order of the steps in the sequence on commands. The algorithm has well defined starting point. Its proper execution and stop is guaranteed. It can be executed many times with different input data - in this case with different configurations of the hardware. This algorithm is understandable for a man. One can apply it using a pen and a piece of paper. Now the question arises: how the task of calculating of the price can be done by a computer? As can be easily guessed, the above algorithm has to be rewritten in some programming language to obtain its source code, which can be then translated to the machine language understandable by the CPU. Thus obtained binary executable can be run on a computer. However from the beginning we encounter some fundamental problems: what the formula "pick ..., store its price" does really mean? Since the program is based on the algorithm, and will be executed by a computer its instructions should be formulated in terms of computer actions. Notice also, that generally an algorithm processes some input data (supplied by a user) to obtain output data as a result. Thus the following has to be specified: what data and when should be supplied to the program, and how they should be processed by the computer. There are 3 possibilities (at least):

  • user supplies the prices, program counts their sum

  • user supplies the profiles of components, program seeks their prices (for example searching the Internet) and sums them up

  • user defines criteria for choosing hardware, program - based on these criteria - picks specific devices and sums their prices up

In the first case the above algorithm changes only a bit, but remember that it is formulated in terms of computer activities.

1. Ask user for CPU price

2. Ask user for motherboard price

...

n-1. Sum given prices up

n. Display result to the user

The two other of the above possibilities lead to much more sophisticated algorithms. Note that the above simple algorithm has quite general form. Its translation to a specific programming language requires taking many decisions, for example:

  • how interaction with user should take place: how input data should be supplied and how the result (the output data) has to be displayed?

  • how to perform summing: are the prices of components to be stored separately or rather should they be added incrementally to the resulting sum?

  • how to react on erroneous input data?

These decisions relate to the so called user interface (the manner in which program communicates with user) and also to the structure of the algorithm in respect to its resistance to errors and ease of modifications. For example the above algorithm should check, whether the given input data are really numbers. Programmer should also consider storing the prices of components in case of potential future requirements pertaining to the output data: besides the sum, detailed "calculations report" might be needed as result (presenting price of each component or even its share in final price). The mere reaction to erroneous input data changes sequence of steps in considered algorithm. Solution to any problem requires usually - besides some simple sequence of steps - the following:

  • checking of a condition, and based on its result making decision pertaining to next steps of the algorithm

  • repetitive execution of specific instructions (given number of times or until a condition is fulfilled).

Taking into account possibility of potential errors in the input data and the need for storing prices of the components, algorithm for calculating price of a computer may look like this:

1. Ask a user for the CPU price.

2. If the price given is not a number, notify the user about error and go to step 1.

3. Store the given price of the CPU (for later reference)

4. Ask a user for the motherboard price.

5. If the price given is not a number, notify the user about error and go to step 4.

6. Store the given price of the motherboard (for later reference)

... other components

... other components

n-1. Calculate the sum of the components

n. Display result

In flow chart decisions are represented by the rhombus. Example: flow chart for the algorithm calculating tax. Algorithms are usually written in pseudocode - formalized (to some degree) form of natural language, independent of any programming language. Pseudocode is much closer to a programming language than natural language and is easier to transform into program written in some definite programming language. Various handbooks on programming present different forms of pseudocode. One can easily define one's own version. Pseudocode uses variables - symbolic representation of data (more about variables will be explained in the next lecture; for now let's treat them as variables in mathematical formulae). Operations on variables are coded with the help of operators - symbols of mathematical operations: addition, subtraction, multiplication, division etc. (more on this in the next lecture). Pseudocode also uses words and expressions precisely defining the meaning of fragments of algorithms (actions, instructions). For example taking a decision can be written down this way:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]