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

Programming Chapter 1

Chapter 1 introduces computer programming, defining a computer as a general-purpose machine that requires programming to perform tasks. It outlines the components of a computer (hardware and software), the programming process, and the importance of problem-solving in programming. Additionally, it discusses different programming languages, paradigms, and the software development life cycle, emphasizing the significance of algorithms and structured programming.

Uploaded by

zeki.mama21.21
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Chapter 1

Chapter 1 introduces computer programming, defining a computer as a general-purpose machine that requires programming to perform tasks. It outlines the components of a computer (hardware and software), the programming process, and the importance of problem-solving in programming. Additionally, it discusses different programming languages, paradigms, and the software development life cycle, emphasizing the significance of algorithms and structured programming.

Uploaded by

zeki.mama21.21
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 1 – Introduction to computer programming 2014

E.C

Chapter 1 that the computer has to follow in order to accomplish a


Introduction to computer programming certain task.

1.1. What is computer • For a computer to be able to do anything (start up, play

Computer is a general purpose electronic machine that is a song, run a word processor), it must first be given the

capable of programmed to do any kind of work. Computer is instructions to do so.

a dump machine & can’t do anything by itself unless it is • The instructions are written by humans in different

programmed by computer programmers. computer languages like C, C++, Java, etc.


• The computer does not understand these languages.
There are two components of a computer:
They must first be converted (compiled) to code that

 Hardware: refers to the physical components of your the machine can run (0101010101111)

computer such as the system unit, mouse, keyboard, 1.3. Reasons to study programming
monitor etc.  Increased capacity to express programming concepts
 Increased ability to learn new languages
 Software: is the instruction that makes the computer
 Understand the significance of implementation
work.  Increased ability to design new languages
1.2. What is computer programming  Mastering different programming paradigms(structures)
 To solve problems
Is the art of making computer do what you want to do or it is
writing a set of instruction to get the computer to perform
Problem solving
some task.
A computer program is simply a set of instruction to tell the
computer to perform the particular task or a set of instruction

1|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 Problem Solving: - is the process of “Problem identification is problem half solved”


transforming the description of a problem into  Also known as statement of the problem
the solution by using our knowledge of the  Before starting any part of the solution, we have to be
introduced with the problem. It is important to give a
problem domain and by relying on our ability to
clear & concise problem statement. You should have
select and use appropriate problem-solving full knowledge of the background of the problem you
strategies, techniques and tools. are attempting to solve.

 To facilitate the problem solving process we will


 Understand problem
use software engineering principle (software
 There are various methods to find these specifications,
development life cycle phase).
like interview, observations, and questioner or for
 The software development life cycle has the
simpler problems in written form.
following components.
2. Analysis (problem specification)
1. Preliminary investigation (problem
definition)  Also called problem analysis.

2. Analysis (problem specification)  Identification of input and expected output.

3. Design  In this phase you must become familiar with:

4. Implementation (coding) o What part of the problem is going to be solved?

5. Testing o What input data are needed to solve problem?


6. Maintenance &Documentation o What procedure is needed to achieve the result?
1. Preliminary investigation o What output are expected?
3. Design
 Also known as algorithm design.

2|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 In this phase we focus on “HOW” question. How can  Syntax rule: correct way of writing or expressing
we implement the new system to achieve system commands using one of programming languages that
objective or solution for the problem?
direct the control of the computer system.
 You will develop algorithm (Algorithm is a step by step
procedure for solving problems). 5. Testing
 There are different ways of describing of algorithm.  Also called program verification or debugging.
o Pseudo codes  The goal is to test the program for several
o Flow chart representative cases& ensure that the appropriate

4. Implementation or coding output is generated in each case.

 Algorithms cannot be executed (performed) directly by  Removing error in the program.

the computer. They must first be translated into  Error in the program is called bug and the process of

programming language, a process referred to as correcting known error is called

