(Latest Revision: 2014/01/08)
CS 1500 Week One Notes
Here we have a graphic depicting a computer system.
The purpose of any language is the expression of ideas.
In the case of a computer programming language, the only ideas that need
expression are actions to be performed by a computing system like the one
depicted in the diagram.
What actions are such systems capable of?
A lot of what computers can do centers around "storage" - which is also
referred to as "memory." In order to get work done, the computing system
needs to "move data around". In other words, data needs to be copied from one
storage location to another. Thus computer languages need to express commands
to:
- copy data from the keyboard to the RAM,
- copy data from one location in RAM to another location in RAM,
- copy between the RAM and the secondary memory (disk), and
- copy from RAM to the computer screen.
If you look at the details of how things like A-D above are actually done,
you'll find that the steps involved are pretty complicated.
For example, to copy some data from the disk to the RAM, instructions have
to execute in the CPU that communicate with the disk controller via the system bus,
telling it what sector to read, and where in RAM to copy the sector.
Then the program that made the request has to wait,
and the controller has to operate the disk, get the sector,
copy it to the desired RAM location via the system bus,
and then notify the waiting program that operation has completed,
so that the program knows when it can continue with its next step.
Programming languages like C++ are designed to "hide" all that complexity
from the programmer, so s/he can focus more on the big picture. The C++ language
has simple commands that cause the computer do complicated things.
For example, the C++ statement:
f_in >> number ;
is the kind of C++ statement that causes the computer to copy data from the disk to a location in the RAM.
The CPU of the computer can perform simple operations on data. Therefore
computer languages need to express commands that tell CPU's to
- copy data from place to place
- cin >> number ; // copies something from the keyboard into RAM
- num2 = number ; // copies one item in RAM to another RAM location
- perform a specific operation (like add, subtract, multiply, divide)
- speed = distance / time ; // performs division and puts the answer in RAM
- total = price + tax ; // performs addition and puts the answer in RAM
- report the outcome of the operation, and
- cout << value ; // write a number to the computer screen
- file_out << price + tax ; // write a number to the end of a file
- make a simple decision regarding which instruction to execute next
- if (age >= 21) cout << "This person is an adult."
else cout << "This person is a minor."
Normally, the Operating System provides the "face" of the computer that the
user sees. The OS is a virtual computer - it simulates a computer that is
more convenient to use than the real hardware.
What is programming?
- A programmer designs a program.
- The programmer uses an editor (like notepad, texteditor, xcode or jove)
to write the algorithm in a high level language such as C++.
- The programmer or user executes a compiler to translate the high level
program (source code) into a machine language program (object code) that
the computer is able to execute.
- A person who wishes to use the program executes the machine language
version of the program.
- There's a variation of this paradigm that involves 'interpretation' - in
some programming environments, the user executes an interpreter program
with the high-level language as an input. The interpreter translates the
program one statement at a time, and executes the machine language
translation of each line immediately after translating it. The paradigm
in our class will be to use a compiler, not an interpreter.
When editing there is an interplay between the primary and secondary memories.
- The goal of the programmer is to use the editor to create a file on
SECONDARY memory - a file that contains the source code of the program.
- However, while editing, the programmer actually makes changes to a
(volatile) COPY of the file. The COPY resides in the primary memory.
After s/he makes updates with the editor, the programmer has to perform a
SAVE OPERATION in order to copy the updates to the permanent copy of the
file in secondary memory.
Word processors are great for creating many kinds of documents but they are
absolutely the WRONG tool for creating a program - because compilers can't
understand anything but 'plain text'.
Programming is basically just problem solving. The challenging part is to
figure out the steps required to solve the problem - the algorithm.
Once we have an algorithm, it's pretty easy to figure out how to write a
program that performs the steps. Of course, there IS a learning curve to
getting good at THAT part of the process too.
In an algorithm, ORDER is EXTREMELY important. You have to specify what is
done first. second, third and so on. If something is done out of order, it
can ruin the entire outcome of the program.
Quoting from our text: "To qualify as an algorithm, a set of instructions must
completely and unambiguously specify the steps to be taken and the order in
which they are taken."
Steps in program design
- Specify: What EXACTLY do we want this program to do?
- Figure out an algorithm that satisfies the specs.
- Check/Test the algorithm
- Write the program in a high level language
- Test the program.
The process described above is often not as 'linear' as described. Often one
has to back up and re-do steps - for example when testing reveals errors.
A mistake or error in a program may be called a bug. The three main types of bugs are:
- Syntax Error (bad syntax - e.g. bad C++ grammar) An example of a syntax
error in English would be a spelling error like spelling climate
"klaimutt", or incorrect part of speech: "He are my friend."
- Logical Error (incorrect meaning - like saying "Go left" when really you
meant "Go right")
- Runtime Error (Something that may turn out to be illogical, but it can't
be known until the program actually executes - like instructions that say
"first read the numbers X and Y from the keyboard input, then divide Y by
X" -- but then when the program reads X, X=0 and the operation Y/X can't
be performed.)
Compilers will catch syntax errors and print error messages (which are
typically hard to understand). The programmer (and helpers) are usually stuck
with the job of finding logical and runtime errors. However, compilers may
print "warnings" that help programmers discover logical or runtime errors.