Updated Chapter One - Introduction
Updated Chapter One - Introduction
Welcome to
Computer Programming
(CoSc1012)
Course Overview
1. Introduction
2. Basics of Programming
3. Control Statements
4. Function and Passing an Argument to a function
5. Arrays, Pointers and Strings
6. Structure
7. File
1
4/9/2023
Chapter One
Introduction
Introduction to Programming
Computer ?
an electronic device that accepts data, performs computations, and makes
logical decisions according to instructions that have been given to it.
then produces meaningful information in a form that is useful to the user.
Computer programs ?
Sets of instructions that control a computer’s processing of data
the instructions that tells the computer what to do
Computer programming ?
is the process of writing, testing, debugging / troubleshooting, and
maintaining the source code of computer programs.
2
4/9/2023
Introduction to Programming
• Computer source code is typically written by computer programmers.
• Source code is written in a programming languages.
• What is Programming Language?
Programming Language - a set of rules, symbols, and special words used to
construct a computer program.
computer programs. defined by
Syntactic - describes the possible combinations of symbols that form a
syntactically correct program
Semantic - The meaning given to a combination of symbols
computers do exactly what they are told to do
3
4/9/2023
Cont.
2. Assembly language
A program written in assembly language consists of a series of instructions mnemonics that correspond
to the executable instructions.
Assembly language instructions and their translation into zeros and ones differ from machine to machine.
machine language programming was simply too slow tedious for most programmers.
still hard for humans to understand on complex program: example ADD X Y Z
Any Assembly language program must be translated into machine language before the computer can
understand and follow the program.
Assembly language programs are translated into machine language by a program called an
assembler.
4
4/9/2023
Cont.
3. High-level languages
They are designed to be easy for human beings to write programs in and to be easy for human beings to read.
High level languages are the computer language in which it is much easier to write a program than the low
level language.
A program written in high level language is just like gibing instruction to person in daily life
closer to English easier to read and understand:
Low-level languages are closer to the language used by a computer, while high-level languages are close to
human languages.
FORTRAN, Pascal, BASIC, C, C++, Java, etc.
• Each type of computer only “understands” its own machine language (zeroes and
ones)
5
4/9/2023
What is Compiler?
• A compiler is a computer program that transforms code written in a high-level
programming language into the machine code.
• It is a program which translates the human-readable code to a language a
computer processor understands (binary 1 and 0 bits).
• The computer processes the machine code to perform the corresponding tasks.
• A compiler should comply with the syntax rule of that programming language
in which it is written.
• However, the compiler is only a program and can not fix errors found in that
program.
• So, if you make a mistake, you need to make changes in the syntax of your
program. Otherwise, it won’t compile.
What is Interpreter?
• An interpreter is a computer program, which converts each high-
level program statement into the machine code.
• This includes source code, pre-compiled code, and scripts.
• Both compiler and interpreters do the same job which is converting
higher level programming language to machine code.
• However, a compiler will convert the code into machine code (create
an exe) before program run.
• Interpreters convert code into machine code when the program is
run.
6
4/9/2023
Cont.
Cont.
• Compiler transforms code written in a high-level programming language into
the machine code, at once, before program runs,
• whereas an Interpreter converts each high-level program statement, one by one,
into the machine code, during program run.
• Which one is faster, compiler or interpreter? Reading assignment
• Compiler displays all errors after compilation, on the other hand, the Interpreter
displays errors of each line one by one.
• Compiler is based on translation linking-loading model, whereas Interpreter is
based on Interpretation Method.
• Compiler takes an entire program whereas the Interpreter takes a single line of
code
7
4/9/2023
Programming paradigm
It is a fundamental style of computer programming, a way of building the structure
and elements of computer programs.
Some programming languages are designed to follow only one paradigm, while others
support multiple paradigms
8
4/9/2023
Any given procedure might be called at any point during a program's execution,
including by other procedures or itself.
Cont.
We have a single program, which is divided into small pieces called procedures
9
4/9/2023
• Each object is capable of receiving messages, processing data, and sending messages to
other objects
• Modeling of the domain as objects so that the implementation naturally reflects the
problem at hand.
• Examples: C++,Java…C#..
10
4/9/2023
Cont.
11
4/9/2023
Algorithm
• An algorithm is defined as a step-by-step sequence of instructions that
must terminate and describe how the data is to be processed to produce the
desired outputs.
• Simply, algorithm is a sequence of instructions
• A procedure or formula for solving a problems.
• Often used for calculation, data processing and programming.
• Algorithms can be expressed in any language or language independent
Properties of Algorithms
• Finiteness: Algorithm must complete after a finite number of steps.
• Definiteness: Each step must be clearly defined, having one and only one
interpretation. At each point in computation, one should be able to tell exactly what
happens next.
• Sequence: Each step must have a unique defined preceding and succeeding step. The
first step (start step) and last step (halt step) must be clearly noted.
• Correctness: It must compute correct answer for all possible legal inputs.
12
4/9/2023
Cont.
• Completeness: It must solve the problem completely.
• Effectiveness: It must be possible to perform each step exactly and in a finite
amount of time.
• Efficiency: It must solve with the least amount of computational resources
such as time and space.
• Generality: Algorithm should be valid on all possible inputs.
• Input/Output: There must be a specified number of input values, and one or
more result values.
Cont.
• Algorithms can be expressed in many different notations, including natural
languages, pseudo code, flowcharts and/or programming languages.
13
4/9/2023
Cont.
• The algorithm and flowchart include following three types of control structures.
Pseudocode
• Is an artificial and informal language that helps programmers develop
algorithms.
14
4/9/2023
Pseudocode
▪ Pseudocode (which means fake code, because its not really programming
code) specifies the steps required to accomplish the task.
▪ Pseudocode is a type of structured English that is used to specify an
algorithm.
Advantages of Pseudocode
• Reduced complexity.
• Increased flexibility.
• Ease of understanding.
▪ The difference between algorithm and pseudocode is that an algorithm is
a step-by-step procedure developed to solve a problem, while a
pseudocode is a technique of developing an algorithm.
15
4/9/2023
Cont.
4. A computer can assign a value to a piece of data
3 cases
i. to give data an initial value Initialize, Set
ii. to assign a value as a result of some processing „ x=5+y
iii. to keep a piece of information for later use Save, Store
5. A computer can compare two piece of information and select one of two
alternative actions.
IF condition THEN
some action
ELSE
alternative action
ENDIF
16
4/9/2023
Cont.
6. A computer can repeat a group of actions
WHILE condition (is true)
some action
ENDWHILE
Cont.
17
4/9/2023
Example
Calculate CGP of five courses.
1. each course have Cr. Hr.
2. each course have grade point (A=4, B=3, C=2, D = 1)
3. total credit is the sum of credit hours
4. total grade point is the sum of Cr. Hr. multiplied by grade pt.
5. CGP is total grade point divided by total credit hour
18
4/9/2023
Exercise
1. Write pseudocode that checks whether the given number is odd or
even
Read number
If number % 2 = 0 THEN
Print number is even
ELSE
Print number is odd
ENDIF
19
4/9/2023
Flowchart
• The flowchart is a diagram which visually presents the flow of data through processing
systems.
• This means by seeing a flow chart one can know the operations performed and
the sequence of these operations in a system.
• A flowchart, will describe the operations (and in what sequence) are required to solve a
given problem
20
4/9/2023
Flowchart
• You can see a flow chart as a blueprint of a design you have made
for solving a problem.
Flowchart Symbols
21
4/9/2023
22
4/9/2023
Example
• Draw flow chart of an algorithm to add two
Algorithm description
Exercise
1. Draw a flowchart to determine a student’ s final grade and indicate whether it
is passing or failing.
2. Write an pseudocode and draw a flowchart that will read the two sides of a
rectangle and calculate its area.
23
4/9/2023
The End.
24