coding. It is expressing the solutions (in the previous Debugging.

step) in one of the various programming language.  Three types of error: -syntax error, run time error and

 The process of writing the program. logical error.

 Conversion of algorithm in to desired programming


language. 6. Maintenance and documentation
 This stage is ongoing throughout the life of the system.
 There are many high-level programming languages like
o Maintenance: Involves correcting faults which
BASIC, COBOL, Pascal, FORTRAN, C, C++, VB, etc.
are not detected during testing & making
To get our program work, we write it based on the
enhancement to satisfy new requirements.
syntax rules of the programming language.
 Correction the error during the use.

3|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 Upgrading the program.  It is the lowest and most elementary level of


o Documentation: It is compiling related programming language and was the first type of
programming language to be developed.
documents throughout the lifetime of the  Is basically the only language which computer can
program development. understand and represented inside the computer by a
string of binary digits (bits) 0 and 1.The symbol 0
There are two approaches of problem solving: stands for the absence of electric pulse and 1 for the
presence of an electric pulse.
o Top down design: is a systematic approach  A machine instruction contains two parts an operation
based on the concept that the structure of the code (op code) and an address. The OP code tells the
microprocessor system what operation it should
problem should determine the structure of the
perform, add, transfer, compare, or move data to
solution and what should be done in lower level. output device, etc. The address identifies the location
This approach will try to disintegrate a larger (memory, register) holding the required operands that
is, the data to be operated upon.
problem into more smaller and manageable
o Advantage of machine language
problems to narrow the problem domain.  It makes fast and efficient use of the
computer
o Bottom up design: is the reverse process
 It requires no translator to translate the
where the lowest level component are built first code i.e. directly understood by the
and the system builds up from the bottom until computer.
o Disadvantage of machine language
the whole process is finally completed.
 All operation code have to be remembered
 All memory address have to be
1.4. Types of computer languages
remembered
i) Machine language  It is hard to find errors in a program written
in the machine language

4|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 Are machine dependent i.e. a particular  Like machine language it is also machine
machine language can be used on only one dependent.
type of computer.  It requires translator for translation
process i.e. it requires more time.
ii) Low level language  Since it is machine dependent therefore
 It was developed to overcome some of the many programmer should have the knowledge of
inconveniences of machine language. the hardware also.
 Represents operations and operands in the form of iii) High level language
alphabetic symbols instead of 0’s and 1’s.  High level language gives formats close to English
 These alphabetic symbols will be known as mnemonic language
codes and can have maximum up to 5 letter  The purpose of developing high level languages is to
combination e.g. ADD for addition, SUB for subtraction, enable people to write program easily and in their own
START, LABEL etc. because of this feature it is also native language environment (English).
known as ‘symbolic programming language’.  High level language are basically symbolic languages
 This language is also very difficult and needs a lot of that use English words and mathematical symbols
practice to master it because very small English rather than mnemonic codes.
support is given to this language.  Each instruction in the high level language is translated
 The instruction of low level language will also be into many machine language instructions thus showing
converted to machine codes by translator to be one-to-many translation.
executed by the computer. o Advantage of high level language
Example: - assembly language  User friendly.
o Advantage of low level language  Similar to English with vocabulary of words
 It is easier to understand and use as and symbols.
compared to machine language. I.e. it is easier to learn.
 It is easy to locate and correct errors.  Require less time to write and easier to
 It is modified easily. debugged errors.
o Disadvantage of low level language  They are easier to maintain.

5|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 Problem oriented rather than ‘machine’ language and low-level language to machine code
based. program are called translators.
 Programs written in a high level language  The translators not only translate the instructions into
can be translated into many machine machine code but also it detects whether the program
language and therefore can run on any fulfills the syntax of the programming language. A
computer for which there exists an program passes through different stages before it
appropriate translator. carries out its function. First the program will be
 It is independent of the machine on which translate to object code (compilation time), then it will
it is used. I.e. programs developed in high be loaded to the memory and finally it will be executed
level language can be run on any (run time) or carries out its function.
computer.
o Disadvantage of high level language Language Translation and Utility Software
 A high level language has to be translated  Source code: high-level language instructions
