Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Programming Chapter 1

Uploaded by

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

Programming Chapter 1

Uploaded by

atakilti
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to Programming using C++ 2015

Chapter One: Introduction to Programming

1.1. General introduction to computer and programming


Computer is a machine that performs a variety of tasks according to specific instructions. It is a
data processing machine which accepts data via an input device and its processor manipulates the
data according to a program. The computer has two major components. The first one is the
hardware which is the tangible part of the computer. It is composed of electronic and mechanical
parts. The second major component is the software which is the intangible part of a computer. It
consists of data and the computer programs. Computers do what we tell them to do NOT what
we want them to do.

Computer Programming involves writing instructions and giving them to the computer to
complete a task. A computer program or software is a set of instructions written in a computer
language in order to be executed by a computer to perform a useful task.
Ex: Application software packages, such as word processors, spreadsheets and databases are
all computer programs
Computer Programmer is a person who translates the task you want a computer to do into a
form that a computer can understand.
Programming language refers to a series of specifically defined commands designed by human
programmers to give directions to digital computers. It is a means of communication between a
human being (programmer) and a computer. A programmer uses this means of communication in
order to give the computer instructions. A programming language is an artificial language that
can be used to control the behavior of a machine, particularly a computer. Programming
languages, like human languages, are defined through the use of syntactic and semantic rules, to
determine structure and meaning respectively. Programming languages differ from natural
languages in that natural languages are only used for interaction between people, while
programming languages also allow humans to communicate instructions to machines.

All programming language instructions must be expressed in binary code before the computer
can perform them.

Page 1 of 11
Introduction to Programming using C++ 2015

1.2. Levels of programming languages


A programming language is used by a human programmer to direct a computer to accomplish a
specific set of steps which lead to a desired outcome. Programming languages can be divided
into three major groups. They are: machine, assembly, and high-level.
1. Machine language: - is the lowest level programming language in which all instructions
and data are written in a binary machine code, i.e. 0s and 1s.
Characteristics:
 It is fast
 It is not easy to learn
 It is machine dependent
2. Assembly language: - is a low level programming language that uses abbreviations
rather than binary code. It is English-like abbreviations representing elementary computer
operations (ADD, MUL…).
Characteristics:
 It is faster to write instruction than machine language
 It is slower than machine language, because it needs language translator program
(assembler).
 It is not easy to remember (learn).
3. High level programming language: - allow programmer to write a program in a familiar
notation (English like statement). It is also called procedural language, i.e. programmer set
the precise procedures or sets of instruction. Example: FORTRAN, Basic, C++, Pascal, etc.
Characteristics:
 It is not machine dependent
 It is easy to understand
 It is slow

1.3. Program translation


Page 2 of 11
Introduction to Programming using C++ 2015

All programs must be translated before their instructions can be executed. Computer translation
languages can be grouped according to which translation process is used to convert the
instructions into binary code:
 Assemblers
 Interpreters

 Compilers

Assembler: a program used to translate Assembly language programs into machine language. It
produces one line of binary code per original program statement. The entire program is
assembled before the program is sent to the computer for execution.

Program in
Program in
Assembly Assembler Machine
language language

Interpreter: A program used to translate high-level programs into machine understandable


form. It translates one line of the program into binary code at a time.

Compiler: a program used to translate high-level programs. It translates the entire program into
binary code before anything is sent to the CPU for execution.

Program in
High level Program in
language Compiler Machine
language

Page 3 of 11
Introduction to Programming using C++ 2015

1.4. SDLC (Software Development Life Cycle)


As with most undertakings, planning is an important factor in determining the success or failure
of any software project. Most professional software developers plan a software project using a
series of steps generally referred to as the software development life cycle (SDLC). SDLC of a
given system usually involves:
 Problem investigation
 System analysis
 System design
 System implementation and
 System Maintenance
You should realize, however, that all of the activities involved are highly related and
interdependent. Therefore, in actual practice, several developmental activities can occur at the
same time.

The table below (table 1.1) shows the detail of activities performed in each of the above mentioned
steps.

Step Jobs to be done Product

 Determine whether a business


problem or opportunity exists
 Conduct a feasibility study to
System/problem determine whether a new or Feasibility study
Investigation improved information system is a report
feasible solution
 Develop a
project management plan and get
management approval.

Page 4 of 11
Introduction to Programming using C++ 2015

 Analyze the information needs of


end users, the organizational
Functional
environment, and any system
Requirement report
System Analysis presently used.
 Develop the functional requirement
of a system that can meet the needs
of the end users.
 Develop specifications for the
hardware, software, people, network
System specification
and data resources, and the
System Design information products that will satisfy
the functional requirements of the
proposed plan
 Acquire (develop) hardware and
software
 Test the system and train people to
System Implementation operate and use it Operational system
 Convert to the new system
 Provide ongoing support
 Use a post implementation review to
System Maintenance Improved and/or
monitor, evaluate, and modify the
fully functional
system as needed.
system

Table 1.1 Steps in SDLC

1.5. Algorithm development and representation


Page 5 of 11
Introduction to Programming using C++ 2015

What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a


problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.
 It is a list of instructions specifying a precise description of a step by step process
that terminates after a finite number of steps for solving a problem, producing the
correct answer in the end.
 It is a recipe for solving problems.
 A finite set of an instruction that specifies a sequence of operation to be carried out
in order to solve a specific problem.
 An unambiguous procedure specifying a finite number of steps to be taken.
 It is precise and independent of specific programming languages.
 Is a procedure or formula for solving a problem.

