Computer fundamentals Computer fundamentals Planning the computer Program

  • Slides: 45
Download presentation
Computer fundamentals Computer fundamentals Planning the computer Program

Computer fundamentals Computer fundamentals Planning the computer Program

How does a programmer plan a computer program? Computer fundamentals Computer fundamentals • The

How does a programmer plan a computer program? Computer fundamentals Computer fundamentals • The programming process begins with a problem statement that helps you clearly define the purpose of a computer program 12/21/2021 2

What is a problem statement? Computer fundamentals Computer fundamentals • Defines certain elements that

What is a problem statement? Computer fundamentals Computer fundamentals • Defines certain elements that must be manipulated to achieve a result or goal – Specifies any assumptions that define the scope of the problem – Clearly specifies the known information – Specifies when the problem has been solved 12/21/2021 3

What is an assumption? Computer fundamentals Computer fundamentals • Something that you accept as

What is an assumption? Computer fundamentals Computer fundamentals • Something that you accept as true in order to proceed with program planning 12/21/2021 4

Algorithm Computer fundamentals Computer fundamentals • What is an Algorithm ? • A set

Algorithm Computer fundamentals Computer fundamentals • What is an Algorithm ? • A set of steps for carrying out a task • Carefully following the steps of a correctly formulated algorithm guarantees that you can accomplish the task for which the algorithm was designed – It is step by step description of how to arrive at the solution of the given problem. – It may be formally defined as a sequence of instructions. 12/21/2021 5

How do I write an algorithm? Computer fundamentals Computer fundamentals • Written in a

How do I write an algorithm? Computer fundamentals Computer fundamentals • Written in a format that is not specific to a particular programming language • This approach allows the programmer to focus on correctly formulating the algorithm, without being distracted by the detailed syntax of a computer programming language 12/21/2021 6

How do I figure out an algorithm? Computer fundamentals Computer fundamentals 12/21/2021 7

How do I figure out an algorithm? Computer fundamentals Computer fundamentals 12/21/2021 7

Computer fundamentals Computer fundamentals • A sequence of instructions must process the following characteristics:

Computer fundamentals Computer fundamentals • A sequence of instructions must process the following characteristics: • Each and every instruction should be precise and unambiguous. • Each instructions should be such that it can performed in a finite time. • One or more instructions should not be repeated infinitely. This ensure that the algorithm will ultimately terminate. • After performing the instructions, that is after the algorithm terminates, the desired results must be obtained. 12/21/2021 8

Sample Algorithms Computer fundamentals Computer fundamentals • Example : 50 students in a class

Sample Algorithms Computer fundamentals Computer fundamentals • Example : 50 students in a class appeared in the final examination. Their mark sheets have been given to you. The division column of the mark sheet contains the division (First, Second, Third or Fail) obtained by the student. Write an algorithm to calculate and print the total number of students who passed in First division. 12/21/2021 9

Solution Computer fundamentals Computer fundamentals Step 1: Initialize Total_First_Division and Total_Marksheets_Checked to zero Step

Solution Computer fundamentals Computer fundamentals Step 1: Initialize Total_First_Division and Total_Marksheets_Checked to zero Step 2: Take the mark sheet of the next student Step 3: Check the division column of the mark sheet to see if it is FIRST. If no, go to Step 5 Step 4: Add 1 to Total_First_Division Step 5: Add 1 to Total_Marksheets_Checked Step 6 : Is Total_Marksheets_Checked=50? It no, go to Step 2. Step 7: Print Total_First_Divisdion Step 8 : Stop 12/21/2021 10

What’s the best way to express an algorithm? Computer fundamentals Computer fundamentals • There

What’s the best way to express an algorithm? Computer fundamentals Computer fundamentals • There are various ways in which an algorithm can be represented. Programmers normally use one or more of the following ways to represent their algorithms: – As programs – As flowcharts – As pseudocodes 12/21/2021 11

Flowcharts Computer fundamentals Computer fundamentals • What is Flowcharts ? – A flowchart is

Flowcharts Computer fundamentals Computer fundamentals • What is Flowcharts ? – A flowchart is a pictorical representation of an algorithm. – It is often used by programmers as a program-planning tool for organizing a sequence of steps necessary to solve a problem by a computer. – It uses boxes of different shapes to denote different types of instructions. 12/21/2021 12

Computer fundamentals Computer fundamentals 12/21/2021 13

