CPPM - 104 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Veer Narmad South Gujarat University, Surat

C.B Patel Computer College,


New Citylight, Surat

Semester - I

104 Computer Programming and Programming Methodology

By:
Bhavsar Jenish

104-CPPM

C.B Patel Computer College

Unit 1: Algorithm and Flowchart


Algorithm
The step-by-step procedure to solve the problem is called Algorithm. These steps also provide the way to solve the problem. It is also known as logic and usually written in simple English language. An Algorithm may be defined as: o A well-defined computational procedure that transforms inputs into outputs, for achieving the desired results. o A set of instructions for solving a problem. An algorithm written for the first time needs reviewing to: o Determine the correctness of various steps o Reduce the number of steps if possible o Increase the speed of solving the problem Features of Algorithms: o It should have fixed number of steps. o Steps are to be simple. o Inputs and outputs are to be clearly defined. o Steps should be correct and accurate.

E.g.: Write an algorithm to add two numbers.


Step-1. Step-2. Step-3. Step-4. Step-5. Start Get the two numbers Add the two numbers Print the sum Stop

E.g.: Write an algorithm to print whether a number is positive or negative.


Step-1. Start Step-2. Accept a number from the user Step-3. Check whether the number is greater than 0 if so then display Positive and go to step 5 else go to step 4 Step-4. Display Negative Step-5. Stop

Flowchart
A flowchart is a graphical representation of the sequence of operations in an information system or program. Program flowcharts show the sequence of instructions in a single program. Flowchart uses boxes of different shapes to denote different types of instructions. The actual instructions are written within these boxes using clear and concise statements. These boxes are connected by solid lines having arrow marks to indicate the flow of operation, that is, the exact sequence in which the instructions are to be executed. Since a flowchart shows the flow of operations in pictorial form, any error in the logic of the procedure can be detected easily. Once the flowchart is ready, the programmer can forget about the logic and can concentrate only on coding the operations in each box of the flowchart in terms of the statements of the programming language. This will normally ensure an error-free program.

Created By: Bhavsar Jenish

104-CPPM Flowchart Symbols 1.

C.B Patel Computer College

Terminal Box: The terminal symbol, as the name implies, is used to indicate the starting (BEGIN), stopping (END), and pause (HALT) in the program logic flow. It is the first symbol and the last symbol in the program logic. Input/ Output Box: 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 an input device or we want to output the results to any output device, are indicated in the flowchart with an input/output symbol. Processing Box: A processing symbol is used in a flowchart to represent arithmetic and data movement instructions. Thus, all arithmetic processes such as adding, subtracting, multiplying and dividing are shown by a processing symbol. Decision Box: 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. During execution, the appropriate path is followed depending upon the result of the decision.

2.

3.

4.

5.

6.

Connector: There are instances when a flowchart becomes too long to fit in a single page and the use of flow lines becomes impossible. Thus, whenever a flowchart 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. Flow Lines: Flow-lines with arrowheads are used to indicate 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.

Rules for Flowcharting: 1. First formulate the main line of logic, and then incorporate the details. 2. Maintain a consistent level of detail for a given flowchart. 3. Do not give every detail on the flowchart. A reader who is interested in greater details can refer to the program itself. 4. Words in the flowchart symbols should be common statements and easy to understand. 5. Be consistent in using names and variables in the flowchart. 6. Go from left to right and top to bottom in constructing the flowchart. 7. Keep the flowchart as simple as possible. The crossing of flow lines should be avoided as far as possible. Flow-lines must be in one-direction only. 8. Properly labelled connectors should be used to link the portions of the flowchart on different pages.

Created By: Bhavsar Jenish

104-CPPM

C.B Patel Computer College

Advantages of Flowcharting: 1. Since a flowchart is a pictorial representation of a program, it is easier for a programmer to understand and explain the logic of the program to some other programmer. 2. It analyses the problem effectively. 3. Flow-chart is a road-map to efficient coding. 4. It assists in reviewing and debugging of a program. 5. It provides effective program documentation. Disadvantages of Flowcharting: 1. Flowcharts are very time-consuming and laborious to draw. 2. Redrawing a flowchart, if there are any changes, is tedious. 3. There are no standards determining the amount of detail that should be included in a flowchart. Flowchart supports the following logic structures: 1. Sequencing: This logical structure is used for performing instructions one after another in sequence. Hence, for this logic instructions are written in the order, or sequence, in which they are to be performed. The flow is from top to bottom. Branching / Selection: It is non-sequential logic which is also known as Decision logic. Statement may or may not be executed based on some condition. The condition is written inside the decision box in the flow chart. If the condition is true a particular path is followed otherwise another path is executed. Iteration/ Looping: When one or more conditions may be executed several times, depending on some condition, we use looping. We can use the same decision box to check the execution times i.e. the number of times the statements are executed. Loops can be fixed or variable.

2.

3.

Programming
Programming is a process that starts with a problem that a program is to solve. The first step in the process is for the programmer to thoroughly understand that problem --- to understand what information will be given to the program or what information the program needs (this is called the input) and what answers (and in what format) the program should produce (this is the output). The programmer then proceeds to design an algorithm to solve the problem and translates that algorithm into a programming language. At this point the programmer types the program into the computer and tests it to see if it works correctly.

Created By: Bhavsar Jenish

104-CPPM

C.B Patel Computer College

Two phases involved in the design of any program: 4. Problem Solving Phase: In the problem-solving phase the following steps are carried out: i. Define the problem ii. Outline the solution iii. Develop the outline into an algorithm iv. Test the algorithm for correctness 5. Implementation Phase: The implementation phase comprises the following steps: i. Code the algorithm using a specific programming language ii. Run the program on the computer iii. Document and maintain the program

