0% found this document useful (0 votes)
52 views62 pages

Fund OfProg in-C++Chp 1

This document provides an overview of computers and computer programming. It discusses the evolution of programming languages from low-level machine languages and assembly languages to modern high-level languages like C++. It describes the basic components of a computer system, including hardware like the CPU, memory, and input/output devices, as well as software like operating systems and application programs. It explains how high-level programming languages are translated into machine-readable code using compilers and interpreters. The goal is to introduce fundamental concepts needed to learn the C++ programming language.

Uploaded by

kemal Beriso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views62 pages

Fund OfProg in-C++Chp 1

This document provides an overview of computers and computer programming. It discusses the evolution of programming languages from low-level machine languages and assembly languages to modern high-level languages like C++. It describes the basic components of a computer system, including hardware like the CPU, memory, and input/output devices, as well as software like operating systems and application programs. It explains how high-level programming languages are translated into machine-readable code using compilers and interpreters. The goal is to introduce fundamental concepts needed to learn the C++ programming language.

Uploaded by

kemal Beriso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Oromia State University

College of Science and Technology


Department of IT

Fundamentals of Programming

Chapter 1: Problem solving using computers

Lecture Note
Overview of Computers and Computer Programming
 Overview of Computer Systems
 The Evolution of Programming Languages

◦ Processing a C++ Program


◦ Programming Methodologies
 Problem Solving Concepts
◦ Problem Solving Methodologies
◦ Top-Down Design, Problem breakdown and
abstraction
◦ Algorithmic Structure
 Software Crisis
2
Introduction

 Many people use the Internet to look for information and to


communicate with others. This is all made possible by the
availability of different software, also known as computer programs.
Without software, a computer is useless. Software is developed by
using programming languages.
 The programming language C++ is especially well suited for
developing software to accomplish specific tasks.

 The main objective of this course is to help you learn how to write
programs in the C++ programming language. Before you begin
programming, it is useful to understand some of the basic
terminology and different components of a computer. We begin with
an Overview of Computers and Computer Programming.

3
What is Computer ?
A Computer is an electronic device that
- accepts data,
- performs computations, and
- makes logical decisions according to instructions that have
been given to it.

Then it produces meaningful information in a form that is


useful to the user.
 They have been deployed to solve different real life

problems, from the simplest game playing up to the


complex nuclear energy production.
 Computers are important and widely used in our society

because they are cost-effective aids to problem solving in


business, government, industry, education, etc. 4
Overview of Computer Systems

Hardware & Software

Computer Systems- is a combination of hardware and software


Hardware- is a visible part or physical component of computer where
as software- is invisible part or a set of instruction that control an
overall activities of a computer how to do and what to do.

5
Hardware
 Four components of a computer system:
◦ CPU - central processing unit
 Makes decisions, performs computations, and
delegates input/output requests
◦ Memory: Disk Drives, CD drives, Tape drives,
USB flash drives.
 Stores information
◦ Input devices: Keyboard, Mouse,
 Gets information from the user to the computer
◦ Output devices: monitor
 Sends information from computer to the user

6
Hardware …

7
Hardware …

M em ory

Input O utput
D evices D evices

C PU

8
Software

S o ftw a re

S yste m A p p lica tio n


S o ftw a re S o ftw a re
9
Software …
 Application software
◦ Easy-to-use programs designed to perform specific tasks
 System software

◦ Programs that support the execution and development of


other programs
◦ Two major types
 Operating systems
 Translation systems (compilers & linkers)

10
Computer Software Relationships
User Interface Application Programs

User Interface Operating System

User Basic Input and Output Services (BIOS)


Interface • needed for a computer to boot up

Computer Hardware

11
Application Software
 Application software makes computer popular and easy to
use
 Common application software:

Microsoft Word, WordPerfect


PowerPoint
Netscape, Internet Explorer
PhotoShop, Photo-Paint
Quick Time
Dreamweaver