Computer fundamentals Computer fundamentals 12/21/2021 13

Basic Flowchart Symbols Computer fundamentals Computer fundamentals Terminal Decision 12/21/2021 Input/Output Flow lines Processing

Basic Flowchart Symbols Computer fundamentals Computer fundamentals Terminal Decision 12/21/2021 Input/Output Flow lines Processing Connectors 14

Terminal Computer fundamentals Computer fundamentals • The terminal symbol is used to indicate the

Terminal Computer fundamentals Computer fundamentals • The terminal symbol is used to indicate the beginning (start ), end ( stop), and pauses ( Half) in the program logic flow. • • It is the first symbol in the program logic. In addition , if the program logic calls for a pause in the program , the pause is also indicated with a terminal symbol. • A pause is normally used in the program logic under some error conditions, or if forms had to be changed in the computer’s line printer during the processing of that program. 12/21/2021 15

Input/Output Computer fundamentals Computer fundamentals • The input / output symbol is used to

Input/Output Computer fundamentals Computer fundamentals • The input / output symbol is used to denote any function of an input / output device in the program. • If there is a program instruction to input data from a disk, tape , terminal, or any other type of input device , that step will be indicated in the flowchart with an input / output symbol similarly, all output instruction whether it is output on a printer, magnetic tape, magnetic disk, terminal screen, or any output device, are indicated in the flowchart with an input / output symbol. 12/21/2021 16

Processing Computer fundamentals Computer fundamentals • A processing symbol is used in a flowchart

Processing Computer fundamentals Computer fundamentals • A processing symbol is used in a flowchart to represent arithmetic and data movement instruction. • Hence, all arithmetic processes of adding subtracting , multiplying and dividing are shown by a processing symbol. . • The logical process of moving data from one location of the main memory to another is also denoted by this symbol. When more than one arithmetic and data movement instructions are to be executed consecutively, they are normally placed in the same processing box and they are assumed to be executed in the order of their appearance. 12/21/2021 17

Decision Computer fundamentals Computer fundamentals • • The decision symbol is used in a

Decision Computer fundamentals Computer fundamentals • • The decision symbol is used in a flowchart to indicate a point at which a decision has to be made, and a branch to one of two or more alternative points is possible. Figure 11. 2 (see book) shows three different ways in which a decision symbol can be used. It may be noted from these examples that the criterion for making the decision should be indicated clearly within the decision box. • Moreover, the condition upon which each of the possible exit paths will be executed should be unidentified, and all the possible paths should be accounted for. • During execution, the appropriate path is followed depending upon the result of the decision. 12/21/2021 No If 1 = 10? Yes 18

Flow Lines Computer fundamentals Computer fundamentals • Flow lines with arrowheads are used to

Flow Lines Computer fundamentals Computer fundamentals • Flow lines with arrowheads are used to indicated the flow of operation, that is, the exact sequence in which the instructions are to be executed. • The normal flow of flowchart is from top to bottom and left to right. • Arrowheads are required only when the normal top to flow is not to be followed. However, as a good practice, and to avoid ambiguity, flow lines are usually drawn with an arrowhead at the point of entry to a symbol. • Good practice also dictates that flow lines should not cross each other, and that such intersections should be avoided whenever possible. 12/21/2021 19

Connectors Computer fundamentals Computer fundamentals • Whenever a flowchart becomes complex enough that the

Connectors Computer fundamentals Computer fundamentals • Whenever a flowchart becomes complex enough that the number and direction of flow lines is confusing , or it spreads over more than one page, it is useful to utilize the connector symbol as a substitute for flow lines. • This symbol represents an entry from, or an exit to another part of the flowchart. • A connector symbol is represented by a circle, and a letter or digit is placed within the circle to indicate the link. 12/21/2021 20

Why use Flowcharts ? Computer fundamentals Computer fundamentals • The flowchart is then expressed

Why use Flowcharts ? Computer fundamentals Computer fundamentals • The flowchart is then expressed in some programming language to prepare a computer program. • The main advantages of this two-steps approach in program writing is that, while drawing a flowchart, a programmers is not connected with the details of the elements of programming language. • It is a good practice to have a flowchart along with a computer program, because a flowchart often serves as a document for the computer program, and is very useful during the testing of the program, and while incorporating further modifications in the program. 12/21/2021 21

Sample Flowcharts Computer fundamentals Computer fundamentals • Example 1: A student appears in an

