Chapter 1
Chapter 1
Chapter 1
Introduction to algorithmic
and programming in C
language
1
Chapter outcomes
• By the end of this chapter, the students will
• understand what an algorithm is used for and be able to tell
the differences between an algorithm and a program,
• be able to write a basic algorithm to solve a simple
problem,
• understand what a program is used for,
• learn what the compilation process consists of and the
different steps composing it.
2
Introduction
Computer Science
• Computer Science
• is not limited to the study of computers.
• is also the study of problems, problem-solving, and the
solutions that come out of the problem-solving
process.
3
Introduction
Problem solving phases
Analysis and specification
o Understand (define) the problem and what the
solution must do.
General solution
o Specify the required data types and the logical
sequence of steps that describe one possible solution
of the problem an algorithm
Verification
o Follow the steps exactly to see if the proposed
algorithm really does solve the problem.
Implementation
o Implement that algorithm in one (or more) 4
programming language a program
Introduction
From a problem to programs
5
What is an algorithm?
6
What is an algorithm?
• The word algorithm comes from the name of a Persian
mathematician Abu Ja’far Mohammed ibn-i Musa al
Khawarizmi.
7
Example of an algorithm
8
Characteristics of an algorithm
• An algorithm should have the following characteristics:
• Unambiguous
• Each of its steps (or phases), and their inputs/outputs
should be clear and must lead to only one meaning.
• Input
• It should have 0 or more well-defined inputs.
• Output
• It should have 1 or more well-defined outputs, and should
match the desired output.
• Finiteness
• It must terminate after a finite number of steps.
• Feasibility
• Should be feasible with the available resources.
• Independent 9
• An algorithm should have step-by-step directions, which
should be independent of any programming code.
How to write an algorithm
• There are NO well-defined standards for writing
algorithms
• Algorithms are NEVER written to support a particular
programming code.
• As we know that all programming languages share basic
code constructs like loops (do, for, while), flow-control
(if-else), etc. These common constructs can be used to
write an algorithm.
• We write algorithms in a step-by-step manner, but it is
not always the case.
10
Algorithm general structure
11
Example
• Problem − Design an algorithm to add two numbers and
display the result.
12
Example
• Problem − Design an algorithm to add two numbers and
display the result.
Algorithm ADD
BEGIN
1− read( a , b)
2−c←a+b
3 − write( c)
END
15
Creating and running a C program
• Compiling:
• The process of transforming a high level language (source
code (human readable)) into a low level language (machine
code or executable (computer language)).
18
Creating and running a C program
• The compiler
• ensures that your program is TYPE correct.
• For example, you are not allowed to assign a string to an
integer variable.
• ensures that your program is syntactically correct.
• For example, "x * y" is valid, but "x @ y" is not.
• does NOT ensure that your program is logically correct.
19