12
System Software: Operating System
 Controls and manages the computing
resources
 Examples
◦ Windows, Unix, MSDOS,
 Important services that an operating system
provides:
◦ Security: prevent unauthorized users from
accessing the system
◦ Commands to manipulate the file system
◦ Input and output on a variety of devices
◦ Window management

13
What is a programming language?
A sequence of instructions

An algorthm A program
(in human language) (in computer language)

 A program needs to be written in a language


 There are many programming languages

◦ Low-level, understandable by a computer


◦ High-level, needs a translator!
 C++ is a high level programming language

14
Evolution/Generation of Programming Languages
 Programming languages are categorized into five
generations: (1st, 2nd, 3rd, 4th and 5th generations)
 These programming languages can also be categorized into

two broad categories: low level and high level languages.


◦ Low level languages- are machine specific or dependent. (1st & 2nd )
◦ High level languages like, COBOL, BASIC, C, C++, JAVA are
machine independent and can run on variety of computers. (3rd, 4th
& 5th )
 Easier to understand and to use by programmers.
 Languages after the forth generation are refereed to as a
very high level languages.

15
Low level languages
 First Generation (Machine languages, 1940’s)
◦ Difficult to write applications with.
◦ Dependent on machine languages of the specific computer being used.
◦ Allow the programmer to interact directly with the hardware, and it can
be executed by the computer without the need for a translator.
◦ Is more powerful in utilizing resources of the computer.
◦ Gives power to the programmer.
◦ execute very quickly and use memory very efficiently.
 Second Generation (Assembly languages, early 1950’s)
◦ Uses symbolic names for operations and storage locations.
◦ A system program called an assembler translates a program written in
assembly language to machine language.
◦ Programs written in assembly language are not portable. i.e., different
computer architectures have their own machine and assembly
languages.
◦ They are highly used in system software development.
16
High Level Languages
 3rd Generation (1950’s to 1970’s):
◦ Uses English like instructions and mathematicians were able
to define variables with statements such as Z = A + B
◦ Such languages are much easier to use than assembly
language.
◦ Programs written in high level languages need to be
translated into machine language in order to be executed.
◦ The use of common words (reserved words) within
instructions makes them easier to learn.
◦ They are procedural languages.
 In procedural languages, the programmer is expected to
specify what is required and how to perform it
Example: PASCAL, C, C++
17
High Level Languages …
 Fourth Generation (since late 1970’s):
◦ They have a simple, English like syntax rules; commonly
used to access databases.

◦ They are object oriented languages (non-procedural languages)


◦ The programmer have less control over how each task is actually
performed.

◦ Programmers concentrate on defining the input and output rather


than the program steps required.
◦ They have a minimum number of syntax rules.
◦ Some examples of 4GL are structured query languages (SQL),
report generators, application generators, JAVA, C++,

18
High Level Languages …
 Fifth Generation (1990’s):
◦ These are used in artificial intelligence (AI) and expert systems;
also used for accessing databases.

◦ They are “natural” languages whose instruction closely


resembles human speech. E.g. “get me Almaz’s grade averages
for the 2005 academic year”.

◦ They require very powerful hardware and software because


of the complexity involved in interpreting commands in human
language.

19
An example:
Machine binary language Low-level assembly High-level

20
High Level Languages …
 Translator: a program that converts one or more
languages to another language. There are three types of
translators, namely assembler, Compiler & interpreter.
◦ Compiler: a program that translates source program into object
code
◦ Examples: C, C++, Visual Basic, etc.
 Assembler: a program that translates assembly languages into
machine code.
◦ Interpreters: another type of program that translates source
program into machine code. translate each statement and execute
it immediately.
◦ Examples: BASIC, JAVA, etc.

21
How to translate?
A program written in high-level programming language
(for example, C++ program)

COMPILER (for example, Dev)

A low-level (machine language) program that is


understandable by a computer (for example, a
PC)
Examples of compilers:
◦ Microsoft Visual C++, Eclipse, g++, Dev C++

