Download presentation
Presentation is loading. Please wait.
Published byBrett Simon Modified over 8 years ago
1
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC LECTURE ONE
2
Computers and Flowcharts Logic is the study of various principles or tools of correct reasoning and of the application of those tools in solving problems. Logical tools : Flowcharts, Warnier diagrams, Nassi-Shneiderman diagrams and Pseudocode. These tools will be used to analyze various types of programming problems and to design solutions for them.
3
Understanding the Programming Process programming steps: 1. Understand the problem. 2. Plan the logic. 3. Code the program. 4. Translate the program into machine language. 5. Test the program. 6. Put the program into production.
4
Programmer –Write programs to satisfy others needs –Provide services to users First understand what users want Well designed program –Steps might involve a short meeting Poorly designed program –Steps might take many meeting hours Thoroughly understanding problem –A most difficult aspect of programming 1. Understanding the Problem
5
Programmer –Envisions objects needed –Writes programming language instructions to create objects Distinct steps –Data modeling Identify all manipulated objects and relations –Create class General object category –Establish communication means Between objects, user 2. Envisioning the Objects
6
Heart of programming process Programmer –Plans program steps –Decides steps to include and order Leads from available input to desired output –Plans the solution: logic tools Flowcharts and pseudocode Both use English-like code –Language syntax not a concern 3. Planning the Logic
7
Write class definitions –Descriptions data in each class –Operations performed with data in each class Object-oriented languages –C++, C#, Java, Visual Basic, COBOL, Simula –Similarity All create objects and establishing communication 4. Coding the Program
8
4. Coding the Program (cont’d.) Program object and logic needed to work with the objects –Executed using any number of languages Syntax (correct spelling and punctuation ) –Concern once language chosen Experienced programmers –Combine planning and program coding in one step
9
5. Translating the Program Code Many programming languages Computer knows one language –Its machine language consisting of many 1s and 0s Translator programs (compilers or interpreters) –Change English-like high-level programming into low-level machine language Executable program Syntax error –Issues when translator cannot translate the code –Must be corrected to allow compilation to complete
10
6. Testing the Program Syntax error free program –Not necessarily free of logical errors –Must test for logical errors Requires entering of sample data –Select test data carefully –Error discovery may require changes To program logic To actual object design Process resembles syntax error correction cycle
11
7. Putting the Program into Production Program adequately tested –Ready for organizational use Putting program into production –Time to implement Can be short or long Depends on program nature, size and user needs –Factors Training, data format changes, new platform causing users to make choices in unanticipated combinations
12
8. Maintaining the Program Maintenance –Process of making required changes Reasons –New legislated tax rates –Input file format alterations –End user requires additional information Not included in original output specifications
13
An algorithm is a set of rules or instructions for doing a task or solving a problem. Algorithm is a step-by-step series of instructions wherein each successive step is determined by the outcome of previous steps. A computer program is simply an algorithm for a computer that is written in a programming language. Algorithm
14
The first algorithm for reviewing a sales receipt book, totaling the sales by using a calculator, and listing customers in three columns. Example of Algorithm
15
The following algorithm describes method of completing this task: 1.Clear the calculator. 2.Read a receipt. 3.If the receipt is blank, write down the total on the calculator, and stop; otherwise continue. 4.Add the amount of the sale to the total on the calculator. 5.If the sale was greater than $1000, enter the buyer's name in column A, and return to step (2); otherwise, continue. 6.If the sale was greater than $100, enter the buyer's name in column B, and return to step (2); otherwise, continue. 7.Enter the buyer's name in column C, and return to step (2).
16
Flowcharts. In the following Figure shows the Flowchart prepared for the "sales report" algorithm In computer programming, algorithms are often designed through the use of special diagrams called flowcharts.
18
Flowcharts are graphic representations of algorithms ( logical program steps) - Program steps in boxes Connected with flowlines (arrows) Shows processing order In computer programming, Flowcharts form an intermediate step between human understanding of the task to be accomplished and the coded program that directs a computer to complete the various steps of the task.
19
Some symbols used in flowcharts 1- Terminal symbol indicates the beginning and the end of processing.
20
2- I/O symbol indicates that the computer is to obtain new data or to record the results of computations. Some symbols used in flowcharts
21
3- Decision symbol represents a branch in the flow of a program Some symbols used in flowcharts
22
4- Process symbol contains instructions that are not represented by other symbols. i.e. it contains calculations, storage of data. Some symbols used in flowcharts
23
5- Flow direction lines indicate the flow of the program. Arrowheads are drawn where flow direction lines join and where flow direction lines meet other symbols. Some symbols used in flowcharts
24
6- comment or annotation symbol is used to annotate other entries. dashed line indicates the position of the comment. Some symbols used in flowcharts
25
7- connector is used to join areas of a flowchart. Some symbols used in flowcharts
26
8- Striped symbol is used to represent a predefined process (or series of operations) when the flowcharts that define the process are included with the current set of flowcharts. Some symbols used in flowcharts
27
9- Predefined process symbol is used to represent a predefined process when the flowcharts that define the process are not included with the current set of flowcharts. Some symbols used in flowcharts
28
10- Off-page connector is used to join areas of a flowchart when the points joined occur on different pages. Some symbols used in flowcharts
29
Any program using this flowchart would: Start. Read some information. Make a decision. On the basis of that decision, carry out one process or the other. Print a result in either case. Stop.
30
Figure shows symbols and the flow direction lines
31
Figure shows a more complex flowchart
32
Figure shows the Flowchart for what will happen to the values of X and Y as the program runs YX 11 22 31 42 1 2
33
YYX 11 222 332 31 32 442
34
Solve Exercises: Exercises: 1 Examine the following flowchart, fill in the table of values, and answer the questions. a. What numbers will be printed? b. What values will X and Y have when the program ends?
35
Exercises: 2- Examine the following flowchart, and fill in the table values. What will the values of X, Y, and Z be when this program ends?
36
Computers, Memory, and Input/Output Computers can be viewed as consisting of three types of components: input devices; output devices; and the central processing unit, or CPU.
37
Computer Memory It is convenient to picture a computer's memory as consisting of a large number of locations in which data can be stored for example: instruction NUMBER = 2 directs the computer to store the value 2 at the location that it is calling NUMBER. 10.0046.382 NUMBER = 2 PROFITCOSTNUMBER COMPUTER STORAGE INSTRUCTION
38
Instruction ADD 1 TO NUMBER 10.0046.382 3 ADD 1 TO NUMBER PROFITCOSTNUMBER COMPUTER STORAGE INSTRUCTION Instruction NUMBER = COST 10.0046.383 46.38 NUMBER = COST PROFITCOSTNUMBER COMPUTER STORAGE INSTRUCTION
39
Instruction NUMBER = COST + PROFIT 10.0046.3856.38 NUMBER = COST + PROFIT PROFITCOSTNUMBER COMPUTER STORAGEINSTRUCTION If we want to store the word yes at a location that we were calling FLAG, we would use the instruction FLAG = "YES". YESFLAG = "YES" FLAGINSTRUCTION
40
Figure describes a portion of a computer's memory before and after a series of instructions is executed. Assume that data in the "before" table resulted from previous instructions.
42
Input/Output Most computer operations require data to be entered and results to be printed, displayed, or recorded. A single input instruction (READ) obtains all the appropriate data from a single record. CUSTOMER FILE CUSTOMER RECORD47621 MAINJOE SMITH 26418 SOUTHTOM ADAMS 44149 CRESTMARK BARNS 3731 6 EASTMARY THOMAS 52947 ADAFRED JONES 47621 MAINJOE SMITH EOF MARKER/* AGE FIELD ADDRESS FIELD NAME FIELD
43
Instruction READ NAME, ADDRESS, AGE directs the computer to store data from three fields of the next record at three memory locations called NAME, ADDRESS, and AGE Once data is stored in memory, it remains until the instruction to read is executed again.
44
Place the EOF decision AFTER the corresponding READ. Tests for EOF BEFORE executing the READ instruction.
45
- To print a line on a report, we will use the instruction PRINT. - The PRINT instruction directs the computer to print information stored at one or more of its memory locations.
46
In addition to data stored in memory, PRINT instructions may be used to print literals. Line PrintedInstruction JOE SMITH NAME NAME JOE SMITH NAME JOE SMITH AGE 47 MY NAME IS JOE SMITH JOE SMITH IS 47. PRINT NAME PRINT "NAME" PRINT "NAME", NAME PRINT "NAME", NAME, "AGE", AGE PRINT "MY NAME IS", NAME PRINT NAME, "IS", AGE, "."
47
Figure describes a program that will read records from the customer file and print a report containing the information from each record.
48
Note: Files must be opened and closed. Because OPEN and CLOSE instructions are not universally required—and especially because they pose no interesting problems in the logic of program design
49
oop I. Loop Loop is a circular logical structure. It causes a series of steps to be continuously repeated until a decision directs program flow to some other part of the program. Loops are diagrammed in either of two ways: 1.By an upward flow direction line. 2.By a connector that returns program flow to a previous point on the chart. Routine Components in Programs
51
EXAMPLE of single loop containing a variety of symbols
52
Nested loops
53
II. COUNTER count of the number of times a process within a loop has been completed. A counter consists of two processes that manipulate a variable: -initializes the variable - The second adds/subtract a constant to (or increments/decrement) the variable each time the loop is completed. Routine Components in Programs
54
Example of flowchart contains a counter
55
The flowchart illustrates the error of initializing the variable within the loop itself.
56
The flowchart prints a report of records in a file called "SALES FILE."
57
III. An accumulator is a structure that maintains a current total without affecting ongoing calculations or processing. - Accumulators are similar to counters in that both consist of a process that sets a variable equal to an initial value and another process that, increments that variable. - Accumulators differ from counters in that they are incremented by variable amounts rather than by constants. Routine Components in Programs
58
The flowchart represents a simple program that contains an accumulator called TOTAL. In this flowchart, the profit for each item is accumulated in TOTAL. When EOF is reached, this total is printed.
59
Flowchart (uses both a counter and an accumulator) represents a program that will read a series of sales and calculate and print their average.
60
IV. indicators (Switches or Flags) Indicators, often called switches or flags, are used to record some event or circumstance within a program. An indicator consists of two elements: -A process that initializes a variable (sets that variable at a predetermined value). -A process that stores a different predetermined value as the value of that variable when the particular event occurs. Routine Components in Programs
61
A message would be printed at the end of the program to indicate whether or not any of the profits exceeded 1500.
62
Exercises 1.4 - 1.5
63
Every flowchart must 1.Start somewhere. 2.Stop somewhere. 3.Unfailingly reach the stop. Universal Requirements for Flowcharts
64
This flowchart illustrates two common errors in flowcharting.
65
This flowchart contains an endless loop that results from its content
66
Error Messages When the data used does not meet the programmer's expectations, the program may not run. Or, if it runs, it produces output that suggests a programming error. EXAMPLE Figure will read a name and a number of hours worked from each record. It will also print the name and gross pay and will print an error message if the number of hours worked is less than zero or greater than 60.
68
zero, positive and Negative The number zero presents programming problems when it is overlooked. FIGURE Counts positive and negative temperatures without counting zero temperatures.
70
Program Design Programs of this sort can be analyzed into three parts: initial process, detail loop, and final process. OPEN FILES PRINT HEADINGS (TITLES AND DATES) INITIALIZE COUNTERS INITIALIZE ACCUMULATORS INITIALIZE INDICATORS INITIAL PROCESS READ AND LOOP EXIT INCREMENT COUNTERS INCREMENT ACCUMULATORS PERFORM CALCULATIONS PRINT DETAIL LINES (INDIVIDUAL NAMES, ETC.) DETAIL LOOP PERFORM FINAL CALCULATIONS PR I NT TOTALS CLOSE FILES FINAL PROCESS
71
EXAMPLE Design a flowchart for a program that will Read names and amounts. Print each name and amount. Print a total of the amounts and the number of records.
73
Exercises 1.6 - 1.9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.