into the machine language by a translator  Compiler: translates high-level code into machine
and thus a price in computer time is paid. language
Example: - FORTRAN, COBL, C, C++  Object code: translated instructions ready for computer
 For every modern language, there is a program that
Translation and execution takes the source code and produces object code in
machine language
 The only language that the computer understands is  There are three types of translators; assembler,
the machine language. Therefore any program that is interpreter, and compiler.
written in either low-level or high level language must Compiler - A set of programs that translate the source
be translated to machine code so that the computer code of higher-level software languages into machine
could process it.
language, or object code
 A program written in high-level or low-level language is
called source code program and, the translated  Translates source code once and produces a complete
machine code is called the object code program. machine language program
Programs that translate a program written in high level  Advantages - fast

6|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 Disadvantages - harder to write and debug 1. Unstructured programming


 Examples - Pascal, C,C++, FORTRAN, and open script 2. Structured programming
3. Object oriented programming
Assembler - An assembler is a software tool for
1. Unstructured programming
translating assembly language into machine language

Interpreter - An interpreter translates source code Unstructured programming refers to write small and simple
instructions, one instruction at a time, from a higher level program, which consists of only one main program. All the
language such as BASIC into machine language
action such as providing the input, processing and display
 It translates a line of source code into one or more lines output are done within one program only. This style of
of object code and then instructs the computer to
programming restricted for developing small application, but
perform those instructions until another line has to be
translated if the application become large it possess real difficulties in
 Advantages - fairly easy to write, and easy to debug terms of clarity of the code, modifiability and easy to use.
 Disadvantage - SLOW
 Examples - basic, lisp 2. Structured programming
 Process of writing a program in small, independent parts.
This makes it easier to control a program’s
Source code Translators development and to design and test its individual
Object code
component parts.
 Structured programs are built up from units called
1.5 Programming paradigms modules, which normally correspond to single
The major land marks in the programming world are the procedures or functions.
different kinds of features or properties observed in the  The most popular structured programming languages
development of programming language. Among those the include C, Ada, and Pascal.
following are worth mentioning:-
Structured programming can performed in to two ways,

7|Page
Chapter 1 – Introduction to computer programming 2014
E.C

1. Procedural programming: this programming has a (a) Procedural Programming


single program that is divided in to small segments
called procedures (also known as function, routines, Complete
and methods). From the main or controller procedure a
Application
Program file 1
procedure call is used to invoke the required
procedure.
2. Modular programming: the program coded within
procedural paradigm usually fit into a single code file
and it is meant for relatively smaller program. Program file 2Program file 1Program file 3

However, if the program gets large, then modular way


of programming is recommended
b) Modular programming

A program 3. Object-oriented programming(OOP)


Main  The idea behind OOP is that, a computer program is
File procedure
composed of a collection of individual units, or objects
as opposed to traditional view in which a program is a
list of instructions to the computer.
 OOP claimed to give more flexibility, easing changes to
Main programs. The OOP approach is often simpler to
procedure
develop and maintain.
Main Main
procedu procedure
Example of OOP: java, C++ and so on.
re

8|Page
Chapter 1 – Introduction to computer programming 2014
E.C

Algorithm representation • Sequence: Each step must have a unique defined


2.1 What is algorithm preceding and succeeding step. The first step (start
step) and last step (halt step) must be clearly noted.
 It is a finite set of well-defined rules (statements)
• Feasibility: It must be possible to perform each
for the solution of a problem in a finite number of
instruction.
steps.
 To design an algorithm for a problem first we • Correctness: It must compute correct answer for all
possible legal inputs.
break down the problem into simpler and
manageable tasks. For one problem there may be • Language Independence: It must not depend on
a lot of algorithms that help to solve, but the any one programming language.
algorithms that we select must be powerful, easy • Completeness: It must solve the problem
to maintain, and efficient (doesn’t take too much completely.
space and time). • Effectiveness: It must be possible to perform each
 Algorithm is a step by step procedure for solving step exactly and in a finite amount of time.
