C20ProgrammingChapter201
C20ProgrammingChapter201
INTRODUCTION TO COMPUTER
PROGRAMMING
1.1 Definition of a Computer Program and Programming Language
Program
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.
Computer Programming
The process of developing and implementing computer programs
to solve a problem is known as computer programming.
Programming Langauge
A programming language is a structured and formalized system of
communication that enables humans to create sets of instructions, known
as code, to be executed by computers.There are various programming
languages used for programming purpose.Each programming language
has a set of syntactic and semantic rules.
Compiled By Bishon
The differences between high level and low level language are:
High-Level Language Low-Level Language
1. High-level language is close to 1. Low-level language is close to
human language. So, it is machine. So, it is machine
programmer friendly. friendly.
2. One doesn’t require the 2. One requires the hardware
hardware knowledge to program knowledge to program in low-
in high-level language level language.
3. High-level language is machine 3. Low-level language is totally
independent. machine dependent.
4. Compiler and interpreter are 4. No translator is required but for
used to translate high-level assembly language, assembler is
language into machine code. used to translate low-level
language into machine code.
5. The program execution is slower 5. The program execution is faster
than low-level language. than high-level language.
6. Debugging and manipulation of 6. Debugging and manipulation of
code is easy. code is difficult.
7. C, C++, Python, Java, etc. are 7. Machine language and assembly
high-level languages. language are low-level
languages.
Compiled By Bishon
➢ It is machine dependent
➢ It is difficult to write program.
➢ Language is complex for programmers
➢ Machine language is error prone
➢ Difficult to modify and troubleshoot
ii. Second Generation (2GL): Assembly Language (1950s-1960s)
➢ Assembly language introduced symbolic representations of
machine instructions using mnemonics.
➢ Programs were written using mnemonics corresponding to the
machine language instructions.
➢ Improved over 1GL in terms of readability and usability.
Advantages of 2GL:
➢ Relatively easy to use and understand than machine level
language
➢ Easier to locate and correct errors
➢ Less error prone
➢ More efficient than high level language
➢ Programs can be modified easily.
Disadvantages of 2GL:
➢ Machine dependent
➢ Harder to learn
➢ Less efficient than machine language
➢ No standardization
➢ No support for modern software engineering techniques
iii. Third Generation (3GL): High-Level Languages (1950s-1970s)
➢ Fortran, COBOL, and ALGOL are examples of early high-level
languages.
➢ These languages introduced abstraction from hardware details
and used more English-like syntax.
➢ Programs were written with statements and expressions that are
closer to human language.
➢ Improved portability and ease of programming.
Compiled By Bishon
iv. Fourth Generation (4GL): Domain-Specific Languages (1980s-
1990s)
➢ Focused on making programming more user-friendly by
providing higher levels of abstraction.
➢ SQL (Structured Query Language) for database management is
an example of a 4GL.
➢ Aimed at specific problem domains and often used for database
queries and business applications.
v. Fifth Generation (5GL): AI and Natural Language Processing
(1980s-Present)
➢ Characterized by languages and tools designed to facilitate
artificial intelligence (AI) and natural language processing.
➢ Prolog and Lisp are examples of early 5GL languages.
➢ Focus on symbolic reasoning, logic programming, and handling
complex knowledge representation.
Problem Analysis
Programming
Compiled By Bishon
Debugging and Testing
Documentation
Fig.: Problem solving steps
Compiled By Bishon
Some commonly used symbols in flowchart are as follows:
Symbol Purpose
Start/stop
Processing
Input/output
Decision
Predefined process/Function
Connector
Flow line
Fig.: Commonly used symbols in flowchart
The advantages of a flowchart design are as follows:
i. Communication: Better way of communicating the logic of
a system to all concerned.
ii. Effective analysis: Problem can be analyzed in more
effective way.
iii. Proper documentation: Serve as a good program
documentation, which is needed for various purposes.
iv. Efficient coding: Act as a guide or blueprint during the
systems analysis and program development phase.
v. Proper debugging: The flowchart helps in debugging
process.
vi. Efficient program maintenance: The maintenance of
operating program becomes easy with the help of flowchart.
It helps the programmer to put efforts more efficiently on that
part.
Basic guidelines to be followed to draw a flowchart are as follows:
i. Standard flowchart symbols should be used according to the
purpose.
ii. There should be a logical start and stop.
Compiled By Bishon
iii. It should be clear, neat and easy to follow. There should not
be any room for ambiguity in understanding the flowchart.
iv. The direction of the flow of a procedure or system is from
left to right or top to bottom.
v. English like language should be used in flowcharts, not
specific programming language.
vi. It is useful to test the validity of the flowchart by passing
through it with a simple test data.
Example: Add two numbers entered by the user
Algorithm:
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values of num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Flowchart:
Compiled By Bishon
Example: Check whether the given number is positive ,negative
or zero
Algorithm:
Step 1: Start
Step 2: Declare variable num
Step 3: Read the value of num.
Step 4: if (num>0) goto step 4.1 else if (num<0) goto step 4.2 else
goto step 4.3
Step 4.1: Display the number is positive and go to step 5
Step 4.2: Display the number is negative and go to step 5
Step 4.3: Display the number is zero and go to step 5
Step 5: Stop
Flowchart
Compiled By Bishon
Example: Display natural number from 1 to N
Algorithm:
Step 1: Start
Step 2: Declare variable N and i
Step 3: Read the value of N.
Step 4: Initialize i to 1
Step 5: Repeat step5.1 and 5.2 until i ≤ N.
Step 5.1 Display the value of i.
Step 5.2 Increment the value of i by 1.
Step 6: Stop
Flowchart
Compiled By Bishon
Example: Find the largest number among three numbers
Step 1: Start
Step 2: Declare variables num1,num2 and num3.
Step 3: Read variables num1,num2 and num3.
Step 4: if (num1 > num2) goto step 4.1 else if (num2 > num3) goto
step 4.2 else goto step 4.3.
Step 4.1: if num1 > num3 goto step 4.1.1 else goto step 4.1.2
Step 4.1.1:Display num1 is the largest number
and go to step 5.
Step 4.1.2: Display num3 is the largest number
and go to step 5.
Step 4.2:Display num2 is the largest number and go to step 5.
Step 4.3:Display num3 is the greatest number and go to step
5.
Compiled By Bishon
Step 5: Stop
Example: Find Roots of a Quadratic Equation ax2 + bx + c = 0
Step 1: Start
Step 2: Declare variables a, b, c, d, r1, r2, rp and ip;
Step 3: Calculate discriminant as:
d ← b2-4ac
Step 4: if(d >0) goto step 5 else if(d=0) goto step 6 else goto step 7
Step 5:
Step 5.1: Calculate r1 and r2 as:
r1 ← (-b+√d)/(2a)
r2 ← (-b-√d)/(2a)
Step 5.2: Display roots are real and unequal
Step 5.3: Display r1 and r2 as roots and go to step 8
Step 6:
Step 6.1: Calculate r1 and r2 as:
r1 ← -b/2a
r2 ← r1
Step 6.2: Display roots are real and equal
Step 6.3: Display r1 and r2 as roots and go to step 8
Step 7:
Step 7.1: Calculate real part and imaginary part as:
rp ← -b/2a
ip ← √(-d)/2a
Step 7.2: Display roots are imaginary and unequal
Step 7.3: Display rp+j(ip) and rp-j(ip) as roots
Step 8: Stop
Example: Find the factorial of a number
Step 1: Start
Step 2: Declare variables n, factorial and i.
Compiled By Bishon
Step 3: Initialize variables as:
factorial ← 1
i←1
Step 4: Read value of n
Step 5: Repeat step5.1 and 5.2 while i ≤ n
Step 5.1:factorial ← factorial*i
Step 5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
Compiled By Bishon
count ←count+1
Step 5.2: i ←i+1
Step 6: if count=2
Display the number is prime and go to step 7
else
Display the number is not prime
Step 7: Stop
Example: Find the Fibonacci series till the term less than 1000
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps while second_term ≤ 1000
Step 5.1: temp ← second_term
Step 5.2: second_term ← second_term + first_term
Step 5.3: first_term ← temp
Step 5.4: Display second_term
Step 6: Stop
1.3.3 Programming
Programming is the process of transforming algorithm or
flowchart into computer understandable form. Programming can
be done in any higher or low level language.
Tips for good programming:
• Follow the standards of programming. Use proper indentation
& comments.
• Always use meaningful names & labels.
• Keep modules short.
• Make modules general such that it can be used for other
purpose.
Compiled By Bishon
1.3.4 Compilation, Linkiing and Execution
Actual codes written in high level language should be converted to
low level language before an executable program is created. Whole
process is known as compilation and execution.
The process of converting a program written in high-level
language (source program) to an executable program is called
compilation and execution.
• In this process, source program is converted to object code
(low level language) through a complier at first.
• Then, object code is linked with the library functions through
linker. That gives real values to all symbolic address (in
object code), there by producing an executable program.
Source Program
Compiler
Object code/file
Runtime library/
library function Linker Executable program
Compiled By Bishon
Types of errors:
Generally, errors are classified into following types:
i. Syntax error: Any violation of rules of the programming
language results in syntax error. Example: missing of
semicolon at the last of any statement.
ii. Run-time error: Errors such as mismatch of data types or
referencing an out of range array element go undetected by
compiler. A program with these mistakes will run but
produce the erroneous results.
iii. Logical error: These errors are related to the logic of the
program execution. Such actions as taking the wrong path,
failure to consider a particular condition and incorrect order
of evaluation of statements belong to this category.
iv. Latent error: These are hidden errors that shows up only
when a particular set of data is used.
Example: r=(x+y)/(p-q); This expression generates error
when p=q.
Debugging techniques:
Debugging is the process of isolating and correcting any type of
above mentioned errors. Different types of debugging techniques
are discussed below:
i. Error isolation: Error isolation is used for locating an error
resulting in a diagnostic message. If we do not know the
general location of the error, we can generally find the
location of the error by temporarily deleting a certain portion
of program and rerunning the program to see whether the
error is again appeared or not. If the error is not appeared
again, we know that the error is in the deleted code. If the
error again appears, we know that the deleted portion is error
free. Temporary deletion is accomplished by surrounding the
instructions with comment markers(/* */). Similarly, we can
put different unique printf() statement in different parts
(blocks) of the program to check whether that part(block) of
the program is executed or not.
Compiled By Bishon
ii. Tracing: In this technique, printf() statement is used to print
the values of some important variables at different stages of
program. By observing these values, we can decide that
whether the assigned values are correct or not. If any value is
incorrect, we can easily fix the location of the error.
Similarly, we can display and observe the values of variables
that are calculated internally and assigned to the variables.
iii. Watch values: A watch value is the value of a variable or an
expression, which is displayed continuously as the program
executes. Thus, we can see the changes in a watch value as
they occur, in response to the program logic. By inspecting
these values carefully, we can determine where the program
begins to generate an incorrect or unexpected value. In Turbo
C++ IDE, to define the watch values, go through the
following steps:
Debug → Watchs → Add watch ... → specify variables or
expression to watch its value → OK
iv. Breakpoints: A break point is a temporarily stopping point
with in a program. Each breakpoint is associated with
particular instruction within the program. When the program
is executed, the program execution will temporarily stop at
the break point before the instruction is executed. The
execution may be resumed until the next break point is
encountered. Breakpoints are often used in conjunction with
watch values, by observing the current watch value at each
break points as the program executes. In Turbo C++, go
through the following steps to set breakpoints:
Debug → Add → Break point → provide the requested
information in the dialog box.
OR
Select a particular line within the program→ press
function F5.
To disable the break point again press function key F5.
v. Stepping: The process of executing one instruction at a time
is called stepping. We can determine which instruction
produce erroneous result or generate error messages by
Compiled By Bishon
stepping through the entire program. Stepping is often used
with watch values, allowing us to trace the entire history of a
program as it executes. Thus, we can observe changes to
watch values as they happen. This allows us to determine
which instruction generate erroneous results.
In Turbo C++, pressing function key F7 can do stepping.
Testing procedure:
i. Black box testing:
• It is an external test called functional test in which
whether or not the program performs properly is tested.
• It is the test of input/output behaviour without considering
the internal structure of the test objects.
ii. White box testing:
• It is a line by line testing method in which interior errors
are examined and recovered.
• It is concerned with the correctness of each statement.
1.3.6 Documentation
All the above processes should be properly documented for future
reference of both the original programmer and beginner. It is the
process of making a program well structured by using appropriate
comments and other techniques. It should start from very
beginning with the help of which the program would be easy to
understand by other person (user or other programmer).
There are two type of documentation:
• Programmer documentation
• User documentation
Merits of documentation:
• It increases the interaction between software and individual
who manage it and improves user efficiency.
• It helps in debugging, testing, maintenance, and redesign
process.
Compiled By Bishon