0% found this document useful (0 votes)
6 views60 pages

Chapter 1 - Introduction

The document provides an introduction to programming, covering key concepts such as algorithms, programming languages, and computer components. It explains the role of source code, variables, data types, and the importance of algorithms in problem-solving. Additionally, it discusses the differences between high-level and low-level programming languages, as well as the function of translators like compilers and interpreters.

Uploaded by

Mehtab Bhullar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views60 pages

Chapter 1 - Introduction

The document provides an introduction to programming, covering key concepts such as algorithms, programming languages, and computer components. It explains the role of source code, variables, data types, and the importance of algorithms in problem-solving. Additionally, it discusses the differences between high-level and low-level programming languages, as well as the function of translators like compilers and interpreters.

Uploaded by

Mehtab Bhullar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Programming for

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

• High Level: MATLAB, Java, Kotlin – Less Code


• Middle Level: C-Language
• Low Level: Assembly/Mnemonics Language – More Code
• 010101111
Programming Language
1. Computers also have language that they understand.
2. Computers don’t understand human language.
3. Computers understand language of 0s and 1s, i.e., binary
code.
4. It will be hectic for human to communicate with computers in
binary codes.
5. Programming languages are like human languages but more
structured and need to be learned.
Programming Language
1. Computer programming languages are classified as
• High level languages are farther from Machine Language but closer
to human language
• Low level languages are closer to Machine Language but farther to
human language
2. Computer needs a translator in between to understand
human language

Computer Program Translator Machine Language


or Source Code or Binary Code
Translator
1. Interpreters processes the source code line by line and runs every line
in the final program. It continues to execute the code until encounters an
error. Interpreter take longer time. For example, is Python

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++.

3. Hybrid translator is a combination of the Interpreter and Compiler. For


example, Java first compiles your source code to an intermediate format
known as the Bytecode. Bytecode is then interpreted and executed by a
runtime engine also known as a Virtual Machine.

4. Assemblers translates low-level Assembly language to binary.


Computer Components
Chapter 1
Computer Components
1. Hardware is so-termed because it is hard or rigid to changes
due physical components.
2. Software is soft because it is easy to change through
programming.
3. Hardware is typically directed by the software to execute any
command or instruction.
4. A combination of hardware and software forms a
usable computing system/embedded system.
5. Some hardware do not co-exist with software.
Computer Components… Hardware
1. Central Processing Unit (CPU)
2. Monitor
3. Mouse
4. Keyboard
5. Memories
6. Graphics Processing Unit
7. Mic & Speakers
8. Motherboard
Input & Output Devices
1. Keyboard 1. Monitors
2. Mouse 2. Graphic Plotter
3. Joy Stick 3. Laser/InkJet Printer
4. Stylus pen 4. 3D Printer
5. Webcam 5. Speaker
6. Scanner
7. Graphic Tablet
8. Microphone
9. Magnetic Ink Card Reader (MICR)
10. Optical Character Reader (OCR)
11. Bar Code/ Optical Mark Reader
Computer Memories
Idea of Algorithms
Steps to Solve Logical & Numerical
Problems
Chapter 1
Algorithm… Definitions
1. Dictionary: A process/set of rules/instruction to be
followed in calculations or other problem-solving operations,
especially by a computer.
2. Wikipedia: An algorithm is a step-by-step procedure for
calculations. Algorithms are used for calculation, data
processing, and automated reasoning.

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

2. Parameter Initialization & Termination Block


Initialization:
Input & Output
3. Processing & Calculation Block
Processing &
Calculations
4. Decision Block

Decision
Flow Chart 1: Factorial of N>0 Flow Chart

Start

Input N>0 &


Factorial M = N

Calculate N = N-1

Yes
If N <= 1 Output is M
No

Calculate M = M*N End