Sample Flowcharts Computer fundamentals Computer fundamentals • Example 1: A student appears in an examination, which consists of total 10 subjects, each subjects having maximums marks of 100. The roll number of the student, his/her name, and the marks obtained by him/her in various subjects, are supplied as input data. Such a collection of related data items, which is treated as a unit, is known as a record. Draw a flowchart for the algorithm to calculate the percentage marks obtained by the student in this examination, and to print it along with his/her roll number and name. • Example 2: 50 students appears in an examination of example 1. Draw a flowchart for the algorithm to calculate the percentage marks obtained by each student in this examination, and to print it along with his/her roll number and name. 12/21/2021 22

Flowchart Start Computer fundamentals Computer fundamentals Start Count = 0 Read input data Add

Flowchart Start Computer fundamentals Computer fundamentals Start Count = 0 Read input data Add marks of all Subjects Giving total Percentage = Total/10 Write output data Add 1 to Count Stop 12/21/2021 yes no Is count = 50? Stop 23

Flowcharting Rules Computer fundamentals Computer fundamentals • Some of the rules and guide lines

Flowcharting Rules Computer fundamentals Computer fundamentals • Some of the rules and guide lines are as follows: – First chart the main line of logic, then incorporate detail. – Maintain a consistent level of detail for a given flowchart. – Do not chart every details, otherwise, the flowchart. Will only be a graphic representation, step-by-step, of the program. A reader, who is interested in greater detail can refer to the program itself. – Word in the flowchart symbols should be common, which are easy to understand. It is recommended to use descriptive titles written in designer’s own languages, rather than ib machine oriented language. 12/21/2021 24

Advantages and Limitations of flowcharts Computer fundamentals Computer fundamentals • Advantages : – Better

Advantages and Limitations of flowcharts Computer fundamentals Computer fundamentals • Advantages : – Better Communication – Proper Program Documentation – Efficient Coding – Systematic Debugging – Systematic Testing 12/21/2021 25

Limitations Computer fundamentals Computer fundamentals • Flowchart are very time consuming, and laborious to

Limitations Computer fundamentals Computer fundamentals • Flowchart are very time consuming, and laborious to draw with proper symbols and spacing, especially for large complex programs. • Owing to the symbol-string nature of flowcharting, any changes or modifications in the program logic will usually require a commonly new flowchart. • There are two standards determining the amount of detail that should be included in a flowchart. 12/21/2021 26

Pseudocode Computer fundamentals Computer fundamentals • What is pseudocode ? – Pseudocode is another

Pseudocode Computer fundamentals Computer fundamentals • What is pseudocode ? – Pseudocode is another programming analysis tool, which is used for planning program logic. – “Pesudo” means imitation or false, and “code” refers to the instructions written in a programming language. – So it is an imitation of actual computer instructions. 12/21/2021 27

Pseudocode for Basic Logic Structures Computer fundamentals Computer fundamentals • Any program logic, no

Pseudocode for Basic Logic Structures Computer fundamentals Computer fundamentals • Any program logic, no matter how complex, could be expresses by using only the following three simple logic structures : – Sequence Logic – Selection Logic – Iteration (or looping ) Logic 12/21/2021 28

Sequence Logic Computer fundamentals Computer fundamentals • Sequence logic is used for performing instructions

Sequence Logic Computer fundamentals Computer fundamentals • Sequence logic is used for performing instructions one after another in sequence. Process 1 Process 2 Flowchart 12/21/2021 Pseudocode 29

Selection Logic Computer fundamentals Computer fundamentals • Selection logic is used for selecting the

Selection Logic Computer fundamentals Computer fundamentals • Selection logic is used for selecting the proper path, out of two or more alternative paths, in the program logic • It uses three control structures called IF…. . THEN, IF…. . THEN……ELSE and CASE. 12/21/2021 30

Figure : Flowchart and pesudocode for IF…. THEN…. . ELSE selection structure Computer fundamentals

Figure : Flowchart and pesudocode for IF…. THEN…. . ELSE selection structure Computer fundamentals Computer fundamentals IF condition Yes IF (condition) No THEN ELSE THEN Process - 1 12/21/2021 Process 2 ELSE Process – 2 Flowchart ENDIF Pseudocode 31

Iteration (Looping) Logic Computer fundamentals Computer fundamentals • Iteration logic is used for producing

