CH 1
CH 1
Sujan Karki
Email: [email protected]
Contact No.: 9819387234
Master’s in Information System Engineering (MscIne) *Ongoing
Bachelor in Computer Engineering – Purwanchal Campus,IOE
UNIT 1
Introduction to Computer
Programming
1. Definition of a program and programming language
2. Types and Generations of Programming Languages
3. Problem-Solving using a Computer
1.3.1 Problem Analysis
1.3.2 Algorithm and Flowchart
1.3.3 Programming
1.3.4 Compilation, Linking and Execution
1.3.5 Debugging and Testing
2 1.3.6 Documentation
Program and Programming language
What is Program ?
o A program is a set of instructions or commands that is arranged in a sequence to
guide a computer to find solution for the given problem. A Program is like a recipe.
It contains a list of ingredients (called variables) and a list of direction (called
statements) that tell the computer what to do with the variables. It is the basic unit of
a software.
o Without programs, computers are useless and can do nothing.
3
Types of Programming Languages
4
Types of Programming Languages
o Low-level language:
- A low-level language is a programming language much closer to the
hardware.
- It requires a thorough knowledge (low level) of the hardware for which the
program is being created.
- Program written for one type of machine may not run (in fact does not run)
on other machines of different manufacture.
These can be divided into two types –
Machine language : Machine language is the lowest level programming
language, which is the only language understood by a computer. While
easily understood by computers, machine languages are almost impossible
for humans to use because they consist entirely of numbers. Early computers
were usually programmed using machine language.
Assembly language: As the machine language involves only numbers
specific to a machine, developers thought that if they can represent those
numbers by some symbols, it would be easier to make programs. So, they
developed assembly languages in which those numbers are replaced by some
5
symbols (called mnemonic).
Types of Programming Languages
o High-level language:
- A high level language is a programming language that enables a
programmer to write programs more or less independent of a particular type
of computer. Such languages are considered high-level because they are
closer to human languages Every high level language has its own set of rules
to represent instructions. This rule is called syntax.
- The main advantage of high-level languages over low-level languages is
that they are easier to read, write, and maintain.
- Like assembly language programs, programs written in a high-level
language also need to be translated into machine language. This can be
done in two ways – by a compiler or interpreter.
6
Generations of Programming Languages
7
o Procedure, Object and Problem Oriented Language.
- With procedure oriented , a program is broken up into parts and each
parts can also be broken into further parts. These parts are procedures.
They are separate but work together. E.g. C, COBOL, FORTRAN etc.
- New and most powerful concept that uses objects – which are instances
of classes – for organizing code. Its principles are inheritance,
Polymorphism, abstraction etc. e.g. C++, JAVA etc.
o Natural Language:
- Language based on AI.
- It uses the concept that rather than solving a problem algorithmically,
an application can be built to solve it, i.e. we make computers learn to
solve any problem.
- Examples of fifth-generation languages include Mercury, OPS5, and
8 Prolog.
…
Compiler:
– A compiler is a program that translates program (called source code) written
in some high level language into object code all at once.
- Compiler translates high-level instructions directly into machine language
and this process is called compiling.
Interpreter:
- A interpreter is a program that translates program (called source code)
written in some high level language into object code line by line.
Assembler:
- A assembler is a program that translates program (called source code)
written in some high level language into assembly language.
Object code: Object code is the code produced by a compiler. Object code is
9
often the same as or similar to a computer’s machine language.
Problem-Solving using a Computer
o In simple language Problem Solving is a systematic approach to find and
implement the solution to a problem.
o The activities we have to plan in a sequence so that we can solve the
problem and get fruitful outcome are:
Problem Analysis
Algorithm and Flowchart
Programming / Coding
Compilation, Linking and Execution
Debugging and Testing
Documentation
1
0
1. Problem analysis: It is the process of decomposing whole parts of
problem into smaller parts or modules. Then identify possible inputs,
processes and outputs with problem.
2. Algorithm: step by step procedure of solving a problem .
3. Flowcharts: It is the graphical representation of the algorithm.
4. Coding: Writing instructions in a particular programming language
to solve a problem.
5. Compilation, Linking and Execution: The compiler will convert
the program code to the machine language which the computer can
understand.
6. Debugging and Testing: After writing a program, programmer
needs to test the program for completeness, correctness, reliability
and maintainability. There are different types of tests: Unit testing,
Program Testing, Verification Testing, Validation Testing etc.
7. Documentation: All the above process should be properly
documented for future reference of both programmer and reader
11
Algorithm
An algorithm is a step by step descriptions of the procedure written in
human understandable language for solving given problem.
The characteristics of an good algorithm are:
12
Algorithm
Algorithm of the sum of two numbers:
Step 1: Start.
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum. sum =
num1+ num2
Step 5: Display the sum of two number, sum.
Step 6: Stop.
1
4
Algorithm to find the sum of n natural numbers:
Step 1: start
Step 2: Declare variables n, sum, counter.
Step 3: Initialize sum=0 and counter= 1
Step 4: Input the value of n form user, n.
Step 5: Calculate the value of sum, sum = sum + counter
Step 6: Increase the value of counter, counter = counter +1
Step 7: If counter <= n then, go to step 5
Step 8: Display the value of sum, sum.
Step 9 : stop.
Advantages :
- The flowchart shows the logic of a problem displayed in pictorial fashion.
- It is useful for debugging and testing of programs.
- Program could be coded efficiently using flowcharts.
- The Flowchart is good means of communication to other users.
Disadvantages:
- It is not useful to represent complex program logic
- For any alterations, the flowcharts have to be redrawn completely.
1
6
Flowchart
17
Flowchart
Rules for writing flowcharts :
- The flow chart should be clear, neat and easy to follow.
- It should be drawn from top to bottom.
- A flowchart always begins with start symbol and ends with stop symbol.
- Flow lines are used to join the symbols
- Decision box should have one entry point and two exit points.
- For lengthy flowcharts, connectors are used to join them.
- Off page connectors are referenced using alphabets.
- On page connectors are referenced using numbers.
- Flow lines cannot cross each others.
1
8
Flowchart
1
9
Flowchart
2
0
Flowchart
Flowchart to find out whether a given integer is zero, positive,
or negative
21
Flowchart
Flowchart to find out the greatest numbers among three
numbers.
2
2
Flowchart
Flowchart to check whether a given number is odd or even.
2
3
Algorithm and Flowchart for finding the roots of quadratic
equation.
2
4
Algorithm and Flowchart for finding the roots of quadratic
equation.
Step 1: start
Step 2: Declare the required variables, a, b, c
Step 3: Read values for a, b, c
Step 4: Calculate the roots of quadratic equation using
D ← sqrt (b × b – 4 × a × c).
if (d > 0) then calculate, (real roots and distinct)
X1 ← (-b + d) / (2 × a).
X2 ← (-b - d) / (2 × a).
elseif (d==0)
x1= x2= (-b/2a)
otherwise,
rp= (-b/2a) and ip =(d/2a)
x1= rp + i * ip
x2= rp – I * ip
Step 5: Display the value of roots as x1 and x2.
Step 6: stop.
2
5
Algorithm and Flowchart for finding the roots of quadratic
equation.
2
6
Compilation and Execution
2
7
Compilation and Execution
2
8
Compilation and Execution
2
9
Compilation and Execution
3
0
Debugging and Testing
o Debugging is the discovery and correction of programming errors. Even
after full care in program design and codding, some errors may remain
in the program, because programmer might have never thought about a
particular case.
o Testing ensures that program performs correctly the required task.
Testing is done by verification and validation.
o Errors means failure of compiling and executing the program
successfully. In programming errors can be categorized into two types:
1. Syntax errors: if program code is not written properly.
2. Semantic errors: program after compilation , we shall get errors like
memory overflow, floating point errors etc. and we are not getting the
correct result. Also known as logical errors.
31
Assignments
1. List and Explain program development (problem solving) and compilation
process in details. Draw a flowchart to find all the possible roots of a
quadratic equation. [4+4]
2. Describe the recent software trends. Explain in details about the
features that a software/program should include. [2+2]
3. List out the general rules for flowcharting. What are the errors that
might occur during debugging? Explain [2+2]
4. Explain compilation process with suitable diagram. Why we need to
analyze the problem before solving it. [3+1]
5. What is program? Explain different types of programming language in
brief. [1+3]
6. What is algorithm? Explain how does algorithm and flowchart helps in
computer programming. [1+3]
7. What are different types of computer software? What do you mean by
high level language and low-level language? [2+2]
8. What are preprocessor directives? Explain with example [2]
3
2
Assignments
8. What is computer programming and computer software? Explain types of
programming language. Define compiler [2+2+2]
9. Explain different generation(evolution) of programming language.[4]
10. Explain compilation linking and loading process with example. How do you
debug a c-program? [4+4]
11. Define algorithm and flowchart. Use the various commonly used symbols
used in flowcharts. How does flowchart help in programming? [2+2+2]
12. Explain structure of C-program with example. How compiler differ from
assembler and interpreter? [2+3]
13. Draw a flowchart to calculate the area of a given triangle using A =
√[s(s-a)(s-b)(s-c)], where 's' is the semi-perimeter of the triangle given
by s = (a + b + c)/2. [4]
14. Write an algorithm and flowchart to find the greatest number among
four numbers. [4]
15. Write an algorithm and flowchart factorial of a given number. [4]
3
3
. . . to be continued !!!
3
4