Pseudocode 1: Factorial of N>0
1. Initialize: Input N, Factorial M = N
2. Calculation: N = N – 1
3. IF N <= 1 THEN
Output Factorial is M
Terminate
ELSE
M = M * N;
Go to 2: Loop
END
Pseudocode 2: Factorial of N>0

1. Initialize: Input N, Factorial M


2. M = 1
3. FOR LOOP (i = N; i >= 1; i --)
M = i x M;
END
1. Output Factorial is M
Flow Chart 2: Root of Quadratic Equation
Start
𝑦 = 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
Input Coefficients: 𝑦 = 𝑥 − 𝑅1 𝑥 − 𝑅2
𝑎, 𝑏 & 𝑐
𝑥 = 𝑅1 & 𝑥 = 𝑅2
Yes
If 𝑎 = 0 ?
No
−𝑏 − 𝑏2 − 4𝑎𝑐
𝑅1 = Output: Not a
2𝑎
−𝑏 + 𝑏2 − 4𝑎𝑐 Quadratic Equation
𝑅2 =
2𝑎

Output Roots:
𝑅1 & 𝑅2

Stop
Pseudocode: Roots of Quadratic
Equation

1. Initialization: Take coefficients from user as Input: 𝑎, 𝑏, 𝑐


2. IF (𝑎 == 0) {
This is NOT A QUADRATIC Equation
} ELSE {
𝑏− 𝑏 2 −4𝑎𝑐 𝑏+ 𝑏 2 −4𝑎𝑐
𝑅1 = − and 𝑅2 = −
2𝑎 2𝑎
}
3. Output: Roots are 𝑅1 and 𝑅2
Flowchart
• A flowchart is a type of diagram that represents a workflow or
process.

• A flowchart can also be defined as a diagrammatic


representation of an algorithm.

• A flowchart is a diagrammatic step-by-step approach to


solve a problem or complete a task.
Pseudocode
• It is simply an implementation of an algorithm in the form of
annotations and informative text written in plain English.

• It has no syntax like any of the programming language

• Pseudocode cannot be compiled, interpreted or Executed by


the computer.
Advantages of Pseudocode
1. Readability: Pseudocode improves the readability and is one of
the best approaches to start implementation of an algorithm.

2. Bridge: Acts as a bridge between the program and algorithm or


flowchart. Also works as a rough documentation, so the
program of one developer can be understood easily when a
pseudocode is written out. In industry, pseudocode is vital.

3. Easy to Code: Pseudocode explains what exactly each line of a


program should do, that makes programming phase easier.
Source Code, Variables
& Data Types
Chapter 1
Variables
1. A symbol or names or labels that stands for some value or
information.
2. A variable holds a changeable value.
3. Variables provide temporary storage to store some
information that will be needed during the lifespan of the
computer program.
4. Every variable has
• Name (Identifier)
• Data Type
• Size
• Value
Variables Types
1. Local Variable are those that are accessible within a specific
part of the program (function)
2. Global Variable are those that can be accessed by any part
of the program during execution of the program

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

const float pi=3.14 ;


• const is constant
• int is Data Type
• pi is Constant Name
Keywords in C (32)

Keywords Keywords Keywords Keywords


auto double int struct
break else long switch
case enum register typedef
const extern return union
char float short unsigned
continue for signed volatile
default goto sizeof void
do if static while
Source Code
1. A text listing set of commands to be compiled or assembled into
an executable computer program.

2. Source code is the language or string of words, numbers,


letters and symbols that a computer programmer uses.

3. Source code is a human-readable text written in a specific


programming language.

4. Source code presents exact rules and specifications for the


computer that can be translated into the machine's language.
Range Calculation for an Integer

• Integer Size = 4 Bytes = 32 Binary Bit