22
Translation System
 Set of programs used to develop software
 Types of translators:

Compiler
Linker
 Examples

◦ Microsoft Visual C++, Eclipse, g++

23
Processing a C++ Program: Software Development Process
 Major activities

Editing (writing the program)


Compiling (creates .obj file)
Linking with compiled files
(creates .exe file) Source Program

 Object files
Compile
 Library modules Library routines
Loading and executing Edit Link

Testing the program Other object files


Think Load

Execute
24
Integrated Development Environments(IDE)
 Combine all of the capabilities that a programmer would
want while developing software (VC++ 2008, Eclipse, Dev
C++)
Editor
Compiler
Linker
builder
Loader
Debugger
Viewer

25
Programming with the Problem Analysis–Coding–Execution Cycle

26
Software crisis
 Software crisis is a term used in the early days of computing science
for the difficulty of writing useful and efficient computer programs in
the required time.
 The causes of the software crisis were linked to the overall
complexity of hardware and the software development process.
 The crisis manifested itself in several ways:
Projects running over-budget
Projects running over-time
Software was very inefficient
Software was of low quality
Software often did not meet requirements
Projects were unmanageable and code difficult to maintain
Software was never delivered

27
Software crisis...
 Various processes and methodologies have been developed over the last
few decades to improve software quality management such as procedural
programming and object-oriented programming. However, software
projects that are large, complicated, poorly specified, or involve
unfamiliar aspects, are still vulnerable to large, unanticipated problems.
 What are the example of software crises?
In 1996, Arian-5 space rocket, developed at the cost of $7000 million
over a period of 10 years was destroyed within less than a minute after
its launch. The crash occurred because there was a software bug in the
rocket guidance system.
 General issues that affect most software:
◦ Heterogeneity,
◦ Business and social change.
◦ Security and trust
◦ Scales
28
Programming Methodologies /Paradigms
 There are three main programming paradigms exist:
◦ Procedural Programming.
 It is a programming paradigm based upon the concept of procedure call.
 Possible benefits: the ability to re-use the same code (function or
procedure) at different places, an easier way to keep track of
program flow than a collection of “GO TO” or “JUMP” statements.
◦ Structured Programming.
 Process of writing a program in small, independent parts.
 Structured programs are built up from units called modules, which
normally correspond to single procedures or functions.
 It can be seen as a subset or sub discipline of procedural programming.
◦ Object-Oriented Programming.
 a computer program is 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.
 It is claimed to give more flexibility, easing changes to programs.
 This approach is often simpler to develop and maintain.
29
Problem Solving Concepts
 A problem
◦ an undesirable situation that prevents the organization
from fully achieving its purpose, goals and objectives.
◦ the gap between the existing and the desired situation
where problem solving will try to fill.
 Problem solving

◦ the process of transforming the description of a problem


into the solution by using our knowledge of the problem
domain and by relying on our ability to select and use
appropriate problem-solving strategies, techniques, and
tools.

30
Problem Solving Methodologies
 There is no perfect method for solving all problems.
 There is no problem-solving computer to which we could
simply describe a given problem and wait for it to provide
the solution.
 Problem solving is a creative act and cannot be completely
explained.
 However, we can still use certain accepted procedures to
structure our thinking and help us solve problems.
 Three methods of problem solving are prominent:
1. Analytic Method.
2. Algorithmic Method.
3. Software Engineering Method.

31
Problem Solving Process/Steps

 Allproblem solving methodologies shares four


problem solving processes/steps:
1. Define and analyze the problem.
2. Develop a solution: write down the solution steps in
detail.
3. Test the solution and revise if necessary.
4. Document and maintain the solution.

32
The Analytic Method
 Analytic problem solving has roots in mathematics, physics,
engineering, chemistry, and a host of other areas in science
and technology.
 Example