Iteration (Looping) Logic Computer fundamentals Computer fundamentals • Iteration logic is used for producing loops in program logic, when one or more instructions may be executed several times, depending on some condition. • It uses two control structures, called DO…WHILE and REPEAT…. UNTIL. 12/21/2021 32

Iteration (looping) Logic Computer fundamentals Computer fundamentals DO WHILE Condition Process 1 Process n

Iteration (looping) Logic Computer fundamentals Computer fundamentals DO WHILE Condition Process 1 Process n ENDDO 12/21/2021 33

Advantages and Limitations Computer fundamentals Computer fundamentals • Advantages: – Converting a pesudocode to

Advantages and Limitations Computer fundamentals Computer fundamentals • Advantages: – Converting a pesudocode to a programming language is much more easier than converting a flowchart to a programming language – As compared to a flowchart, it is easier to modify the pseudocode of a program logic, when program modifications are necessary. – Writing of pseodocode involves much less time and effort than drawing an equivalent flowchart. Pseudocode is easier to write than an actual programming language, because it has only a few rules to follow, allowing the programmer to concentrate on the logic of the program. 12/21/2021 34

Limitations Computer fundamentals Computer fundamentals • In case of pseudocode, a graphic representation of

Limitations Computer fundamentals Computer fundamentals • In case of pseudocode, a graphic representation of program logic is not available. • There are no standard rules to follow in using pseudocode. Different programmers use their own style of writing pseudocode. Hence, communication problem occurs due to lack of standardization. • For a beginner, it is more difficult to follow the logic of pseudocode, or write pseudocode, as compared of flowcharting. 12/21/2021 35

Program Coding: How do I code a computer program? Computer fundamentals Computer fundamentals •

Program Coding: How do I code a computer program? Computer fundamentals Computer fundamentals • It depends on: – The programming language you use – The programming tools that you selected – The programming paradigm that best fits the problem you’re trying to solve 12/21/2021 36

What is a VDE? Computer fundamentals Computer fundamentals 12/21/2021 37

What is a VDE? Computer fundamentals Computer fundamentals 12/21/2021 37

How does a programmer know if a program works? Computer fundamentals Computer fundamentals •

How does a programmer know if a program works? Computer fundamentals Computer fundamentals • A computer program must be tested to ensure that it works correctly • Testing consists of running the program and entering test data to see whether the program produces the correct results 12/21/2021 38

What can cause program errors? Computer fundamentals Computer fundamentals • A syntax error occurs

What can cause program errors? Computer fundamentals Computer fundamentals • A syntax error occurs when an instruction does not follow the syntax rules, or grammar, of the programming language • A runtime error occurs when the computer can’t execute an instruction • A logic error is an error in the logic or design of a program 12/21/2021 39

How do programmers find errors? Computer fundamentals Computer fundamentals • Proofreading lines of code

How do programmers find errors? Computer fundamentals Computer fundamentals • Proofreading lines of code • Programmers can also use a debugger, which allows programmers to step through a program and monitor the status of variables, input, and output 12/21/2021 40

Can the computer make decisions while it executes a program? Computer fundamentals Computer fundamentals

Can the computer make decisions while it executes a program? Computer fundamentals Computer fundamentals • A selection control structure (decision structure or branch) tells a computer what to do, based on whether a condition is true or false 12/21/2021 41

Is there an alternative to sequential execution? Computer fundamentals Computer fundamentals A selection control

Is there an alternative to sequential execution? Computer fundamentals Computer fundamentals A selection control structure (decision structure or branch) tells a computer what to do, based on whether a condition is true or false 12/21/2021 42

Can a computer automatically repeat a series of instructions? Computer fundamentals Computer fundamentals •

Can a computer automatically repeat a series of instructions? Computer fundamentals Computer fundamentals • A repetition control structure directs the computer to repeat one or more instructions until a certain condition is met 12/21/2021 43

Can a computer automatically repeat a series of instructions? Computer fundamentals Computer fundamentals •

Can a computer automatically repeat a series of instructions? Computer fundamentals Computer fundamentals • A repetition control structure directs the computer to repeat one or more instructions until a certain condition is met 12/21/2021 Page 593 44

Some Examples Computer fundamentals Computer fundamentals 1. S = 1+2+3+4+…+n 2. S = 12+22+32+…+n

Some Examples Computer fundamentals Computer fundamentals 1. S = 1+2+3+4+…+n 2. S = 12+22+32+…+n 2 3. Find even odd numbers 12/21/2021 45