# How do programs work?

1. Aug 29, 2011

### Worm

Ok, real basic stuff here. I have virtually no mathematics knowledge. I think I know that pseudo code is complied and turned into machine language but what I can't figure out is how one abstracts the instructions into algorithms(?). I'm having a hard time phrasing what I what to know. To para phase: How do you use math when writing a program?

2. Aug 29, 2011

### MarcoD

Usually, computer scientist abstract from concrete machines and postulate that all computers are concrete instances of abstract (Von Neumann) machines. They are nothing more than machines which consist of a central processing unit and memory (plus auxilliary input/output devices). Instructions are fetched in cycles from memory (the programs), read from memory (the data), and write data back.

An algorithm is an abstract definition of a program which in a concrete form can be defined in a concrete programming language and can be compiled to concrete machine instructions interpretable by a concrete machine (with a Von Neumann architecture).

The study of algorithms is based on the fact that an algorithm is mathematical in nature, that it can be abstractly defined in terms of an abstract mathematical language, and that algorithms defined in that language can be 'run' on an abstract mathematical machine. In general, algorithms can be studied as mathematical entities, and mathematical qualities of specific algorithms tell how any concrete instance of that algorithm on any concrete machine will behave.

First, you don't abstract instructions into algorithms. A series of instructions concretely defines a program, and abstractly defines an algorithm.

Second, in an abstract sense, you always use math to define a program since concrete programs are instances of abstract algorithms [which are mathematical entities].

An example: I want to write a program which adds two numbers.

Now you need to define a program. The search for that is a mathematical activity. In your mind, you're trying to figure out the mathematical solution to a mathematical specification. You end up with something, in pseudo code, in your 'mind's eye' like:

procedure add(x, y) : return x + y

Concretely, you now need to 'translate' your abstract notion of an algorithm to a concrete program, defined in a concrete programming language, which can be translated to concrete machine code, which can be run on a concrete machine.

So, you might end up with something in Java, or C, or any other language. For example,

int add_two_numbers(int x, int y) {
return x + y;
}

(Together with the rest of the 'plumbing' you need to turn it into a 'runnable' program like a main procedure, etc.)

Last edited by a moderator: Aug 29, 2011
3. Aug 29, 2011

### Staff: Mentor

Pseudocode is not compiled. Pseudocode is used only to work out the details of the algorithm that is to be used. Once these details are worked out, then an implementation of the function is written in the programming language of choice.

The program instructions are then compiled, and the compiler generates machine code instructions. The linker then combines the machine language that was generated by the compiler with machine language instructions for library functions, to produce an executable program.

Not all programming languages are compiled into machine code. Languages such as python and the .NET Framework languages are translated into an intermediate language, and another program translates individual instructions to machine language, and then executes them.

Not all programs require the use of mathematics. One example is a program that just displays some text on the console.

4. Aug 29, 2011

### MarcoD

I agree with most you said, but since the original question was also on the relation between programming and math, I would rather rephrase your last comment to: Most computing scientists believe that designing any (non-trivial) program is a mathematical activity.

EDIT: In a thread like this I can only provide short, dumbed-down, answers to questions. But you could dive into the question why students are taught pseudocode. And in finding an (historical) answer to that, if I don my CS hat, I would say that pseudocode was developed as a recognition that programs are mathematical in nature, that one can abstract from concrete programming languages, and that pseudocode should be taught to have student recognize that programs are instances of abstract algorithms (which, in later courses, can be shown to be amenable to mathematical analysis.)

Last edited by a moderator: Aug 29, 2011
5. Aug 29, 2011

### Worm

6. Aug 29, 2011

### rcgldr

I assume you mean a high level language which is compiled and turned in to machine language. Normally pseudo code is a simplified description of an algorithm or process that doesn't get into all the details. Think of psuedo code as an alternative to flow charts.

Normally it's done the other way around. The programmer(s) develop an algorithm, then implement that algorithm via some specific programming language, which could be high level or machine language. The computer just follows a stream of instructions.

Most programs don't involve a lot of math, it's more about the logic required to implement an algorithm.

A computer can follow a set of instructions, and has the ability to select different sub-sets of instructions based on testing of inputs (decision making). Normally this is done by branching, such as a common set of instructions for building similar items, where you skip over some instructions and follow other instructions, (if you're building "abc", then skip to step "qrs", if you're building "def", then skip to step "tuv").