PROGRAMMING: What’s It All About? Hele-Mai Haav: February 2002 PROGRAMMING: What’s It All About? Though, thy beginning was small, yet thy end will be very great. JOB 8:7
INPUT Ingredients: sugar, flour recipe OUTPUT Software Hardware Oven/baker (mixing, pouring, heating etc.) ALGORITHM is the formal written version of recipe Program is precise representations of algorithms written in special computer-readable language like Basic, C, C++, Pascal, Cobol… OUTPUT
Algorithm A formula or set of steps for solving a particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point. Algorithms can be expressed in any language, from natural languages like English or French to programming language like FORTRAN. We use algorithms every day. For example, a recipe for baking a cake is an algorithm. Most programs, with the exception of some artificial intelligence applications, consist of algorithms. Inventing elegant algorithms -- algorithms that are simple and require the fewest steps possible -- is one of the principal challenges in programming.
Summing problem Step 1. make a note of the number 0 Step 2. proceed through the list, adding each employee's salary to the noted number Step 3. having reached the end of the list, produce the noted number as output
Does this algorithm do the job? Text of the algorithm is short and fixed in length. The process it describes and controls depends on the length of the employee list and can be very long.
Does this algorithm do the job? Let us have two companies: with 1 empl. with 1 million empl. They both can use the same algorithm for solving salary summation problem. Only a single noted number is required for both.
What we have? 1. fixed algorithm 2. a variety of possible inputs the precise duration and nature of the process depends on inputs .
Algorithm- precise order of the actions to be performed. The order in which the basic actions are carried out is crucial. The algorithm must contain control instructions to "push" the processor in this or that direction, telling it what to do at each step and when to stop and say "I'm done".
CONTROL STRUCTURES Sequence control - one process occurs immediately after another "do A followed by B" "do A and then B" A B
CONTROL STRUCTURES Selection structure, or if-then-else structure represents conditional program logic "if Q then do A" "if Q then do A otherwise do B" Q is some condition Can be true or false False True Q? A B C
CONTROL STRUCTURES A A A Iteration structure or looping structure describes occurrence of one or more processes one or more times. - Bounded iteration "do A exactly N times" N is a number - Conditional iteration do-while "while Q do A" do-until "repeat A until Q" No >N A Yes True Q? A False True A Q? False
Summing problem Let us assume the following input: 1. list of employees and their salaries 2. total number of the employees in the list, designated by letter N
Summing problem algorithm 1.Make a note of 0 Point to the first salary on the list 2. Do N-1 times the following 2.1. add the salary pointed at to the noted number 2.2. point to the next salary 3.Add the salary pointed at to the noted number 4.Produce the noted number as output
Diagrams for algorithms Flowcharts
Programs are written in some programming languages which allow the use of only special combinations of selected symbols and keywords. "please read the value X from the input" "how about getting me a value of X" INPUT X READ X
A typical programming language includes : rules for writing control structures ways of defining various types of data (numeric, textual, etc) formats for the basic instructions
Summing algorithm for summing the numbers from 1 to N might be written in Basic as follows: REM Summing INPUT N SUM = 0 FOR INDEX = 1 TO N SUM = SUM + INDEX NEXT INDEX PRINT "Sum of the first N digits is"; SUM END RUN
SUM = 0 sets variable SUM to value 0 FOR INDEX = 1 TO N FOR-NEXT loop repeats the loop fixed number of times At the top of the loop index variable INDEX is given the value 1 NEXT INDEX returns control to the top of the loop and variable INDEX is automatically incremented by 1, and looping continues until INDEX value exceeds the final limit N. Then the control goes to the statement following the NEXT statement PRINT "Sum of the first N digits is"; SUM prints the value of the variable SUM