Methods of Specifying Algorithm


 Finiteness: - there is an exact number of step to be taken and has an end.
 Correctness: - it must be correct and must solve the problem for which it is
designed.
 Absence of Ambiguity:- means that every instruction is precisely described and
clearly specified. . Every step in an algorithm must be clear as to what it is
supposed to do and how many times it is expected to be executed.
 Generality:- i.e. it should have to be general.
 Sequence of Execution - instructions are performed from top to bottom.
 Input and Output - defined the unknowns of the problem is specified and with the
expected outcome.
 Effectiveness - the solution prescribed is guaranteed to give a correct answer and
that the specified process is faithfully carried out.
 Scope Definition - applies to a specific problem or class of problem.

 Algorithms are usually represented in pseudo code or by a flow chart.

Page 6 of 11
Introduction to Programming using C++ 2015

Pseudo code: is English like language for representing the solution to a problem. Pseudo code
is independent of any programming language. Pseudo code (or a flow chart) is the first step in
the process of planning the solution to a problem (also called developing an algorithm).
Flow chart: is a graphical way of representing the solution to a problem. The symbols used in
flow charts are shown in table 1.2 below.

1.5.1. Flow charts


A flowchart is a schematic representation of an algorithm or a process. The advantage of flowchart is it
doesn’t depend on any particular programming language, so that it can used, to translate an algorithm
to more than one programming language. Flowchart uses different symbols (geometrical shapes) to
represent different processes. The following table shows some of the common symbols.

Symbol Name Meaning


Indicates where the flowchart begins and ends. It shows the
Terminal entry point of your flowchart and the exit point. It is useful to
represent an end result of a flow process.
Indicates an input or output operation
Input/output

Indicates computation or data manipulation. It shows


Process something that has to be done or an action that has to be taken.
The text in the rectangle almost always includes a verb.
Used to connect the flowchart symbols and indicate the logic
Flow lines flow. The lines with arrows determine the flow through the
chart.
Indicate a program branch point. A decision asks a question.
The answer to the question determines which arrow you
Decision follow out of the decision shape. Decisions can take place at
points where results need to be evaluated before the process
may continue.
Indicates an entry to, or exit from, another part of the flow
Connector
chart

Table 1.2 Symbols used in flow charts


1.5.2. Pseudo-code
Pseudo-code is an alternative to flow charts which uses plain(ish) language, to describe an
algorithm. Unlike flow charts, there are no real rules as to how to write pseudo-code, other than

Page 7 of 11
Introduction to Programming using C++ 2015

that you need to clearly indent (or bracket) sections of code that belong inside a conditional
statement or loop, and you need to be consistent about the wording you use. No standard for
pseudocode syntax exists, as a program in pseudocode is not an executable program.

The purpose of using pseudocode is that it may be easier for humans to read than conventional
programming languages, and that it may be a compact and environment-independent generic
description of the key principles of an algorithm. Writing pseudocode will save you time later
during the construction & testing phase of a program's development.

Example 1: -Adding two numbers: When you are asked to add numbers, what you know for sure
is that addition operation involves two operands at a given time. The first step is to have the two-
operand values. Then we add these operands and store the result to another operand. This is just
a description. To check if this works lets assume the following.

 Input; - x, y
 Process: Compute the arithmetic sum of the two numbers, put result on variable
 Output: display the sum of two numbers

Flowchart Pseudo code

1. Begin
Start
2. Read X, Y
Read X,Y 3. Calculate Sum

3.1. Formula:
Sum= X+Y
Sum=X+Y

4. Display Sum
Display Sum

5. End

Stop Structures
Basic Control

Page 8 of 11
Introduction to Programming using C++ 2015

In general speaking, the problems in programming can have either of these structures: sequence,
selection and repetition.

 A sequence is a series of statements that execute one after another


 Selection (branch) is used to execute different statements depending on certain conditions.
 Repetition (looping) is used to repeat statements while certain conditions are met.

Example 2: Average of three numbers


Flowchart Pseudo code

Start 1. Begin

2. Input a, b, c
Input a,b, c
3. Calculate Sum

3.1. Formula:
Sum= a+b+c
Sum=a+b+c

Average= Sum/3 4. Calculate Average

4.1. Formula:
Display Average
Average=Sum/3

5. Display Average
Stop
6. End

Example 3: larger of two numbers


Flowchart Pseudo code
Page 9 of 11
Introduction to Programming using C++ 2015

Start 1. Begin

2. Read a,b
Read a,b
3. If a>b
No Print a
Is
a>b 4. Else
Yes
Print b
Print a Print b
5. End

Stop

Page 10 of 11
Introduction to Programming using C++ 2015

Example 4: sum of the first 10 natural numbers

Flowchart Pseudo code


1. Begin

2. Set Sum=0, N=0


Start
3. Increment N by 1
N=0
4. Calculate sum
Sum=0
Sum= Sum + N
N=N+1
5. While N<10 goto
Sum=0
step3
Sum=Sum + N

Sum=0 6. Print Sum


Yes
7. End
Is
N<10 Exercise

Problem 1: Develop an algorithm to calculate the area of a circle


and display the result.

Print Sum No Problem 2: Draw a flowchart to find the smallest of three


numbers A, B and C.

Problem 3: Draw a flow chart to converts the input Fahrenheit


Stop degree into its Celsius degree equivalent. Use the
formula: C= (5/9)*F-32.
Page 11 of 11

You might also like