• Range of values = 216 ⇒ 0000 0000 0000 0000 = 1111 1111
1111 1111
• 1111 1111 1111 1111 ⇒ 1 × 215 + 1 × 214 + 1 × 213 + 1 × 212 + 1 ×
211 + 1 × 210 + 1 × 29 + 1 × 28 + 1 × 27 + 1 × 26 + 1 × 25 + 1 × 24 +
1 × 23 + 1 × 22 + 1 × 21 + 1 × 20
• = 32,768 + 16,384 + 8,192 + 4,096 + 2,048 + 1,024 + 512 + 256 +
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 65535
• 0 to 215 ≡ 0 to 65,535
Variables and Memory
Locations
Chapter 1
Variables
1. A variable is a name given to a storage area that our
programs can manipulate.
2. Each variable has a specific data type that determines
• the size of required memory to store information and
• the range of values that can be stored within that memory
• the set of operations that can be performed on that variable
3. Variable name can be composed of letters, digits, and the
underscore character.
4. It must begin with either a letter or an underscore. Upper
and lowercase letters are distinct because C is case-
sensitive.
Data Types in C
1. Character
2. Integer
3. Float
4. Double
5. Void
Data Types: Character Type

Data Type Size (Bytes) Value Range

char or signed char 1 -128 to 127

unsigned char 1 0 to 255


Data Types: Integer Type

Data Type Size (Bytes) Value Range


15 15
int or signed int 2 -32,768 (-2 ) to 32767 (2 -1)
16
unsigned int 2 0 to 65,535 (2 -1)
7 7
short int or signed short int 1 -128 (2 ) to 127 (2 -1)
8
unsigned short int 1 0 to 255 (2 -1)
31 31
long int or signed long int 4 -2,147,483,648 (2 ) to 2,147,483,647 (2 -1)
32
unsigned long int 4 0 to 4,294,967,295 (2 -1)
Data Types: Float & Double Type
Data Type Size (Bytes) Value Range

Float 4 3.4E-38 to 3.4E+38

double 8 1.7E-308 to 1.7E+308

long double 10 3.4E-4932 to 1.1E+4932


Variable Declaration
<type> <variable list> ;
• Example

• int is data type int count, num ;


• count is first variable name
• num is second variable name
Variable Declaration with Initialization
<type> <variable name> ;
• Example

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

• He goes to the cold (Semantic Error)


• Cold is an adjective.
• Grammatically/Syntactically sentence is correct
• But incorrect semantically with correct syntax.
In C Language - Syntax Error
• Example Code

while(.) {
printf("hello");
}

• While Expression cannot have dot (.) argument


In C Language - Runtime Error
• Example Code

div = n/0;
printf(“Result = %d", div);

• Divide by zero
In C Language - Linker Error
• Example Code

void Main(){
….
}

• Here we should use main() not Main()


In C Language - Logical Error
• Example Code

for(i = 0; i < 3; i++);


{
printf("Loop No. %d", i);
}

• FOR LOOP is terminated by semicolon


• No Output
In C Language - Semantic Error
• Example Code

int a, b, c;
a + b = c;

• Right Value after operation should be assigned Left Value


• Syntax is fine
• Semantically incorrect
Object Code and
Executable Code
Chapter 1
Object Code & Executable Code
Complier
(Pre-processor)
Source File

Assembler
Assembly File

Linker
Object File

Executable File
Source Code to Executable Code

1. Source code is C program that is written in IDE and saves as


.c file
2. Some compilers are designed to convert source code into an
Assembly Language Code
3. Assembly Language Code is a human-readable notation
using the mnemonics.
4. Object/Machine code is the output of a compiler after it
processes the source code.
5. Executable (Binary) Code is the output of a Linker after it
processes the Object/Assembly Code.
• 0 and 1 – Binary
• 0 to 7 – Octal
• 000 – 0
• 001
• 010
• 111 – 7
• 0 – 9 Decimal
• 0000 – 0
• 1001 – 9
• 0 – F Hexa-Decimal
• 0000 – 0
• 1111 – F (0-9, A, B, C, D, E, F)

You might also like