0% found this document useful (0 votes)
4 views

Algorithms

The document provides an overview of algorithms and flowcharts, defining algorithms as step-by-step procedures for problem-solving and flowcharts as graphical representations of these algorithms. It contrasts procedural programming and object-oriented programming, highlighting their key features and appropriate use cases. Additionally, it outlines the evolution of programming languages through five generations, detailing their characteristics and examples.

Uploaded by

rishitagupta2007
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)
4 views

Algorithms

The document provides an overview of algorithms and flowcharts, defining algorithms as step-by-step procedures for problem-solving and flowcharts as graphical representations of these algorithms. It contrasts procedural programming and object-oriented programming, highlighting their key features and appropriate use cases. Additionally, it outlines the evolution of programming languages through five generations, detailing their characteristics and examples.

Uploaded by

rishitagupta2007
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/ 12

Unit 2

Algorithms & Flowcharts


1. Algorithms
An algorithm is a step-by-step procedure to solve a problem or perform a task. It is
written in a human-readable format before being implemented in a programming
language.

Characteristics of a Good Algorithm:

• Unambiguous – Each step should be clear and precise.


• Input & Output – Should take zero or more inputs and produce at least one
output.
• Finiteness – Must terminate after a finite number of steps.
• Feasibility – Should be implementable with available resources.
• Language Independent – Should be written in a way that can be implemented
in any programming language.

Example: Algorithm to Add Two Numbers

1. Start
2. Read first number (A)
3. Read second number (B)
4. Compute Sum = A + B
5. Display Sum
6. Stop

Algorithm: Simple Interest Calculation


Step 1: Start
Step 2: Input the principal amount P
Step 3: Input the rate of interest R
Step 4: Input the time period T
Step 5: Calculate the simple interest using the formula:
SI = (P × R × T) / 100
Step 6: Display the value of SI
Step 7: End

2. Flowcharts
A flowchart is a graphical representation of an algorithm using symbols to denote
different operations.

Common Flowchart Symbols:

Example: Flowchart to Add Two Numbers

Co
Start


Read A


Read B


Sum = A + B


Display Sum


Stop

Flowchart: Simple Interest Calculation

Start

Input Principal (P)

Input Rate of Interest (R)

Input Time (T)

Calculate Simple Interest: SI = (P × R × T) / 100

Display SI

End
Differences Between Algorithm & Flowchart
Algorithm Flowchart
Text-based representation. Graphical representation.
Uses natural language or pseudocode. Uses symbols and arrows.
Easier to debug and analyze logic. Easier to visualize the flow of control.
No strict rules for writing. Follows standardized symbols.

4. When to Use Which?

• Use algorithms when:


o You need a clear, step-by-step explanation.
o You are working on complex logic before coding.
• Use flowcharts when:
o You want to visualize the program flow.
o Explaining the logic to non-programmers.

Procedural Programming vs. Object-Oriented


Programming (OOP)
Both Procedural Programming and Object-Oriented Programming (OOP) are
programming paradigms used to structure code, but they follow different approaches.
Below is a detailed comparison:

1. Procedural Programming (PP)


Procedural Programming is based on the concept of procedures (functions) that
operate on data.

• Programs are divided into functions (or subroutines).


• Follows a top-down approach (breaks problems into smaller functions).
• Data and functions are separate.

Key Features:

✔ Focuses on procedures/functions rather than data.


✔ Uses global data (variables can be accessed by any function).
✔ No access modifiers (like private, public).
✔ Less code reusability (functions are written for specific tasks).
✔ Examples: C, Pascal, BASIC, FORTRAN.

Example in C:
#include <stdio.h>

// Function to add two numbers (Procedural approach)


int add(int a, int b) {
return a + b;
}

int main() {
int num1 = 5, num2 = 10;
int sum = add(num1, num2);
printf("Sum: %d", sum);
return 0;
}

Explanation:

• add() is a function that performs an operation.


• Data (num1, num2) and functions (add()) are separate.

2. Object-Oriented Programming (OOP)


OOP is based on objects that contain data (attributes) and methods (functions).

• Programs are divided into classes & objects.


• Follows a bottom-up approach (builds small components first).
• Combines data + functions into a single unit (object).

Key Features:

✔ Encapsulation – Bundles data & methods into a class.


✔ Inheritance – Allows classes to inherit properties from other classes.
✔ Polymorphism – One method can behave differently in different contexts.
✔ Abstraction – Hides complex details and shows only essential features.
✔ Examples: Java, C++, Python, C#.
3. Key Differences
Procedural Programming Object-Oriented Programming
Feature
(PP) (OOP)
Approach Top-down (functions first) Bottom-up (objects first)
Data &
Separated Combined in a class (Encapsulation)
Functions
Reusability Low (functions are standalone) High (Inheritance & Polymorphism)
Security Less secure (global data) More secure (private data)
Complexity Good for small programs Better for large-scale applications
Examples C, Pascal, FORTRAN Java, C++, Python, C#

4. When to Use Which?


• Use Procedural Programming when:
o The program is small and simple.
o Performance is critical (e.g., embedded systems).
o No need for complex data structures.
• Use OOP when:
o The program is large and complex.
o Code reusability is important.
o Working with real-world entities (e.g., GUI apps, games, databases).

