Chapter 1
Goals
From the book:
- To Understand what a computer program is.
- To be able to list the basic stages involved in writing
a computer program.
- To understand what an algorithm is.
- To learn what a high level programming language is.
- To be able to describe a compiler and what it does.
- To understand the compilation and execution process.
- To learn the history of the C++ programming language.
- To learn what the major components of a computer are and
how they work together.
- To be able to distinguish between hardware and software.
- To learn about some of the basic ethical issues confronting
computing professionals.
- To be able to choose an appropriate solving method
for developing an algorithmic solution to a problem.
What is a computer?
com · put · er n, often attrib (1646): one that
computes; specific: a programmable electronic device that can store
retrieve, and process data.
(
Webster's Collegiate Dictionary)
At one time this was:
- group of people.
- a collection of vacuum tubes.
- a collection of transistors.
- a very expensive device.
- and many other things.
What is a program?
program v, (1896): to work out a sequence of operations
to be performed by (a mechanism): provide with a program.
To insert a program (a particular action) into or as if into a
mechanism.
To predetermine the thinking, behavior, or operations of as if by
computer programming.
(
Webster's Collegiate Dictionary)
In our case, a series of instructions to a computer.
What do you already know how to program?
- An alarm clock?
- A VCR?
- A microwave oven?
- An electronic radio?
- Your dog?
- People? (Instructions to your house?)
How would you program the above? Give an example?
How does programming a dog (or person) differ from programming
a VCR?
- Time involved?
- Accuracy?
- Freedom of actions?
- Intelligence?
My new definition:
computer n, an unintelligent machine that can
very accurately follow a simple set of instructions exactly as they are
given. (Dan)
How do you convert from military (24 hour) time?
Get (read, ask ...) the time in military time
If the time is larger than 1259
Subtract 12 from the time
ending is pm
If the time is not past 1259,
ending is am
Return (say, write ...) the time and the ending
Examples:
0930 = 9:30 AM
1304 = 1:04 PM
Programming is an art and a science. A programmer must learn a number of
techniques and tools to correctly program, however they must also learn
how to put these pieces together to solve a problem. This is somewhat like
building a house, without the physical presence.
Programming
Our book suggests a three phase approach
- Problem Solving Phase
- Analysis and specification
- General Solution (algorithm)
- Verify
- Implementation Phase
- Concrete Solution (program)
- Test
- Maintenance Phase
Any one of the later phases could lead us back to a previous phase.
The longer we spend at the earlier phases of this process, the easier it is
to find and correct mistakes in the process.
The later in the process we find a mistake (bug), the more expensive it is
to fix.
Some Definitions:
Algorithm a step by step procedure for solving a problem in a finite
amount of time.
bug a mistake in a computer program. The original bug was a moth
that was caught in a relay.
Bugs can be in the syntax of spppeling. or formal, grammer, ov language the.
Bugs can also be in the logic of a program:
To calculate pay:
get hours
get hourly_rate
if hours are greater than 40
pay = 1.5 * hourly_rate * hours
if hours are 40 or less
pay = hourly_rate * hours
output hours
The process of finding these errors is known as debugging.
After we have produced an algorithm for a given problem, we must translate
that into a programming language.
A programming language is a set of grammatically rules and symbols used
to construct a computer program.
There are many different programming languages:
- COBOL
- FORTRAN
- Basic
- Ada
- lisp
- C, C++
The process of translating an algorithm into a programming language is
called coding.
An implementation of an algorithm in a programming language is called a
program.
Again, let me restate the importance of a good design before you begin
coding.
It is sometimes useful to write smaller programs to better understand
how something works, or how to use a programing technique or concept.
Before, during and after the coding phase is the documentation phase.
Documentation is additional information that the programmer provides so
- you can understand what you did. (Code is cryptic)
- others can understand what you did. (You might move to another job)
- end users can understand what your program does.
Good documentation is both internal (inside the program), and external. We
will talk about this as time goes by.
Comments are a form of documentation that is within the code.
Information is any knowledge that can be communicated.
Data is information in a form that a computer can use.
In some sense, as programmers we write programs that take data and turn it
into useful information.
For example, we might take 100 years worth of daily high temperatures for each
day of the year, and determine the monthly average temperature. It is hard
to judge anything by the 36,500 data points, but the 12 numbers we would
return are useful.
What is a programming Language?
Deep inside of the computer, everything is represented in binary
or base 2.
- We normally work in decimal or base 10.
- Base 10 requires 10 different digits to represent numbers.
- Base two or binary requires only two digits.
- A single binary digit is called a bit
- Groups of 8 bits are called a byte
- This is easier to deal with electronically (on or off).
- It is also easier to build hardware to perform the basic
operations such as addition and multiplication.
Each computer has, like your vcr, a set of instructions that it can follow.
These instructions are just binary numbers.
For Example:
100011 01001 01000 0000 0100 1011 0000
000000 10010 01000 0100 0000 0010 0000
101011 01001 01000 0000 0100 1011 0000
are the machine instructions to add two numbers on a MIPS machine.
With the first electronic computers (ca 1950), programmers wrote this code
and inserted it into the computer via toggle switches.
This is called Machine Language.
This was:
- Very difficult to do.
- Very difficult to debug.
- Very difficult to understand.
- Done because there were many programmers and few machines.
- Not portable, machine language on one family of computers is
different from machine language on another family of computers.
What does it mean when two computers are binary compatible?
It is easier to remember words than numbers so someone came up with the
idea of using words to represent the numbers.
Example:
lw $t0, 1200($t1)
add $t0, $s2, $t0
sw $t0, 1200($t1)
The above example in MIPS assembly language
This is called assembly language, and a program that translates assembly
language into machine language is called an assembler
Assembly language is
- somewhat easier to write.
- somewhat easier to understand.
- somewhat easier to debug.
- still difacult to write, debug and understand.
(Especially in the large)
- not portable between machine families.
- almost the same as machine language line for line.
There are problems with assembly language, especially when you are writing
large programs or programs that you want to move between machines. The next
step in language development was high level languages.
High level languages use either a compiler or an interpreter to
turn them into machine language.
- A compiler translates the entire program into machine language
and produces an executable, a compiled program that can
be run on a computer.
- The program in the high level language is called source code.
- The program in assembly language is called object code.
- Think of translating a book.
- An interpreter translates the program one line at a time and executes
each line after it has been translated.
- Think of interpreting into American Sign Language.
This is a simplified view of this process, but it will do for now.
High level languages tend to be:
- easier to understand. (than assembly or machine language)
- portable. (after the compiler or interpreter has been written for
the target machine)
- Easier to write and debug code in. (If it is written properly)
- More natural.
Some high level languages are:
- FORTRAN - Formula Tranlator - a language for scientific
programming
- COBOL - Common Business Oriented Language -
an attempt to program in English.
- lisp - a recursive language
- BASIC - Beginners All Purpose Simbolic
Instruction Code - a FORTRAN training language
- B - a short lived language
- PASCAL - a language to teach programming with.
- C - the language that followed B.
- MODULA-2 - an improvement on PASCAL.
- C++ - the language that followed C.
- JAVA - both compiled and interpreted. A language for the Internet.
- AND MANY others.
In this class we will be learning C++.
What can a programming language do?
The operations a computer can perform:
- Store data at a location.
- Move data from one location to another.
- Get data from an input device (keyboard, mouse ...)
- Send data to an output device (screen, printer ...)
- Store and retrieve data in long term memory (disk, tape ...)
- Compare values and take a different action based on the results.
- Perform arithmetic operations (add, multiply ...)
A programming language gives you access to these operations.
The most simple construct in a programming language is a sequence of instructions
Example: Fahrenheit to Celsius conversion:
read temperature in Fahrenheit
subtract 32 from that number
divide the result by 9
multiply the result by 5
write the result as temperature in Celsius
Languages also support decision making (in a simple form)
Example: Military time to AM/PM time
read in the military time
is the time greater than 1259
newtime is oldtime minus 1200
otherwise
newtime is oldtime
is the military time greater than 1200 but less than 2400?
ending is PM
otherwise
ending is AM
Languages also support looping, or repeating tasks:
Example: Find the average of 10 numbers
set total to 0
set numberofnumbers to 0
top: read in a number
add the number to total
add one to numberofnumbers
is numberofnumbers less than 10?
goto top
divide total by 10
write the result