Problem
The sum of two numbers is s and their product is p. Find
the 2 numbers.
Reasoning
Let us call the first number x and the second one y.
Therefore, x + y = s and x * y = p.
From the first equation we get: y = s - x.
Substituting y from the first equation into the second equation, weget:

x * (s - x) = p or
x2 - sx + p = 0. 33
The Analytic …
 Solution
The solution to this problem is the solution to the quadratic
equation: x2 - sx + p = 0, provided that s2 - 4p  0.
◦ This solution is known from the field of mathematics.
 Testing
To check the correctness of the above solution we calculate x + y and
x * y.
 Limitations of the Analytic Approach
◦ Some problems cannot be solved analytically even with the most
sophisticated technique. For example, mathematicians have proven
that no analytic method can find the roots of a fifth-degree
polynomial equation of the form:
ax5 + bx4 + cx3 + dx2 + ex + f = 0 for arbitrary coefficients.

34
The Algorithmic Approach
 Algorithmic Problem
◦ It is any problem whose solution can be expressed as a list
of executable instructions.
 By executable we mean that an independent executor
(human or machine) should be able to carry out the
instructions in a step-by-step manner.

Examples of Algorithmic Problems


1. Taking the sum of a finite list of numbers.
2. Sorting a finite list of names.
3. Playing Tic-Tac-Toe.
4. Baking a cake.
5. Knitting a sweater.

35
Algorithm
 It is a finite set of well-defined instructions for accomplishing
some task which, given an initial state, will terminate in a
corresponding recognizable end-state.
 It should be:

◦ Precise and unambiguous


◦ Simple
◦ Correct
◦ Efficient
Phases of Algorithmic Problem Solving
1. Understand the problem.
2. Devise a plan for solving the problem.
3. Carry out the plan.
4. Evaluate the solution for accuracy and for its potential as a
tool for solving other problems.

36
Software Engineering Method
 Software Engineering.
◦ It is the profession/discipline that creates and maintains software
applications by applying technologies and practices from
computer science, project management, engineering, application
domain and other fields.

◦ The method used in solving problems in computer science and/or


information systems is called the software development life cycle
(SDLC).
 It is a conceptual model used in project management that describes
the stages involved in a computer system development project
 from an initial feasibility study through maintenance of the
completed application.
37
Software Development Life Cycle (SDLC): Software Engineering Phases
 Feasibility study-which includes e.g. the following components:
Organizational Feasibility
Economic Feasibility
Technical Feasibility
Operational Feasibility
◦ identify a need for the new system.
 Problem Analysis - (Correct Problem)
 Identify data objects
 Determine Input / Output data
 Constraints on the problem
 Design
 Decompose into smaller problems
 Top-down design (divide and conquer)
 Develop Algorithm (Desk check)
38
SDLC...
 Implementation
◦ The real code is written here.
◦ Systems implementation is the construction of the new system
and its delivery into production or day-to-day operation.
◦ Writing the algorithm using a programming language (ex. C++)
 Testing
 Verify the program meets requirements
 System Integration and Unit testing
 Documentation
 Key part in the development process
 Maintenance
◦ What happens during the rest of the software's life: changes,
correction, additions, moves to a different computing platform
and more.
39
Software Engineering Goals
 Reliability
◦ An unreliable life-critical system can be fatal
 Understandability
◦ Future development becomes very difficult if software is hard to
understand
 Cost Effectiveness
◦ Cost to develop and maintain should not exceed profit
 Adaptability
◦ System that is adaptive is easier to alter and expand
 Reusability
◦ Improves reliability and maintainability, and reduces development
costs

40
Abstraction
 Extract the relevant object properties while ignoring inessentials
◦ Defines a view of the object
 Example – car
◦ Car dealer views a car from selling features standpoint
 Price, length of warranty, color, …
◦ Mechanic views a car from systems maintenance standpoint
 Size of the oil filter, type of spark plugs, …

41
Problem Solving Approaches
 Generally, there are two approaches of problem
