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

Updated Chapter One - Introduction

The document outlines an introduction to computer programming course, beginning with an overview of the topics to be covered in the course. It then provides details on an introductory chapter that defines computers and computer programs, and explains that programming involves writing instructions that control a computer's processing of data. The chapter also discusses different types of computer languages like machine language, assembly language, and high-level languages. It explains the differences between compilers and interpreters and how they translate code into machine-readable format. Finally, it briefly introduces procedural programming as a fundamental programming paradigm.

Uploaded by

wubie derebe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views

Updated Chapter One - Introduction

The document outlines an introduction to computer programming course, beginning with an overview of the topics to be covered in the course. It then provides details on an introductory chapter that defines computers and computer programs, and explains that programming involves writing instructions that control a computer's processing of data. The chapter also discusses different types of computer languages like machine language, assembly language, and high-level languages. It explains the differences between compilers and interpreters and how they translate code into machine-readable format. Finally, it briefly introduces procedural programming as a fundamental programming paradigm.

Uploaded by

wubie derebe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

4/9/2023

Welcome to

Computer Programming
(CoSc1012)

April 9, 2023 Elias Debelo, Haramaya University 1

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

April 9, 2023 Elias Debelo, Haramaya University 2

1
4/9/2023

Chapter One
Introduction

April 9, 2023 Elias Debelo, Haramaya University 3

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.

April 9, 2023 Elias Debelo, Haramaya University 4

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

April 9, 2023 Elias Debelo, Haramaya University 5

Reason to study computer programming

April 9, 2023 Elias Debelo, Haramaya University 6

3
4/9/2023

Types and usage of computer languages


• There are many languages for writing programs. But they can be categories broadly into three
categories.
1. Machine language(binary language)
 The most elementary and first type of computer language invented, was machine language.
 Machine language is a collection of binary digits or bits that the computer reads and
interprets.
 Machine language was machine dependent means different computers understand different
sequences.
 It is computer’s native language, sequence of zeroes and ones (binary)
 Machine languages are the only languages understood by computers.
 Might be hard for humans to understand: 01010001

April 9, 2023 Elias Debelo, Haramaya University 7

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.

April 9, 2023 Elias Debelo, Haramaya University 8

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.

April 9, 2023 Elias Debelo, Haramaya University 9

Interpreting vs Compiling program


• Any high-level language program must be translated into machine language before
the computer can understand and follow the program.

• Each type of computer only “understands” its own machine language (zeroes and
ones)

• Compiler: is to transform a program written in a high level programming


language from source code into object code.

• Interpreter: is a program that executes instructions written in a high-level


language. They do the same thing but interpreter works each statement at a time.
April 9, 2023 Elias Debelo, Haramaya University 10

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.

April 9, 2023 Elias Debelo, Haramaya University 11

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.

April 9, 2023 Elias Debelo, Haramaya University 12

6
4/9/2023

Cont.

April 9, 2023 Elias Debelo, Haramaya University 13

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

April 9, 2023 Elias Debelo, Haramaya University 14

7
4/9/2023

Problem Solving Techniques

April 9, 2023 Elias Debelo, Haramaya University 15

Programming paradigm
 It is a fundamental style of computer programming, a way of building the structure
and elements of computer programs.

 Capabilities and styles of various programming languages are defined by their


supported programming paradigms;

 Some programming languages are designed to follow only one paradigm, while others
support multiple paradigms

• Programming paradigms that are often distinguished include declarative, Procedural


and object-oriented programming

April 9, 2023 Elias Debelo, Haramaya University 16

8
4/9/2023

Procedural (imperative) programming


 Based upon the concept of procedure call and top down approach. A procedure call is used
to invoke the procedure

 Procedures (routines, subroutines, methods, functions) simply contain a series of


computational steps to be carried out to solve a problem.

 Any given procedure might be called at any point during a program's execution,
including by other procedures or itself.

 Procedural programming is a list or set of instructions telling a computer what to do


step by step and how to perform from the first code to the second code.

