Unit 1
Unit 1
CT 101
BACHELOR IN ENGINEERING
PULCHOWK CAMPUS
Pulchowk, Lalitpur
Syllabus
1. Introduction to Computer 8. Structures
Programming
2. Overview of C Programming
9. File management
3. Operators and Expressions
4. Input and Output 10. Recent Trends in
5. Control Structures Programming
6. Array and Pointer
7. User-defined Functions
2
UNIT 1 Introduction to Computer Programming
Contents
1.1 Definition of a computer program and programming language
1.2 Types and Generations of Programming Languages
1.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
1.3.6 Documentation
3
Programming Language
• It a set of rules that provides a way of telling a computer what
operations to perform
• It is used for writing programs, which are specifications of a
computation or algorithm
• It is a set of words, symbols and codes that enables human to
communicate with computers
• The term programming language usually refers to high-level
languages, such as BASIC, C, C++, COBOL, FORTRAN, Python, and
Pascal
4
Characteristics
• Must be simple, easy to learn and use, have good readability, and be
human recognizable
• Well-structured and documented so that it is suitable for application
development
• Abstraction and ability to define the complex structure
• Consistent in terms of syntax and semantics
• Portable : capable of developing software for more than one
computer system
• Efficient : easily converted into a machine code and executed
consumes little space in memory
5
Language Processors
• We need to convert program into machine language so that the
computer can understand it
• Compilers, interpreters, translate programs written in high-level
languages into machine code that a computer understands
• Assemblers translate programs written in low-level or assembly
language into machine code
6
Language Processors
• Compile is to transform a program written in a high-level
programming language from source code into object code
• This can be done by using a tool called compiler
• A compiler reads the whole source code and translates it into a
complete machine code program to perform the required tasks which
is output as a new file
7
Language Processors
• Interpreter is a program that executes instructions written in a high-
level language
• An interpreter reads the source code one instruction or line at a time,
converts this line into machine code and executes it
8
Language Processors
• Assembler is used to translate the program written in Assembly
language into machine code
• The source program is an input of an assembler that contains
assembly language instructions. The output generated by the
assembler is the object code or machine code understandable by the
computer
• Assembler is basically able to convert these mnemonics in Binary
code. Here, these mnemonics also depend upon the architecture of
the machine
9
Generations of Programming Language
• The first generation languages, or 1GL, are low-level languages that
are machine language (fast execution but more difficult to write)
• The second generation languages, or 2GL, are also low-level
languages that generally consist of assembly languages
o Symbolic or machine code (mnemonics)
o Assembler used to translate to machine code
• The third generation languages, or 3GL, are high-level languages such
as C, FORTRAN, COBOL
o Procedure Oriented, Programmer friendly
o Compiler/Interpreter
10
Generations of Programming Language
• The fourth generation languages, or 4GL, are languages that consist
of statements similar to statements in a human language.
o Problem Oriented, non-procedural i.e. query language
o Fourth generation languages are commonly used in database programming
and scripts (Perl, PHP, Python, SQL)
• The fifth generation languages, or 5GL, are programming languages
that contain visual tools to help develop a program.
o Natural language, Artificial Intelligence
o Solves problem using given constraints
o Example of a fifth generation language is Prolog, Mercury, OPS5
11
Levels of Programming Languages
• Machine language (Low-level language)
• Assembly language (Low-level language)
• High-level language
12
Levels of Programming Languages
13
Machine Language
• Machine language is a collection of binary digits or bits that the
computer reads and interprets
• Machine languages are the only languages understood by computers
• While easily understood by computers, machine languages are almost
impossible for humans to use because they consist entirely of
numbers
14
Machine Language
• Machine Language
• 01001000 01100101 01101100 01101100 01101111 00100000 01010111
01101111 01110010 01101100 01100100
• High level language
FOR I=1 TO 1000: PRINT "A";: NEXT I
15
Assembly Language
• Low level language that deals with hardware registers by name
• A program written in assembly language consists of a series of
instructions mnemonics that correspond to a stream of executable
instructions, when translated by an assembler, that can be loaded
into memory and executed
• It uses keywords and symbols, much like English, to form a
programming language
16
Assembly Language
• Computer doesn't understand the assembly code, so we need a way
to convert it to machine code, which the computer does understand
• Assembly language programs are translated into machine language by
a program called an assembler
• It is specific to particular computer architecture
17
Assembly Language
• Example:
• Machine language : 10110000 01100001
• Assembly language : mov a1, #061h
• Meaning: Move the hexadecimal value 61 (97 decimal) into the processor
register named "a1"
18
High Level Language
• High-level languages allow us to write computer code using
instructions resembling everyday spoken language (for example:
print, if, while) which are then translated into machine language to be
executed
• Programs written in a high-level language need to be translated into
machine language before they can be executed
• Some programming languages use a compiler to perform this
translation and others use an interpreter
19
High Level Language
• Examples of High-level Language:
oC
o C++
o JAVA
o BASIC
o COBOL
o PASCAL
o PYTHON
o .NET
20
History of high-level programming languages evolution form 1954 – 2002
21
Program development
• Programming is the implementation of logic to facilitate specified
computing operations and functionality.
• It is the process of performing a particular computation usually by
designing/building an executable computer program
• Syntax refers to the rules that define the structure of a language.
• It means the rules that control the structure of the symbols, punctuation, and
words of a programming language
• using character structures that a computer can interpret
22
Algorithm
• It is a finite sequence of well-defined instructions, typically used to
solve a class of specific problems or to perform a computation
• It is a set of well-defined instructions to solve a particular problem. It
takes a set of input and produces a desired output
• Systematic logical approach which is a well-defined, step-by-step
procedure that allows a computer to solve a problem
• Representation:
• Flowchart
• Pseudo Code
23
Algorithm
• Pseudocode
• It is a simpler version of a programming code in plain English which uses short
phrases to write code for a program before it is implemented in a specific
programming language
• Pseudocode is not an actual programming language
• Flowchart
• It is a graphical representation of an algorithm. Programmers often use it as
a program-planning tool to solve a problem
• It makes use of symbols which are connected among them to indicate the flow of
information and processing
• Program It is exact code written for problem following all the rules of the
programming language
24
Algorithm
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values 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
25
Flowchart Basic Symbol
26
• Terminal: used to show the
beginning and end of a set of
computer-related processes
• Input/Output: used to show any
input/output operation
• Processing: used to show any
processing performed by a
computer system
• Predefined processing: used to
indicate any process not specially
defined in the flowchart
Flowchart
27
• Comment: used to write any
explanatory statement required to
clarify something
• Flow line: used to connect the
symbols Document Input/Output:
used when input comes from a
document and output goes to a
document
• Decision: used to show any point in
the process where a decision must
be made to determine further
action
• On-page connector: used to connect
parts of a flowchart continued on
the same page
• Off-page connector: used to
connect parts of a flowchart
continued to separate pages
28
Decision-making
29
Flowchart to check negative number
30
Q. Algorithm and Flowchart
31
Q. Algorithm and Flowchart
32
Program Development Life Cycle
• It is a set of steps or phases that are used to develop a program in any
programming language
• Problem Definition
• Problem Analysis
• Algorithm Development
• Programming Compilation, Linking and Execution
• Testing & Debugging
• Documentation
• Maintenance
33
Program Development Life Cycle
1. Problem Definition
• Define the problem statement and we decide the boundaries of the problem
• Understand the problem statement, what is our requirement, what should be
the output of the problem solution
2. Problem Analysis
• Determine the requirements like variables, functions, etc. to solve the
problem.
• Gather the required resources to solve the problem defined in the problem
definition phase.
• Determine the bounds of the solution
34
Program Development Life Cycle
3. Algorithm Development
• Develop a step by step procedure to solve the problem using the specification
given in the previous phase
• This phase is very important for program development. That means we write
the solution in step by step statements
4. Coding / Programming
• Uses a programming language to write or implement the actual programming
instructions for the steps defined in the previous phase
• Construct the actual program to solve the given problem using programming
languages like C, C++, Java, etc.,
35
Compilation Process
• The compilation is a process of converting the source code into object
code
• The compiler tests the source code for syntactic or structural errors
and produces the object code if the source code is error-free
• Following are the steps that a program goes through until it is
translated into an executable form:
• Preprocessor
• Compiler
• Assembler
• Linker
41
Program Development Life Cycle
7. Documentation
• The structure charts, pseudocodes, flowcharts and decision tables
developed during the design phase become documentation for others who
are associated with the software project
• This phase ends by writing a manual that provides an overview of the
program’s functionality, tutorials for the beginner, in-depth explanations of
major program features, reference documentation of all program commands
and a thorough description of the error messages generated by the program
42
Program Development Life Cycle
• 8. Maintenance
• The program is actively used by the users. If any enhancements found in this
phase, all the phases are to be repeated to make the enhancements
• If the user encounters any problem or wants any enhancement, then we need
to repeat all the phases from the starting, so that the encountered problem is
solved or enhancement
43
Program Errors
• Compile time errors: syntax errors and static semantic errors
indicated by the compiler
• Runtime errors: dynamic semantic errors, and logical errors, that
cannot be detected by the compiler (debugging)
44
Program Errors
• Syntax errors: errors due to the fact that the syntax of the language is
not respected
• Semantic errors: errors due to an improper use of program
statements
• Logical errors: errors due to the fact that the specification is not
respected
45