Types of Programming Languages:


Computer cannot do anything on its own. It needs programs which are group of instructions, which when executed solves the problem. For this purpose, different languages are developed for performing different types of work on the computer. Basically, languages are divided into two categories according to their interpretation. 1. Low Level Language (Machine Language) 2. Assembly Language 3. High Level Languages 1. Low Level Language (Machine Language): Low level computer languages are machine codes or close to it. Computer cant understand instructions given in high level languages or in English. It can only understand and execute instructions given in the form of machine language i.e. language of 0 and 1. Machine Language is basically the only language which computer can understand. Machine language require detailed knowledge of every aspect of the computer's architecture, including processor register function, I/O details, memory layout and access methods, etc. Machine language is sometimes also referred as the binary language because it has base-2 i-e, the language of 0 and 1. Very few computer programs are actually written in machine language. Advantages: 1. Faster execution 2. Computer easily understands Disadvantages: 1. Because it consists of only 0s and 1s it is very difficult to code and modify. 2. It is machine dependent and not portable. 3. Error solving is also difficult. 2. Assembly Level Language: It is an intermediate language which is written using mnemonics and 1s and 0s. It is a 2nd generation language. Since the computer cannot understand it directly, it needs to be translated into machine-level language. It requires a program called Assembler for converting assembly code to machine code. Advantages: 1. Easier to program as compared to machine level language. 2. Easy to understand 3. Easy to locate and correct errors Disadvantages: 1. It is machine dependant and so not portable. 2. Takes more execution time than machine level language. 3. Compute cannot understand directly it requires translator (Assembler). Created By: Bhavsar Jenish 5

104-CPPM

C.B Patel Computer College

3. High Level Language: High level language programs are written in English language. The programs cannot be executed directly. It needs to be converted to machine level language for execution. It requires a translator called Compiler or Interpreter for converting high-level to machinelevel language. E.g.: BASIC, FORTRAN, PASCAL, C, COBOL, etc. They are also known as Procedure-oriented languages. Programmer needs to specify complete instructions inorder to perform a task. Advantages: 1. Easier to program. 2. Easy to write and understand. 3. Portable i.e. Machine independent. 4. Easy to locate and correct errors Disadvantages: 1. Takes more execution time. 2. Compute cannot understand directly it requires translator (Compiler/Interpreter).

Structured Programming
Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. A defined function or set of similar functions is coded in a separate module or sub-module, which means that code, can be loaded into memory more efficiently and that modules can be reused in other programs. After a module has been tested individually, it is then integrated with other modules into the overall program structure. The reason for the evolution to structured programming is the need for well organized programs that are: (a) Easier to Design (d) Easy to Test and Debug (b) Easy to Read and understand (e) Combine with other programs (c) Easy to Modify (f) More Reliable Examples of Structured Programming Languages: C, Ada and Pascal. The Structure Theorem: It is possible to write any computer program by using only three (3) basic control structures, namely: 1. Sequential 3. Repetition (looping, DoWhile) 2. Selection (if-then-else)

Fourth Generation Language


These are Non-procedure Oriented Languages. It requires outline not detailed instructions to perform a task as compared to a High level language. It is commonly known as 4GL. They are used to work with files and databases.

Compiler
A program which translates a high-level language program into a machine language program is called a compiler. It checks all kinds of limits, ranges, errors etc. But its program execution time is more, and occupies a larger part of the memory. It has low speed and low efficiency of memory utilization. 6

Created By: Bhavsar Jenish

104-CPPM

C.B Patel Computer College

Interpreter
An interpreter is a program which translates one statement of a high-level language program into machine codes and executes it. In this way it proceeds further till all the statements of the program are translated and executed. A compiler is nearly 5 to 25 times faster than an interpreter. An interpreter is a smaller program as compared to the compiler. It occupies less memory space. It can be used in a smaller system which has limited memory space. Compiler Scans the program line by line and translates immediately. Syntax errors for each line are to be solved and then translated. Once all the lines are checked and translated, the program is executed. It is quicker and easier to make changes in the program and thus development time can be reduced. They use less memory space and reliable for smaller systems.

Interpreter Scans the entire program first and then translates the whole program. List of errors which are generated for the whole program are to removed and then the program is executed. Changes in program needs recompilation hence it takes more time. They use more memory space.

Editor An editor is a simple computer program which allows users to create, change, or edit programs. There are four types of editors:
1. Linkage Editor: The linkage editor is a utility program which adapts a program that has just been assembled or compiled into a particular computer environment. 2. Text Editor: A text editor is a software program that creates and manages text files. 3. Line Editor: A line editor allows lines of text to be entered and edited only by entering commands which refer to a particular line or block of text. 4. Screen Editor: A screen editor allows the programmer to position the cursor anywhere on the screen and insert or delete text.

Types of Program Errors


Once a program has been typed in, different types of errors may show up. These include: (a) Syntax/semantic errors (c) Runtime errors (b) Logic errors

1. Syntax/ Semantic Errors: Syntax is a set of rules governing the structure of and relationship between symbols, words and phrases in a language statement. A syntax error occurs when a program cannot understand the command that has been entered. 2. Logic Errors: Logic refers to a sequence of operations performed by a software or hardware. Software logic or program logic is the sequence of instructions in a program. Logic errors are the errors that have been entered in the instructions created because of the mistake made by a programmer. 3. Runtime Errors: Runtime errors occur when a program is run on the computer and the results are not achieved due to some misinterpretation of a particular instruction. This could be something like dividing a number by zero which results in a very large value of quotient. It may also be any other instruction which a computer is not able to understand.

Created By: Bhavsar Jenish

You might also like