April 9, 2023 Elias Debelo, Haramaya University 17

Cont.
We have a single program, which is divided into small pieces called procedures

Procedural programming languages include C++, FORTRAN, Pascal, and BASIC.

April 9, 2023 Elias Debelo, Haramaya University 18

9
4/9/2023

Declarative (Functional) Programming


• Is when you write your code such a way that is describes what you want to do not how
you want to do it. It needs to perform logic.

• Program describes logic rather than control flow

• Program describes “what” rather than “how”.


• Aims for correspondence with mathematical logic

• Automatic reasoning by applying inference rules

• SQL , Markup languages, Lisp, Prolog.


• Functional programming is becoming popular paradigm now days.

April 9, 2023 Elias Debelo, Haramaya University 19

Object Oriented Programming


• Is a method of implementation in which programs are organized as cooperative
collections of objects.

• Data and operations are grouped together

• 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#..

April 9, 2023 Elias Debelo, Haramaya University 20

10
4/9/2023

Cont.

April 9, 2023 Elias Debelo, Haramaya University 21

Problem Solving Techniques


• A computer is a useful tool for solving a great variety of problems. To make a computer do
anything (i.e. solve a problem), you have to write a computer program.
• In a computer program you tell a computer, step by step, exactly what you want it to do. A computer
program is written in order to solve a problem.
• A solution to a problem actually consists of two things:
▪ A way to organize the data
▪ Sequence of steps to solve the problem
• The way data are organized in a computer’s memory is said to be Data Structure
• The sequence of computational steps to solve a problem is said to be an algorithm
• As a result program is the nothing it is the operations (Algorithms) plus operands (data structures).

April 9, 2023 Elias Debelo, Haramaya University 22

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

April 9, 2023 Elias Debelo, Haramaya University 23

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.

• Feasibility: It must be possible to perform each instruction.

• Correctness: It must compute correct answer for all possible legal inputs.

April 9, 2023 Elias Debelo, Haramaya University 24

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.

April 9, 2023 Elias Debelo, Haramaya University 25

Cont.
• Algorithms can be expressed in many different notations, including natural
languages, pseudo code, flowcharts and/or programming languages.

• Pseudo code is an informal high-level description of the operating principle of a


computer program or other algorithms. It is not an executable program.

• A flowchart is a type of diagram that represents an algorithm, workflow or


process, showing the steps as boxes of various kinds, and their order by
connecting them with arrows.

April 9, 2023 Elias Debelo, Haramaya University 26

13
4/9/2023

Cont.
• The algorithm and flowchart include following three types of control structures.

• or programs could be written in terms of 3 structures:

• Sequence – which instruction should be done next?


• Selection – select between options

• Repetition – repeat an action while a given condition stays true

April 9, 2023 Elias Debelo, Haramaya University 27

Pseudocode
• Is an artificial and informal language that helps programmers develop
algorithms.

• A shorthand notation for programming which uses a combination of


informal programming structures and verbal descriptions of code.

• In general, pseudocode is used to outline a program before translating it


into proper syntax.

• Pseudocode is very similar to everyday English.

April 9, 2023 Elias Debelo, Haramaya University 28

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.

April 9, 2023 Elias Debelo, Haramaya University 29

Why is Pseudocode Necessary?


The programming process is more complex than the pseudocode.
▪ You must first understand the program specifications.
▪ Then you need to organize your thoughts and create the program.
▪ You must break the main tasks that must be accomplished into
smaller ones in order to be able to eventually write fully developed
code.
▪ Writing Pseudocode will save your time later during the
construction & testing phases of a program's development.

April 9, 2023 Elias Debelo, Haramaya University 30

15
4/9/2023

How to Write Pseudocode Statements?


