Download - gidnepal.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm wikipedia , lookup

Functional programming wikipedia , lookup

Program optimization wikipedia , lookup

Parsing wikipedia , lookup

Programming language wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Reactive programming wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Control flow wikipedia , lookup

Assembly language wikipedia , lookup

Compiler wikipedia , lookup

History of compiler construction wikipedia , lookup

Go (programming language) wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
XI-Computer Science
Programming Concept
Programming Concept
Introduction to Program and Programming Language
The communications between two parties whether they are machine or human beings
always need a common language or terminology. The language used in the communication
of computer instruction is known as the programming language. It is the set of instructions
used to write a program. Though there are higher levels of languages too, the programming
languages are mainly divided into three types. They are
1. Machine Language
2. Assembly Language and
3. High Level Language
Machine Language
The language whose instructions are written in binary is called machine level language. It
is the language of CPU. All instructions and data should be written using binary codes 1
and 0. It is difficult to remember various combination of 1 s’ and 0 s’ are representing
numerous data and instruction. Every types of machine have its own machine language the
user cannot communicate with other computer.
Advantages
i. time taken to execute program is less.
ii. The program written in this language need not to be translated.
Disadvantages
i. It is machine dependent.
ii. The knowledge of internal architecture is essential for program coding.
iii. Program written in machine language is difficult to learn, debug and modify.
Assembly Language
The languages which use mnemonic codes (like HALT, MOV, JMP, ADD, MUL END
etc.) rather than binary codes (1 or 0) are called assembly language. As computer
understands only machine code instruction, a program written in assembly language must
be translated into machine language before the program is executed. This translation is
done by a computer program referred as Assembler.
Advantages
i.. Much easier to understand as compared to machine language
ii. Easier for program debugging.
iii. Easier to modify as compare to machine language.
Disadvantages
i. It is machine dependent.
ii. It requires translator to translate into machine code.
iii.
Program written in assembly language are comparatively larger.
Prepared by: Suresh Khatiwada
1
XI-Computer Science
Programming Concept
High Level Language
The language whose instructions are written in English statements and mathematical
notations is called high level language. These languages consist of words and symbols and
one can write programs using these in conjunction with certain rules like English language.
It is machine independent and a program written in high level language can be run on
computers of different makes with little or no modification. A program written in high
level language is known as the source program and can be run on different machines with
different translators. The translated program is called the object program.
Advantages
i.. Much easier to understand and learn as compared to assembly language
ii. It is machine independent.
iii. Easier for program debugging.
iv. Lower program preparation cost.
Disadvantages
i. It requires more time and memory to execute.
ii.
Sometimes it is less efficient.
Translators for high level languages are:
i) Compiler
ii) Interpreter.
Compiler
A compiler checks the entire user written program code known as the source program and,
if error free produces a complete program in machine language known as object program.
Then the object program is loaded into the computer for execution. E.g. C, C++
Interpreter
Function of Interpreter is same as compiler, however interpreter translate one statement at a
time, where as a compiler translate the whole program at a time. For example, BASIC.
Error correction (debugging) is much simpler in the case of the interpreter because it
translates one statement at a time, so the error can be corrected at the same time. But the
compiler produces an error list for the entire program at the end. On the other hand,
Interpreter takes more time where as compiler takes less time as there are no steps by steps
translation.
The difference between compiler and interpreter is as follows:
Compiler
1. A compiler is a computer program that
translates the program written in high level
language (source code) into machine level
language (object code) at once.
Interpreter
1. An interpreter is a computer program that
translates the program written in high level
language (source code) into machine level
language (object code) line by line. It does
not produce the whole object code.
2. The object code executes faster than 2. The interpreted code executes slower than
interpreted code.
object code.
Prepared by: Suresh Khatiwada
2
XI-Computer Science
Programming Concept
3. If error is discovered in the program, the
whole program has to be recompiled. It is
slow for debugging.
4. Compilers are large program and
complex to write. It require more
memory space.
5. The programming languages that
require compiler are C, C++, JAVA
etc
3. There is no need of recompilation each
time an error is discovered. It is faster for
debugging.
4.Interpreter is simple to write and require
less memory space than compiter.
5.The programming language that require
interpreter are BASIC, LISP etc.
Generation of Programming Languages
1. First Generation Language (1st GL) e.g. Machine Language
2. Second Generation Language (2nd GL) e.g. Assembly Language
3. Third Generation Language (3rd GL) e.g. Procedural Language like C, Pascal,
BASIC etc.
4. Fourth Generation Language (4th GL) e.g. Database oriented Language like oracle,
FoxPro, Access, Sybase, Dbase etc. They are more advanced than high level
language. They are dedicated for particular application development. These
languages are machine independent and can define what processing must be done
on data.
5. Fifth Generation Language (5th GL) Natural Language. It is a language used for
everyday communication. They will be used in coming generation computers to
communicate human with computers. Two popular languages are LISP and Prolog
which are used in AI applications.
System Design Tools
Flowcharts
The pictorial or diagrammatic representation of different control structures is called
Flowchart. It is a tool and technique to find out solution of programming problems through
some special symbols. The flowchart indicates the direction of flow of a process, relevalent
operations and computations, points of decisions and other information which are a part of
solution.
There are various flowchart symbols that carry different messages and are used for
different purposes. Symbols used in Flowcharts are
Name
Oval
Diagram
Prepared by: Suresh Khatiwada
Uses
Start/End program
3
XI-Computer Science
Programming Concept
Parallelogram
Input/Output
Rectangle
Processing
Diamond
Decision
Connector
connects one place to
another.
Directed lines
shows direction
of flows.
Advantages of flowchart
i.
It is easy to maintain relationship since it breaks the problem down into logical
sub-division.
ii.
It is easy to communicate with facts and faster.
iii.
It helps coding to be effective and faster.
iv.
It is easy to make changes to program and it is easy to debug.
Disadvantages of flowchart
i.
It takes time so it is wastage of time and show down the process of software
development.
ii.
It is quite costly to produce and difficult to use and manage.
iii.
It is not a means of communication for user and computer.
Algorithm
An algorithm is a well-defined sequence of steps required to solve a particular problem.
Algorithm is a precise method useable by a computer for the solution of a problem. An
algorithm is composed of finite set of steps each of which may require one or more
operations.
Example:
An algorithm for adding two numbers
Step 1: Start
Step 2: Input any two numbers, say A and B
Step 3: Find the sum using C=A+B
Prepared by: Suresh Khatiwada
4
XI-Computer Science
Programming Concept
Step 4: Print the result
Step 5: End
Characteristics of algorithms
i.
Each and every instructions used in algorithm should be simple and easy to
understand.
ii.
An algorithm should have finite number of steps.
iii.
It should not depend on particular programming language.
iv.
An algorithm should produce correct result.
v.
The processing rules used in algorithm should be executed by the computer.
Pseudo-code (Structured English)
A rough text version of the code, which look similar to programming code but does not
have all precise syntax and details necessary for real code. The simplified text version of
program is called Pseudo-code.
Example:
Input salary
if salary<=10000
tax=0
else
tax=0.1*salary
output tax
Decision Table
A decision table allows us to identify the exact couse of actions for given conditions. It
provides unambiguous decisions leading to a good program design. A decision generally
consists of two parts. They are: i) conditions and ii) actions.
Decision Tree
The tree like presentation of conditions and actions are called decision tree. Every node
denotes condition. It is used for logical variations and problems involving few complex
decisions with complex branching routines.
Control Structure in Program Design
Different control structures are available for designing algorithm. After the algorithm is
designed it is converted into an appropriate programming language. Control structures can
be divided into three categories
1. Sequence
2. Selection
3. Iteration (loops)
Sequence
Set of computer instructions, which executed sequentially one after another. Instructions
are put in predefined order and the next instruction is executed one after execution of
previous instruction.
Prepared by: Suresh Khatiwada
5
XI-Computer Science
Programming Concept
Selection
These are the instructions that are executed conditionally. Each instruction is executed
based on some condition that can be either true or false. If a condition is true it will be
executed one instruction otherwise it will be executed another. There can be various forms
of conditions that are:
1. IF<condition> THEN
false
Execute statement A(no other)
2. IF<condition> THEN
Execute statement A
ELSE
Execute statement B
Statement B
Condition
?
true
true
Statement A
true
Statement A
true
Statement B
If condition1
false
3. IF <condition 1> THEN
Else if
condiotion2
Execute statement A
ELSE IF<condition 2> THEN
Execute statement B
ELSE
Execute statement C
false
Statement C
Prepared by: Suresh Khatiwada
6
XI-Computer Science
Programming Concept
4. Switch statement:
Entry to switch
Select Case
Expression
Case case1
Do operation 1;
Case case2
Do operation 2; Case1
:
:
:
default
Do operation N;
End SelectCase;
Statement 1
Statement 2
Case 2
Case 3
Statement 3
Default
Iteration (loop)
A loop causes a section of a program to be repeatedly executed for a certain number of
times. The repetition continues while the condition set for it remains true. When the
condition becomes false, the loop ends and the control is passed to the statement following
the loop.
Syntax:
While Loop
Syntax
While<condition>
Statement;
Condition?
statement
true
false
Prepared by: Suresh Khatiwada
7
XI-Computer Science
Programming Concept
do….while statement:
syntax:
do{
statement block;
} while(condition);
statement
Condition?
true
false
For loop
For (Initialization; condition; increment/decrement)
statement block;
.
initialize
.
.
Condition?
true
Statements
increment
false
Prepared by: Suresh Khatiwada
8
XI-Computer Science
Programming Concept
Features of Good Program/Desirable program characteristics
Some important characteristics that should be considered during programming are:
1. Integrity
This refers to the accuracy of the calculations. It should be clear that all other program
enhancement would be meaningless if the calculations are not carried out correctly.
Thus, the integrity of the calculation is important in computer program.
2. Clarity
It refers to the overall readability of the program. If a program is clearly written, it
should be possible for another programmer to follow the program logic easily.
3. Simplicity
The clarity and accuracy of a program are usually enhanced by keeping things as
simple as possible. Same problem can be done in two or more ways, but one needs to
choose the simplest way to solve the problem.
4. Efficiency
Efficiency is concerned with execution speed and efficient memory utilization. i.e. it
should run as fast as possible with the minimum memory requirements.
5. Generality
Program should be as general as possible, within reasonable limits.
Programming Techniques (Paradigm)
The main programming paradigm are as follows:
i. Structured Programming
ii. Object Oriented Programming
Structured Programming
Structured programming can be seen as a subset or sub discipline of procedural
programming, one of the major techniques for programming. Procedural programming is
based on the idea of modularity, where the entire program is divided into modules. Again
each module is composed of one or more procedures (also called as functions or
subroutines)
The key principle of structured programming technique is modularity with single entry and
sings exit point. For each procedure, there must be one starting point and one ending point.
Structured programming is often (but not always) associated with a top down approach to
design.
Object Oriented Programming
Object Oriented Programming is based on concept of Object and Class rather than
procedure. This is a programming technique which uses ‘bottom-up’ approach. The
bottom-up design is a way to construct software from small pieces of code to the whole
system. The first step is to indentify individual subtasks which can be used as building
blocks to compose the overall task. Object oriented technique emphasizes the following
aspects:
i. Objects
ii. Class
iii. Abstraction and Encapsulation
Prepared by: Suresh Khatiwada
9
XI-Computer Science
Programming Concept
iv. Polymorphism
v. Inheritance and Reusability
Software System Development Life cycle (SDLC)
The system development method might be different based on size and nature of problems,
but in general following steps are followed in developing a new system:
1. Problem definition
2. System analysis
3. System Design
4. System Testing
5. System Implementation
6. System Evaluation
7. System Maintenance
Problem Definition
This step is intended to discover the requirement i.e. the requirements, which are needed to
be fulfilled by the new system. The process involves the interactions with system
customers and other end-users. This state also determines which part of the system is to be
computerized and which part is not.
System Analysis
The system requirement that obtained in previous phase is analyzed in this phase. This
determines, the proposed system is feasible to implement or not and it also estimates the
tentative cost of the proposed system. This phase use various tools for analyses of the
system such as Data flow diagram, entity relationship diagram etc.
System Design
System design is concerned, how system functionality is to be provided by the different
components of the system. Here each modules are further broken down into smaller pieces
and elaborated minutely with the help of flow-chart or Pseudocode. The designed process
may be top-down or bottom up approach.
System Implementation
After the detailed design of the system, it is ready for implementation. An appropriate
programming language is chosen and coding starts. It involves the conversion of
Pseudocode or flow-chart into the programming language construct.
System Testing/Verification
The individual program units or programs are integrated and tested as a complete system or
ensure that the software requirements have been met. Testing is perform using two ways:
1. Black Box testing: In this technique, a known input is provided and checked
whether system gives expected output or not.
2. White box testing: In this technique, all possible operations are checked
sequentially.
System Maintenance
This is the continuous process and is done until the lifetime of software. System is installed
and put into practical use. Maintenance involves correcting errors, which weren’t
Prepared by: Suresh Khatiwada
10
XI-Computer Science
Programming Concept
discovered in earlier stages of system development, improving the implementation of
system units and enhancing the system’s services as new requirement discovered.
Documentation
Documentation is one of the most vital parts of software development. It helps to those
who use and maintain software so that the software becomes easy to extend, redesign and
debug. It will include a statement and detail of problems such as algorithm, charts,
drawing, coding sheets, technical data and instructions to users. It is the process of
collecting, organizing, storing and maintaining a complete record of programs and other
documents.
System documentation is the documentation of whole system. It includes functional
description, introductory manuals, reference manual, installation manual; user manual etc.
Program documentation includes a phase of program development document. It includes
statements of problem, description of input/output values, description of each module, error
message produced by the program, test data to be used etc.
Program Debugging
Program debugging is the process of finding out and correction of programming error. Any
program cannot be absolutely correct. So, debugging is an important in the programming.
In debugging we find two types of errors
1. Syntactic error.
2. Semantic or logical error.
Syntactic error
It is the error in the syntax of program. These errors result when rules or syntax of the
programming language are not followed in the program. These kinds of errors occur at the
time of program development and easy to debug at the time of compilation. In many cases,
these errors arise due to spelling missing, missing difficult symbols etc.
Logical error
This is the error in the logical sequence of the program or in planning the program’s logic.
Logical errors are complex types of errors in programming. It is very difficult to find the
logical errors. Therefore programmer should think about the program and its required
output getting or not. At the time of compilation, computer does not show any error, but it
gives the wrong output, then we know that an error has been occurred.
Operation, Operand and Operator
An operation is the action on data. For example, the result of the operations 3+2 is 5.
The individual data or variables on which operations are performed are called operands. In
the expression, a+b, the variables a and b are operands.
Operators are symbols, which are used to compute values, test different condition or
assigned value to a variable.
Prepared by: Suresh Khatiwada
11
XI-Computer Science
Programming Concept
Arithmetic operator
Operators
+
*
/
Action
Addition
Subtraction
Multiplication
Division
%
Modulo division X=y%z
Example
X=y+z
X=y-z
X=y*z
X=y/z
Relational Operators
Operator
>
<=
<
<=
==
!=
Operation
Greater than
Greater than or equal
to
Less then
Less than or equal to
Equal to
Not equal to
Example
x>y
x>=y
x<y
x<=y
x==y
x!=y
Logical Operators
Logical operation
&&
||
!
Operation
AND
OR
NOT
Some Data structures in programming language
Array
An Array is a collection of elements of the same type that are referenced by a common
name. Each element of an array can be referred by an array name and its subscript. Array
may be single dimensional, double dimensional or multidimensional. If A is declared as an
array of 5 elements of type integer it looks like:
Data type array_name[index]
i.e. int A[5]
A[1]
A[2]
A[3]
A[4]
A[5]
Almost all programming languages support the array data structure. The above
array can store data of only integer type we cannot put char type data real type or any other
rather tan integer.
Link List
A link list is a data structure composed of elements, containing data and a pointer to the
next element of same type. A list is a chain of elements, where one element points to
another. If the last element in the list points nowhere is called null pointer. If last pointer in
Prepared by: Suresh Khatiwada
12
XI-Computer Science
Programming Concept
the list points to the first element of the list, then the list becomes a circular list. Sometimes
the pointer may be bi-directional called doubly linked list.
Each element contains data and pointer to next element
Tree
Tree is another complex data structure where each element contains a data part and pointers
to another node. In link-list a node can point one or at most two nodes but in tree a node
can point to any number of other nodes. The topmost node in the tree is called root node
and the node that does not point any node is known as leaf node.
If a node in a tree can point at most two other nodes the tree is called binary tree.
root
parent
child
leaf node
Prepared by: Suresh Khatiwada
13