Algorithms
Def: An algorithm is a sequence of step to solve the problems.
Characteristics of Algorithm
Input: An algorithm should take zero or more inputs.
Output: An algorithm should produce at least one output.
Unambiguous: Every instruction within the algorithm must be clear and specific.
Finiteness: The algorithm must terminate after a finite number of steps.
Effectiveness: Each step of the algorithm must be a basic operation.
Language Independence: The algorithm can be implemented in any programming language.`
Correctness: The algorithm should be error-free and produce the desired output for every valid input.
Algorithms can be written in three ways:
By using Natural Language: Any high levels Language like English etc...
By using Pseudo code: It’s an intermediate language, which contains high level language
along with symbols.
By using Flowcharts: It’s a structural representation of an Algorithm.
Time Complexity: Measures the amount of time, takes to execute the set of instructions.
Best Case: The minimum number of steps an algorithm takes on any input of a given size.
Worst Case: The maximum number of steps an algorithm takes on any input of a given size.
Average Case: The average number of steps taken over all possible inputs of a given size.
Amortized Analysis: Averages the cost of a sequence of operations over time.
Space Complexity: Measures the amount of memory requires to run the instructions.
Flowcharts
Flowcharts: Flowcharts are the visual representations of an algorithm or a process.
Types of Flowcharts:
Process Flowchart: It represents the sequence of steps in a process.
Swim lane Flowchart: It organizes the process into different lanes, each representing a different
person or department.
Workflow Diagram: It represents how tasks, documents, or information move through a system.
Data Flow Diagram (DFD): It focuses on detailing the inputs, processes, and outputs
Decision Flowchart: It focuses on mapping out decision points within a process and the possible
outcomes of each decision.
Symbols used in Flowchart Designs:
Terminal/Terminator: The oval symbol indicates Start, Stop.
Input/output: A parallelogram denotes any function of input/output type.
Action/Process: A box represents arithmetic instructions such as adding, subtracting, multiplication
and division are indicated by action/process symbol.
Decision: Diamond symbol represents a decision point. Decision based operations such as yes/no
question or true/false.
On-Page Connector/Reference: Whenever flowchart spreads over more than one page, connectors
are used to indicate a jump from one part of the flowchart to another without drawing long or
complicated lines.
Off-Page Connector/Reference: Whenever flowchart spreads over more than one page, connectors
are used to indicate a jump from one part of the flowchart to another without drawing long or
complicated lines.
Flow lines: The Lines that connect different symbols in a flowchart to show the sequence and
directions of the process.
Rules for Creating a Flowchart:
Rule 1: Flowchart opening statement must be ‘start’ keyword.
Rule 2: Flowchart ending statement must be ‘end’ keyword.
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: Each decision point should have two or more distinct outcomes.
Rule 5: Flow should generally move from top to bottom or left to right.
Problem Solving Strategies
Top-Down Approach:
Breaking the problem into major components
Breaking each component into smaller subcomponents
Continue this process until every part is simple enough to implement.
Bottom-Up Approach:
Identifying and specifying the smaller components (or objects).
Linking these smaller parts together to form larger components
Continuously integrating them to complete the system
[Link]. Top-Down Approach Bottom-Up Approach
1. In this approach, the problem is broken down In this approach, the smaller problems are
into smaller parts. solved.
2. It is generally used by structured programming It is generally used with object oriented
languages such as C, COBOL, FORTRAN, etc. programming paradigm such as C++, Java,
Python, etc.
3. It is generally used with documentation of It is generally used in testing modules.
module and debugging code.
4. It does not require communication between It requires more communication between
modules. modules.
5. It contains redundant information. It does not contain redundant information.
6. Decomposition approach is used here. Composition approach is used here.
7. The implementation depends on the Data encapsulation and data hiding is
programming language and platform. implemented in this approach.
Basics of Computer
Generations Period Technology Used
First Generation 1946-1959 Vacuum tube-based
Second Generation 1959-1965 Transistor-based
Third Generation 1965-1971 Integrated Circuit based
Fourth Generation 1971-1980 VLSI microprocessor-based
Fifth Generation 1980-onwards ULSI microprocessor-based
Input Devices: Allow users to enter data and commands, such as keyboards, mouse, and scanners.
Output Devices: Display or produce the result of the computer’s processing, like monitors, printers,
and speakers.
Control Unit (CU): The process of input, output, processing and storage is performed under the
supervision of a unit called 'Control Unit'. It decides when to start receiving data, when to stop it,
where to store data, etc. It takes care of step-by-step processing of all operations inside the computer.
Memory Unit: Computer is used to store data and instructions.
Arithmetic Logic Unit (ALU): The major operations performed by the ALU are addition, subtraction,
multiplication, division, logic and comparison.
C- Programming
C is a general-purpose procedural programming language initially developed by Dennis Ritchie in
1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to
write the UNIX operating system.
Structure of the C program:
The first component in C program is Header files. A header file is a file start with # and ends
with extension .h, which contains C Library function like stdio.h, conio.h, string.h, math.h etc...
The next part of a C program is the main () function. It is the entry point of a C program and
the execution typically begins with the first line of the main (). The body of the main method in
the C program refers to statements that are part of the main function. Pair of curly brackets
defines the body of a function. All functions must start and end with curly brackets.
Statements are the instructions given to the compiler. In C, a statement is always terminated by
a semicolon (;). In this particular case, we use printf () function to instruct the compiler to
display "Hello World" text on the screen.
Compile and Run C Program:
Step 1: Open turbo C IDE (Integrated Development Environment), click on File, and then click on
New
Step 2: Write a program, save the program by click on F2 key, give the name with the extension .C.
Step 3: Click on Compile menu and then on Compile option, or press the keys and press Alt + F9 to
compile the code.
Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs are first compiled to generate
the object code and then that object code is run.
Step 5: See the Output.
C- Programming
C TOKENS: The smallest individual units are known as tokens. C has the below types of tokens.
Character Set
Identifiers
Keywords
Data Types
Constants
Variables
Operators
Character set in C Language
• Upper case: A to Z
• Lowercase: a to z
• Digits: 0 to 9
• Special characters:! " # $ % & ' ( ) * + - . : , ; ` ~ = < > { } [ ] ^ _ \ /
Identifiers
In C programming, identifiers are the names used to identify variables, functions, arrays, structures, or
any other user-defined items. It is a name that uniquely identifies a program element and can be used
to refer to it later in the program.
Rules for Identifiers in C
An identifier can only have alphanumeric characters (a-z, A-Z, 0-9) and underscore (_) symbol.
Identifier names must be unique.
The first character must be an alphabet or underscore.
You cannot use a keyword as an identifier.
Only the first thirty-one (31) characters are significant.
It must not contain white spaces.
Identifiers are case-sensitive, for example a and A are different.
Keywords
Keywords are predefined words that have a special meaning in c language and they cannot be used for any
other purpose. C language has 32 keywords. Keywords cannot be used as identifiers.
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
Data Types
It refers the type of data and used to declare variable, constants, arrays, pointers, and functions.
C has the following 4 types of data types
Basic built-in data types: int, float, double, char
Enumeration data type: enum
Derived data type: pointer, array, structure, union
Void data type: void
There are two types of type qualifier in c
Size qualifier: short, long
Sign qualifier: signed, unsigned
Data Size Description Example
Type
int 2 or 4 Stores whole numbers, without decimals 1
bytes
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for 1.99
storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for 1.99
storing 15 decimal digits
char 1 byte Stores a single character/letter/number, or ASCII values 'A'
Format Specifier Data Type
%d or %i Int (-32768 to 32767), unsigned int (0 to 65535)
%f or %F float
%lf double
%c char ( -128 to 127), Unsigned char (0 to 255)
%s Used for strings (text), which you will learn more about in a later chapter
Constants
Constant is a value that cannot be changed during program execution. In C, any number, single
character, or character string is known as a constant.
This can be done with the const keyword, which makes a variable unchangeable and read-only.
const int BIRTHYEAR = 2007;
Variables
Variables are containers for storing data values, like numbers and characters. In C, there are different
types of variables (defined with different keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
float - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Characters are surrounded by single quotes
Syntax:
Variable declaration: Data type variable Name;
int myNum;
myNum = 15;
Variable assignment: Data type variable Name = value;
int myNum = 15;
Change Variable Values
If you assign a new value to an existing variable, it will overwrite the previous value.
Exe1:
int myNum = 15; // myNum value is 15
myNum = 10; // Now myNum value is 10
Exe2:
int myNum = 15;
int myOtherNum = 23;
// Assign the value of myOtherNum (23) to myNum
myNum = myOtherNum;
// myNum is now 23, instead of 15
printf("%d", myNum);
Variables Together
int x = 5;
int y = 6;
int sum = x + y;
printf("%d", sum);
Declare Multiple Variables
To declare more than one variable of the same type, use a comma-separated list:
Exe1:
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
Exe2:
int x, y, z;
x = y = z = 50;
printf("%d", x + y + z);