(Latest Revision: 2014/09/07)
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 from RAM to the computer screen:
- cout << "Hello World\n" ; // literal string
- cout << price_of_gold << endl ; // value of a variable
- cout << "One plus two plus three equals: " << 1+2+3 << endl ; // string, then value of an expression
- copy data from the keyboard to the RAM,
- cin >> age_of_user ; // int variable
- cin >> interest_rate ; // double variable
- cin >> next_value >> go_again ; // maybe int, then char
- copy data from one location in RAM to another location in RAM, and
- value_one = value_two ; // simple copy
- change = amount_tendered - price ; // store value of expression
- copy between the RAM and the secondary memory (disk),
- infile_stream << "Report\n\nThe first value is:" << age_of_roof ; // output to a file
- outfile_stream >> floorSpace ; // input from a file
The CPU of the computer can perform simple operations on data.
- copy data from place to place (like the examples above)
- perform a specific operation (like add, subtract, multiply, divide)
- x = r + 2.3 ; // store value of simple expression
- r1 = -b + sqrt(b*b-4*a*c)/(2*a) ; // store value of more complex expression
- make a simple decision regarding which instruction to execute next (this
why it's possible to have if-then logic in programs, loops, and sub-programs.)
- if (age < 21) cout << "Sorry, we cannot allow this purchase.\n\n"
- if (a == 0) cout << "Can't divide by zero\n" else cout << b/2*a << endl ;
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." An example
of a C++ syntax error would be forgetting a semi-colon at the end of a
statement, or misspelling cout as count.
- Logical Error (incorrect meaning - like saying "Go left" when really you
meant "Go right") An example of a C++ logical error might be writing amount + price
you meant to calculate amount - price.
- 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.