There are six basic computer operations
1. Start with the statement of a pseudo code which establishes the main goal or
the aim
2. A computer can receive information, Read (information from a file) to Get
(information from the keyboard).
3. A computer can put out information, Write (information to a file) to Display
(information to the screen)
4. Each step of the algorithm is written on a line of its own, in sequence.
5. A computer can perform arithmetic. Use actual mathematical symbols or the
words for the symbols
Example: Add number to total Total = total + number
+, -, *, / Calculate, Compute also used

April 9, 2023 Elias Debelo, Haramaya University 31

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

April 9, 2023 Elias Debelo, Haramaya University 32

16
4/9/2023

Cont.
6. A computer can repeat a group of actions
WHILE condition (is true)
some action
ENDWHILE

FOR a number of times


some action
ENDFOR

April 9, 2023 Elias Debelo, Haramaya University 33

Cont.

April 9, 2023 Elias Debelo, Haramaya University 34

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

April 9, 2023 Elias Debelo, Haramaya University 35

Example: Verbal Description


1. Set total credit hour to zer0
2. Set total grade point to zero
3. Set course counter to 1
4. While course counter is less or equal to number of course
5. Calculate course grade point
6. Add course grade point to total grade point
7. Calculate total credit hour
8. Calculate CGPA by dividing total grade point by total credit
9. Print CGPA

April 9, 2023 Elias Debelo, Haramaya University 36

18
4/9/2023

Example: the pseudocode


This program will allow the user to calculate cumulative grade point (CGPA) for 5 courses
Set totalCredit = 0
Set totalGradePoint = 0
Set courseCounter = 1
WHILE courseCounter ≤ 5 DO
Read Course
courseGradePoint = courseCredit * courseGrade
totalGradePoint = totalGradePoint + courseGradePoint
totalCredit = totalCredit + courseCredit
courseCounter = courseCounter + 1
ENDWHILE
CGP = totalGradePoint / totalCredit
Print CGP

April 9, 2023 Elias Debelo, Haramaya University 37

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

April 9, 2023 Elias Debelo, Haramaya University 38

19
4/9/2023

Check if the work day is Wednesday


Read day
IF day = Monday
Print day is Monday
ELSE IF day = Tuesday
Print day is Tuesday
ELSE IF day = Wednesday
Print Yes It Is Wednesday
ELSE
Print It is day

April 9, 2023 Elias Debelo, Haramaya University 39

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.

• Algorithms are nothing but sequence of steps for solving problems.


• So a flowchart can be used for representing an algorithm.

• A flowchart, will describe the operations (and in what sequence) are required to solve a
given problem

April 9, 2023 Elias Debelo, Haramaya University 40

20
4/9/2023

Flowchart

• You can see a flow chart as a blueprint of a design you have made
for solving a problem.

• when you have a problem to solve using computer or in other word,


you need to write a computer program for a problem then it will be
good to draw a flowchart prior to writing a computer program.

• Flowchart is drawn according to defined rules.

April 9, 2023 Elias Debelo, Haramaya University 41

Flowchart Symbols

April 9, 2023 Elias Debelo, Haramaya University 42

21
4/9/2023

General Rules of flowcharts


1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no other entry points.
The exit point for all flowchart symbols is on the bottom.
3. The Decision symbol has two exit points.
4. Generally a flowchart will flow from top to bottom.
5. Connectors are used to connect breaks in the flowchart.
Examples are:
• From one page to another page.
• From the bottom of the page to the top of the same page.
6. Subroutines and Interrupt programs have their own and independent flowcharts.

April 9, 2023 Elias Debelo, Haramaya University 43

Flowchart: Decision and Iteration Structure

April 9, 2023 Elias Debelo, Haramaya University 44

22
4/9/2023

Example
• Draw flow chart of an algorithm to add two

numbers and display their result.

Algorithm description

1. Read the two numbers (A and B)


2. Add A and B
3. Assign the sum of A and B to C
4. Display the result ( c)

April 9, 2023 Elias Debelo, Haramaya University 45

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.

April 9, 2023 Elias Debelo, Haramaya University 46

23
4/9/2023

The End.

April 9, 2023 Elias Debelo, Haramaya University 47

24

You might also like