solving:
◦ Top down design
 A systematic approach based on the concept that the structure
of the problem should determine the structure of the solution and
what should be done in lower level.
 This approach will try to disintegrate a larger problem into more
smaller and manageable problems to narrow the problem
domain.
◦ Bottom up design
 The reverse process where the lowest level component are built
first and the system builds up from the bottom until the whole
process is finally completed.

42
Top-Down Design
 A program is divided into a main module and its related
modules. Each module is in turn divided into sub-modules
until the resulting modules are understood without further
division.

43
Algorithmic Structure
 An algorithm is written as a finite step-by-step procedure
(sequence) in which choices can be made where necessary
(selection), and all or part of the algorithm can be repeated
(repetition).
 The basic structures of an algorithm are classified as:
1. Sequence
2. Selection
3. Repetition
Algorithm Representation
There are commonly used tools or methods to help to document
program logic (the algorithm). The three methods to represent an
algorithm are:
 Pseudo code
 Flow Chart
 Programming Languages(structured chart)
44
Pseudocode
 It is an English language like notion that can be used to express an
algorithm for a given problem
 It is used during the early stages of algorithm development resemble
but is less formal than a programming language.

 Original Program Specification:


Example1: Write an algorithm using pseudocode that determine the
area of a triangle.
step1: INPUT base, height
step2: area := base*height/2
step3: OUTPUT area
step4: STOP

45
Example2:
 Original Program Specification:
 Write a program that obtains two integer numbers
from the user. It will print out the sum of those
numbers.

Pseudocode
Prompt the user to enter the first integer
Prompt the user to enter a second integer
Compute the sum of the two user inputs
Display an output prompt that explains the answer as the sum
Display the result

46
Flowchart
 It is a graphical way of expressing the steps needed to
solve a given problem.
 It is a schematic (diagrammatic description) representation

of a process.

Basic flow
charting
symbols

Circle: on page
Connector
Home base: off page

47
Example1
 Example: Write an algorithm using flowchart that
determine the area of a triangle.
Start

Input base, height

Area = base * height/2

Print area

End

48
Example 2:
Write an algorithm description and draw a flow chart to
check a number is negative or not.
Algorithm description.
 Read a number x
 If x is less than zero write a message negative
 else write a message not negative

 The flow chart is:

49
Structured Charts
 It depicts the logical functions to the solution of the problem using
a chart.

 It provides an overview that confirms the solution to the problem


without excessive consideration to detail.
 It is high-level in nature.

 Example: Write a program that asks the user to enter a temperature


reading in centigrade and then prints the equivalent Fahrenheit
value.

50
Input Process Output
Centigrade  Prompt for centigrade value Fahrenheit
 Read centigrade value
 Compute Fahrenheit value
 Display Fahrenheit value

51
Programming as a Problem Solving Process
 Define and analyze the problem.
◦ What is the input & output?
◦ What other information is necessary?
 Develop an algorithm.
◦ What steps must be done?
 Implement a program.
 Compile, test, and debug the program.
 Document and maintain the program.

52
Structured Programming
 It is the set of design and implementation processes
that yield well-structured programs.
 It is a disciplined approach to programming
 It follows top-down design approach
 It is based on step-wise refinement using a restricted
set of program structures
◦ The set of program structures used in structured
programming is:
 Sequence
 Selection
 Repetition

53
Sequence
 An algorithm that is based on the notion of
sequence, which is the ordering of statements.
 Step n cannot be started until step n-1 is
complete.
 Problem1: Develop an algorithm that
calculates and prints the area of a trapezoid
when the values of the bases and height are
given as input.
Input: base1, base2, height
Output: area
Algorithm1:
step1: INPUT base1, base2, height
step2: area := (base1+base2)*height/2
step3: OUTPUT area
step4: STOP

54
Selection
 Selection is the choice of alternate paths (branches) depending on a
condition that may arise in the logical flow of the algorithm.