Generations of Programming Languages


Programming languages have evolved over time, categorized into five generations,
each improving abstraction, efficiency, and ease of use. Below is a breakdown:

1. First Generation (1GL) – Machine Language (1940s-


1950s)
• Description: Binary code (0s and 1s) understood directly by the CPU.
• Example:
binary
01001000 01101001 (Represents "Hi" in some architectures)
• Characteristics:
o Fast execution (no translation needed).
o Extremely difficult for humans to read/write.
o Machine-dependent (not portable).

2. Second Generation (2GL) – Assembly Language


(1950s-1960s)
• Description: Uses mnemonics (e.g., MOV, ADD) instead of binary.
• Example (x86 Assembly):
assembly

MOV AX, 5 ; Load 5 into register AX


ADD AX, 3 ; Add 3 to AX
• Characteristics:
o Slightly more human-readable than machine code.
o Requires an assembler to convert to machine code.
o Still hardware-specific.

3. Third Generation (3GL) – High-Level Languages


(1960s-Present)
• Description: English-like syntax, portable across machines.
• Examples:
o Procedural: C, Pascal, Fortran.
o OOP: C++, Java, Python.
• Example (C++):
int sum = 5 + 3; // Human-readable
• Characteristics:
o Requires a compiler/interpreter.
o Focus on logic rather than hardware.
o Used for most modern software (e.g., apps, OS).
4. Fourth Generation (4GL) – Very High-Level
Languages (1980s-Present)
• Description: Designed for specific domains (e.g., databases, reports).
• Examples:
o SQL: SELECT * FROM users;
o MATLAB: A = [1 2; 3 4]; (Matrix operations)
• Characteristics:
o Closer to human language.
o Reduces code volume (e.g., 10 lines of SQL ≈ 100 lines of C).
o Used in data analysis, business apps.

5. Fifth Generation (5GL) – AI & Natural Language


(1990s-Present)
• Description: Focuses on constraint-based and AI-driven programming.
• Examples:
o Prolog (Logic programming):

prolog

father(john, bob). % Fact


?- father(john, X). % Query: Who is John's child?
o AI Tools: ChatGPT (natural language to code).
• Characteristics:
o Solves problems using rules/constraints.
o Used in AI, machine learning, robotics.

Comparison Table
Generation Example Languages Key Features Use Case
1GL Machine Code Binary, hardware-specific Early computers
2GL Assembly Mnemonics, assembler Firmware, drivers
Generation Example Languages Key Features Use Case
3GL C, Java, Python Portable, high-level syntax Modern software
4GL SQL, MATLAB Domain-specific, minimal code Databases, analytics
5GL Prolog, AI tools Natural language, AI-driven AI, expert systems

C++ First Program


#include <iostream>

using namespace std;

int main() {

int a = 5, b = 3;

cout << "Sum: " << a + b;

return 0;

#include <iostream>

using namespace std;

int main() {

int a, b;

cout << "Enter two numbers: ";

cin >> a >> b;

cout << "Sum: " << a + b;

return 0;

}
This is a simple C++ program that calculates the sum of two numbers and prints the
result. Below is a line-by-line breakdown:

1. #include <iostream>
• Purpose: Includes the Input/Output Stream library, which allows the program to
use functions like cin (input) and cout (output).
• Example: Without this, cout and cin won’t work.

2. using namespace std;


• Purpose: Tells the compiler to use the standard namespace (std) so we don’t
have to write std::cout every time.
• Alternative: Without this, we’d need to write std::cout << "Sum: " << a + b;.

3. int main() { ... }


• Purpose: The main function is where program execution begins.
• int return type: Indicates the function returns an integer (usually 0 for success).

4. int a = 5, b = 3;
• Purpose: Declares two integer variables (a and b) and assigns them values
(5 and 3).
• Key Points:
o int means integer (whole numbers).
o Variables can be modified later (e.g., a = 10;).

5. cout << "Sum: " << a + b;


• Purpose: Prints the output to the console.
• Breakdown:
o cout → Standard output stream (prints to screen).
o << → Insertion operator (sends data to cout).
o "Sum: " → A string literal (text displayed as-is).
o a + b → Computes the sum of a and b (5 + 3 = 8).
• Output: Sum: 8
6. return 0;
• Purpose: Indicates successful program termination.

Full Program Flow


1. Starts at main().
2. Declares a = 5 and b = 3.
3. Computes a + b (which is 8).
4. Prints Sum: 8 to the console.
5. Exits with return 0.

Possible Modifications
1. User Input Version (Dynamic Values):
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << a + b;
return 0;
}
Now, the user can input any two numbers.
o
2. Using Functions (Modular Approach):
#include <iostream>
using namespace std;

int add(int x, int y) {


return x + y;
}

int main() {
int a = 5, b = 3;
cout << "Sum: " << add(a, b);
return 0;
}

#include <iostream> → Needed for input/output.


using namespace std; → Simplifies code (avoids std:: prefix).
int main() → Mandatory entry point.
cout << → Prints output.
return 0; → Indicates successful execution.

You might also like