CSEN102 Introduction to Computer Science: Lecture 2

This lecture for CSEN102: Introduction to Computer Science, given by Milad Ghantous, PhD, focuses on algorithms, their properties, and representation.


2.1 What is an Algorithm?

An algorithm is a step-by-step procedure for solving a problem or accomplishing a task. It's a set of well-defined instructions that, when executed, will produce a desired output and terminate in a finite amount of time.

Key Properties of an Algorithm:


2.2 Why are Algorithms Important?

Algorithms are at the heart of computer science and programming. They provide a blueprint for how to solve computational problems. Without well-designed algorithms, even the fastest computers would be inefficient. They enable:


2.3 Representing Algorithms

Algorithms can be represented in various ways, each with its own advantages for clarity and precision:

1. Pseudocode:

A high-level description of an algorithm that uses a combination of natural language statements and programming-like constructs. It is not tied to a specific programming language, making it easy to understand for humans but not directly executable by computers.

Example: Algorithm to find the sum of two numbers

Algorithm SumTwoNumbers
    Input: Number1, Number2
    Output: Sum

    Step 1: Get Number1
    Step 2: Get Number2
    Step 3: Calculate Sum = Number1 + Number2
    Step 4: Display Sum
End Algorithm

2. Flowchart:

A graphical representation of an algorithm using standard symbols to depict the flow of logic, operations, and decisions. Flowcharts are good for visualizing the control flow but can become complex for large algorithms.

Example (Conceptual Flowchart for SumTwoNumbers):

A flowchart would visually connect these symbols: Start -> Input Number1 -> Input Number2 -> Process: Sum = Number1 + Number2 -> Output Sum -> End.

3. Structured English (or Natural Language):

Using clear, concise English statements to describe the steps of an algorithm. This is often the initial approach but can lack the precision needed for complex algorithms.

4. Programming Language Code:

The most precise representation, where the algorithm is written in a specific programming language (e.g., C++, Python, Java) that can be executed directly by a computer.


2.4 Sequential Algorithms

A sequential algorithm is one where instructions are executed in a strict, linear order, one after another, from top to bottom. There are no branches, loops, or conditional jumps in a purely sequential algorithm. Most simple programs start with a sequential flow.

Example:

The "SumTwoNumbers" algorithm above is a sequential algorithm.

Thank you