problems in terms of the action& the order in • Efficiency: It must solve with the least amount of
which these actions to be executed in a finite computational resources such as time and space.
number of steps.
 Experience has shown that the most difficult part
• Generality: Algorithm should be valid on all
possible inputs.
of solving a problem on computer is developing
an algorithm for the solution. • Input/Output: There must be a specified number of
input values, and one or more result values.
2.1.1 Properties of an algorithm
2.2 Introduction to flow chart and pseudo code
• Finiteness: Algorithm must complete after a finite
number of steps.  The most common ways to represent algorithms
• Definiteness: Each step must be clearly defined, include
having one and only one interpretation. At each i) Pseudo code
point in computation, one should be able to tell ii) Flow chart
exactly what happens next.
i) Pseudo code

9|Page
Chapter 1 – Introduction to computer programming 2014
E.C

 It is an artificial & informal language that will convert to Display an output prompt that explains the answer as the
structured programs for executed on computer. sum
 By making use language. Display the result

Pseudo code Structure Eg 1) A pseudo-code to calculate interest rate


ACCEPT Name, Principal, Rate
Input: READ, OBTAIN, GET and PROMPT Interest=Principal X Rate
Output: PRINT, DISPLAY and SHOW DISPLAY Name, Interest
A pseudo-code that calculates grade
Compute: COMPUTE, CALCULATE, and DETERMINE
ACCEPT Mark, Name
Initialize: SET and INTIALIZE IF Mark>80 Then
GradeA
Add one: INCREAMENT
ELSE IF Mark>=70 Then
Example: GradeB
ELSE IF Mark>=60 Then
Original Program Specification: GradeC
ELSE IF Mark>=50 Then
Write a program that obtains two integer numbers from the GradeD
user. It will print out the sum of those numbers. ELSE
GradeF
Pseudo code: ENDIF
DISPLAY Grade, Name
Prompt the user to enter the first integer Most algorithms incorporate three programming conventions
 Sequence
Prompt the user to enter a second integer
 Decision and
Compute the sum of the two user inputs  Repetition

10 | P a g e
Chapter 1 – Introduction to computer programming 2014
E.C

while converting the algorithm into a computer


Sequence program.
STOCK=STOCK+ QUANTITY

Decision Major flowchart symbols


IF HOURS>40
THEN
PERFORM OVERTIME
ELSE
PERFORM REGULAR
ENDIF

Repetition

INITIALIZE SUM TO ZERO


WHILE SUM<100
GET DATA
ADD DATA TO SUM
ENDWHILE
ii) Flow chart
 Consist of an ordered set of standard symbols
(geometrics shape) 2.2 How to write a pseudo code and draw a
Consist of an ordered set of standard symbols flow chart for a given algorithm
(geometrics shape) which represent operations, data
flow, input, output or equipment. Examples: Written algorithm description or pseudo code and draw
 It acts like a roadmap for a programmer guide him/her a flow chart for each of the following
on how to go from the starting point to the final point

11 | P a g e
Chapter 1 – Introduction to computer programming 2014
E.C

E.g2. Write an algorithm description and draw a flow chart to


check a number is negative or not.
Solution

I. Pseudo code
E.g3. Write an algorithm description and draw a flow chart to
Algorithm description. find the sum of n natural numbers.
I. Pseudo code
1. Start /begin
2. Read a number x
1. Start /begin
3. If x is less than zero write a message negative
2. Read a number N
4. else write a message not negative
3. Initialize i to 1 and sum to 0
5. End/Stop
4. If i less than or equal to N goto step 6
5. Else if goto step 9
6. sum = sum + i
7. i = i+1
8. goto 4
9. print sum
10.End/Stop

12 | P a g e
Chapter 1 – Introduction to computer programming 2014
E.C

Flowchart Calculate grade for ten students based on the scale:

13 | P a g e

You might also like