55
Selection …
Problem2: Write an algorithm that accepts a number representing either a Fahrenheit
or a Celsius temperature scale and converts it to the other scale.
Input: temperature, scale (Fahrenheit or Celsius)
Output: newTemp (Converted Temperature)
Algorithm2:
step1:INPUT scale, temperature
step2:IF scale = 'f' THEN
newTemp := 5/9*(temperature - 32)
ELSE
newTemp := 9/5*temperature + 32
step3:OUTPUT newTemp
step4:STOP
 The above algorithm has introduced IF...THEN...ELSE, which is called a
selection structure. This statement reads "IF it is true that the scale equals f
(Fahrenheit) THEN convert the input temperature to Celcius ELSE convert it to
Fahrenheit".
 In an IF...THEN...ELSE statement, if the logical condition after IF is true, the
statements after THEN are executed; otherwise, the statements after ELSE are
executed.
56
Selection …
Problem3: Assume that a salesperson is paid a commission based on the number of sales made
during the week. The salesperson is paid 8% per sale for fewer than the established quota of
15 sales, 12% per sale if the quota is reached, and 16% per sale if the quota is exceeded.
Input: sales (i.e., number of sales)
Output: commission
Algorithm3:
step1: INPUT sales
step2: IF sales < quota THEN
rate := 8
ELSE IF sales = quota THEN
rate := 12
ELSE
rate := 16
step3: commission := rate * sales
step4: OUTPUT commission
step5: STOP
 Observe that rate must be calculated before the commission can be computed.
 This algorithm has introduced multiple selection based on the value of sales. Multiple
selections can be achieved by the repeated use (or nested use) of ELSE IF.
 The last ELSE of the nested IF becomes the default or catchall case that is considered if
none of the other possibilities is true.

57
Repetition
 Repetition or looping provides for the repeated execution of part of the
algorithm.
 Syntax
while (condition)
action
false
 How it works: condition
◦ if condition is true then execute
action
◦ repeat this process until condition
evaluates to false true
 action is either a single statement or a
group of statements within braces. action

58
Another Repetition Statement
 Syntax
for (initialization; condition; update)
action initialization
 How it works:
◦ execute initialization statement
◦ while condition is true condition
 execute action
 execute update true false

action

update

59
Yet Another Repetition Statement
 Syntax
do action
while (condition)
action
 How it works:
◦ execute action
◦ if condition is true then
execute action again
◦ repeat this process until true
condition evaluates to false. condition
 action is either a single
false
statement or a group of
statements within braces.

60
Example: repetition …
Problem4: Reconsider the sales commission problem for many salespeople.
Input: number of sales people, number of sales for each salesperson.
Output: commission for each salesperson.
Algorithm4:
step1: INPUT numSalesPeople
step2: LOOP numSalesPeople
a: INPUT sales
b: IF sales < quota THEN
rate := 8
ELSE IF sales = quota THEN
rate := 12
ELSE
rate := 16
c: commission := rate * sales
d: OUTPUT commission
step3: STOP
 A loop is used to repeatedly read the data for each salesperson and compute the associated
commission.
 The word LOOP here means that the following sequence of steps (2a to 2d in this example) is to be
repeated for a specific number of iterations (numSalesPeople in this example).
 The LOOP construct is only appropriate if the number of iterations can be predetermined as it is in
the above algorithm.
 For many situations, however, it is not possible to predetermine the number of iterations and a
more flexible looping structure is required. 61
Home Assignment-1
1. What is programming?
2. Why do we need to learn computer programming?
3. What is programming language?
4. What skills do we need to be a programmer?
5. Write the algorithmic description and draw a flow chart
to check whether the number is even or odd number
6. Write the algorithmic description and draw a flow chart for
student marks:
if mark is >80 display A
if mark is >60 and <=80 display B
if mark is>40 anx<=60 display C
else display fail
7. Calculate and print the average of three numbers: 5, 10, and 15 by:

a) Pseudocode
62
b) Flowchart

You might also like