Mod1-Lec2-RJ

Download Report

Transcript Mod1-Lec2-RJ

BIEN 001 Introduction to Biomedical Engineering Methods
Module 1:
Roger H. Johnson,
Introduction to Computers,
Flowcharting and Programming in C
[email protected]
September 5, 2000
Objectives:
• Introduction to Computers
and Computing Environment at Marquette.
• Develop problem-solving strategies through flow charting.
• Introduction to program development environment.
• Introduction to C language programming.
• Develop two real C programs and run them.
Lectures 1-2:
•
•
•
•
•
•
Computing History
Computer Constituents
Evolution of Programming
Essential Programming Steps
Syntax
Develop Two Programs in C
• • Computing History
• Blaise Pascal and the mechanical adder
Pascal was a French genius and mathematician
who died young after religious mania.
In 1642 he invented a mechanical calculator to assist
in the adding of long columns of numbers in his
father’s tax office.
His most important writing is Thoughts on Religion.
• Gottfried Wilhelm Leibnitz’ adder/multiplier
German invention of 1673 (or1694)
which used stepped-gear-wheels.
• Joseph Jacquard and his pasteboard loom cards
1812: Pasteboard card read by the loom.
Hole pattern determined thread combinations.
• Charles Babbage and his analytical engine
1830’s: First the Difference Engine, which would have been
two tons of brass, steel and pewter clockwork.
1840: Then the Analytical Engine which could decide between
two courses of action. Steam-engine-powered, would
read punched cards, compute, request cards with bell.
• Herman Hollerith and the census device
1890: Used punched cards to process census information
and speed tabulation.
• Howard Aikens and the Mark I
1944: Harvard Mark I. Used mechanical counters and
electromagnetic relays to control its operation.
• John Mauchly and John Eckert and the Eniac
1946: Huge vacuum-tube computer; part of the war effort.
Ballistic computations and code-breaking.
• John Backus and FORTRAN
1954: First inovation to translate human-intelligible
language into machine language.
• Kernighan and Ritchie and C
1970’s: Dominant language in scientific programming today.
FORTRAN 77 persists and other language, such as Pascal,
Basic, and Cobol are used in niches.
• Computer Constituents
CPU = central processing unit
= control unit + arithmetic and logical unit
Memory
Input
Output
Memory
Input
Output
CPU
Arithmetic and
logical unit
Control
unit
Control Unit: Controls timing and directs all
computer operations.
Arithmetic Logic Unit: Makes all decisions
and performs calculations.
Memory: Stores instructions and data live
within the computer.
Input: Devices which input information into memory.
Output: Devices which print or display data from
memory.
• Evolution of Programming
Machine language: sequence of zero’s and one’s
= “binary string”.
(refer to handout on binary number system)
Assembly language: uses keywords to stand for
sequences of zero’s and one’s:
“ADD” = 0010001110001111
One keyword is one line of code
is one binary string is one instruction.
Compiler level: (“high-level” resembles English)
e.g. C = A + B
for(i=1;i<5000;i++)
{.....
One line of code can contain several words and
require many machine language instructions.
Object oriented: New “pictorial” method we
won’t get into.
• Essential Programming Steps
1) A clear statement of the problem:
Decide what it is you really want to do. Helps to
decide if computer is really the way to do it.
2) A rough or general solution algorithm:
Write down in brief format the steps required
to solve the problem.
3) A refined algorithm of the solution:
More detailed sequence of steps.
4) A flowchart of the full solution process:
Draw a block diagram or “flow diagram”
of the computer program.
5) Computer language coding + documentation:
A line-by-line written version of computer code.
6) Creation of a computer file of the coding:
Type in the code using a text editor.
7) Compile and run the file:
Type a command which creates the object file
and the machine language version of code.
8) Obtain sufficient output to test the full
solution algorithm for generality.
An algorithm is a description of a solution
method. Synonyms are “procedure”, “method”,
“technique” or “(set of) rule(s)”.
In the programming context,
an algorithm must possess these characteristics:
• It must end after a finite number of steps.
• It must be describable by a finite sequence of
steps or instructions.
• It must be capable of dealing with all members
of a particular class of problem.
A flowchart is a pictorial representation of an
algorithm.
Flowcharts are sometimes called “block diagrams”
or “flow diagrams”.
A flowchart is a schematic of the logic used
in problem solution and, properly used,
should aid in the development
of a sound logical approach.
A completed flowchart should exactly represent
the sequence of steps coded into the program,
and can help avoid a number of common
programming errors.
Flowchart Symbols:
Casanova Flowchart Example:
Meal-ordering Flowchart:
Flowchart for Tuition Computation:
Tuition Computation Flowchart with Nested
Decision Loops:
Tuition Computation Flowchart with Termination:
Tuition Computation with Termination:
Decision with a Three-way Branch:
Balance Computation
with Termination:
Counter for Loop
Termination:
Example used in Lab 1:
C program to solve two linear equations
with two unknowns.
Of the several methods to solve this problem,
we choose to solve for x and y
by the use of determinants.
e.g.
x + 2y = 5
5x - y = 3
5
2
3 1
5  6 11
x


1
1 2
1  10 11
5 1
1 5
5 3
3  25 22
y


2
1 2
1 10 11
5 1
For the general case:
Ax + By = C
Dx + Ey = F
C B
F E
CE  BF
x

A B AE  BD
D E
A C
D F AF  DC
y

A B AE  BD
D E
It must be borne in mind that division by zero
is not allowed. This would happen with parallel lines:
x + 2y = 5
3x + 6y = 10
5
2
10 6 30- 20
x

1 2
0
3 6
1
5
3 10 10- 15
y

1 2
0
3 6
and would also occur with coincident lines:
x + 2y = 5
-2x -4y = -10
5
2
-10 -4 -20 + 20 0
x


1 2
-4 + 4
0
-2 -4
Steps Required in Writing the Program:
Steps 1 through 4 should be done
before you come in to lab:
1) Statement of the problem.
What we just did.
2) General Algorithm:
an overall solution process
which should consist of numbered steps.
3) Refined Algorithm:
expand the general algorithm;
add major variable names or formulas;
add solution detail to the plan.
4) Flow Chart: give complete control process;
supply minor control variables;
use algorithm-defined variables and formulas;
think through “what will happen if?”.
5) Program Coding: uses all of the above;
supplies the input and output
detail and documentation;
provides adequate comments.
Step 2) Example of General Algorithm:
1) Open data file.
2) Repeatedly
3) read coefficients
4) write system
5) calculate determinants
6) test for output case, process and output
7) End.
Example of refined algorithm:
1.1 Query for input filename
1.2 Open input file
2.1 For all data
3.1 Read coefficients A, B, C, D, E, F
3.2 If end of data, go to 6.9.
4.1 Write system: Ax + By = C
Dx + Ey = F
5.1 Den = AE-DB
5.2 Xnum = CE-FB
5.3 Ynum = AF-DC
6.1 If Den !=0, go to 6.5
6.2 If Xnum=0, write “no unique solution”
6.3 If Ynum !=0, write “no solution”
6.4 Go to 3.1
6.5 X = Xnum/Den
6.6 Y = Ynum/Den
6.7 Write “‘x =‘ X; ‘y=‘ Y.”
6.8 Go to 3.1
6.9 End.