Chapter 1 - Introduction
Chapter 1 - Introduction
Problem Solving
Dr Satvir Singh
Chapter 1: Introduction
1. Introduction to Programming;
2. Introduction to components of a computer system (disks, memory,
processor, where a program is stored and executed, operating
system, compilers etc.);
3. Idea of Algorithm: Steps to solve logical and numerical problems;
4. Representation of Algorithm: Flowchart / Pseudocode with
examples;
5. From algorithms to programs;
6. Source code, Variables with data types;
7. Variables and memory locations, Syntax and Logical Errors in
compilation;
8. Object and executable code.
Introduction to
Programming
Chapter 1
Introduction to Programming
1. By definition, programming is the method of instructing
computers to carry out some activity.
2. A computer program is a set of instructions organized in a
specific order to execute a particular task.
3. A Computer is a device that is capable of processing code.
For example, Mobile phone, laptop, desktop computer &
mainframe computer, etc.
4. Programming is also called Coding. Programs are
sometimes called Source Code.
Source Code & C-Language
• Open Source: Example C-Language
• Proprietary/Freeware
• Proprietary/Paid
• C-Language
• Middle Level
2. Compilers convert the source code into binary code in totality; process
is called compilation. Then binary code is executed. Incase of error
detected during compilation, the binary code is not generated. For
example, middle level language C/C++.
Algorithm:
Determine
Input Output
output from
inputs
Need for Algorithm
1. The algorithm improves the efficiency of an existing technique.
2. To compare the performance of the algorithm with respect to other
techniques.
3. The algorithm gives a strong description of requirements and goal of
the problems to the designer.
4. The algorithm provides a reasonable understanding of the flow of the
program.
5. The algorithm measures the performance of the methods in different
cases (Best cases, worst cases, average cases).
6. The algorithm identifies the resources (input/output, memory) cycles
required by the algorithm.
7. With the help of an algorithm, we can measure and analyze the
complexity time and space of the problems.
8. The algorithm also reduces the cost of design.
Properties of Algorithm
1. Well defined Inputs: There can be one or more quantities
that are supplied as inputs to algorithm.
2. Well defined Output: At least one quantity is produced as
output.
3. Definiteness: Each instruction of the algorithm should be
clear and unambiguous.
4. Finiteness: The process should be terminated after a finite
number of steps.
5. Effectiveness: Every instruction must be basic enough to be
carried out theoretically or by using paper and pencil.
Representation of
Algorithm: Flowchart &
Pseudocode
Chapter 1
Flow Charts
1. Start & End Block
Start & End
Decision
Flow Chart 1: Factorial of N>0 Flow Chart
Start
Calculate N = N-1
Yes
If N <= 1 Output is M
No
Output Roots:
𝑅1 & 𝑅2
Stop
Pseudocode: Roots of Quadratic
Equation
Global Variable
Program
Local Variable Function
Variable Naming Rules
1. Variable name should use only alphabets, number and
underscore
2. Special characters are not allowed except underscore
3. Variable name can not begin with a number
4. Variable name must have at least one alphabet
5. Variable name can not be any reserved keyword
6. Blank spaces are not allowed in variable names
7. Valid names: ikgPtu, Vr4PTU, Ikg_ptu, _ECE
8. Invalid names: 2ptu, ptu@kpt, int, ECE 2020
Example: Variables
𝐴𝑟𝑒𝑎 = 𝜋𝑟 2 ≡ 𝐴 = 𝑝𝑖 ∗ 𝑟𝑎𝑑𝑖𝑢𝑠 ∗ 𝑟𝑎𝑑𝑖𝑢𝑠
1. This is a programming expression
2. Here, 𝐴𝑟𝑒𝑎 & 𝑟𝑎𝑑𝑖𝑢𝑠 are variables and pi (𝜋) is constant
3. Variable can store values that can be
1. Numerical
2. Character
3. String
4. Boolean
5. Memory Address
Constants
1. The opposite of a variable is a constant
2. A constant holds a value that never change
3. Due inflexibility constants are used less often than variables
4. Constants can hold similar values as variables do
5. Consider an expression 𝐴 = 𝑝𝑖 ∗ 𝑟 ∗ 𝑟
• pi =3.14 is constant
• 𝐴 & 𝑟 are variables
Data Type
1. Without data types computers cannot store information
2. Different data types have different sizes in memory
3. Difference Data Types in C
1. Character = 8bit = 1byte
2. Integer = 32bit = 4byte (short, long, long long)
3. Float = 64bits = 8byte (Double, Double Double)
4. Void = 1bit
Variable Declaration
int count=5 ;
• int is Data Type
• count is Variable Name
Constant Declaration
int count = 10 ;
char x = ‘Y’ ;
• count is int is data type variable name with initial value of 10
• x is char is data type variable name with initial value of ‘X’
Syntax, Semantic,
Runtime, Linker and
Logical Errors
Chapter 1
Syntax
1. The word syntax comes from the Ancient Greek for
coordination or ordering together.
2. In English language, syntax refers to the set of rules
(tense) that determines the arrangement of words in a
sentence.
3. In programming language, syntax refers to the protocols to
be followed while writing a program.
4. It is must to follow proper syntax while coding to get the
desired set of output.
5. The C basic syntax consists of header files, main function,
and program code line terminated with semicolons.
Semantics
• Semantics is about the meaning of the sentence.
• It answers the questions:
• Is this sentence valid?
• If so, what does the sentence mean?
• Example:
• a++; // Increments an integer a with 1
• If variable a would have been of float type then this increment
operation is meaningless
• However, syntactically there is no error
Simple Language… English Sentence
• He go to the school (Syntax Error)
• Incorrect grammar (syntax)
• Though meaning is being conveyed
while(.) {
printf("hello");
}
div = n/0;
printf(“Result = %d", div);
• Divide by zero
In C Language - Linker Error
• Example Code
void Main(){
….
}
int a, b, c;
a + b = c;
Assembler
Assembly File
Linker
Object File
Executable File
Source Code to Executable Code