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

Chapter One_Problem Solving Computer

Uploaded by

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

Chapter One_Problem Solving Computer

Uploaded by

felpitmom
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Problem solving using

computers

CHAPTER 1
1.1. What is a program?
2

A computer is a tool for solving problems with


data.
A program is a sequence of instructions that
tell a computer how to do a task.
When a computer follows the instructions in a
program, we say it executes the program.
A computer will only do what you tell it to do.
This might make programming frustrating at
first
computers don't understand steps written on
paper.
1.1. What is a program?
3

Computers are machines and at the most basic


level, they are a collection of switches—where 1
represents "on" and 0 represents "off".
Everything that a computer does is implemented
in this most basic of all numbering systems—
binary.
If you really wanted to tell a computer what to
do directly, you'd have to talk to it in binary,
giving it coded sequences of 1’s and 0’s that tell
it which instructions to execute. However, this is
nearly impossible.
In practice, we use a programming language.
1.2. Programming Language
4

Programming means telling a computer what to


do.
"Do this, then do that, than take the two results, mix
them up this way, and repeat until you get a result out
of it…"
Programming languages are the method used to
instruct a computer on how to accomplish a task.
A programming language is a collection of
operators and instructions, with specific rules or
syntax regarding how the instructions are placed
together.
1.2. Programming Language
5

 Types of programming languages

 There are three types of programming languages:


 machine language,
 assembly language, and
 high level language.
 Low-level (assembly) programming is close to
machine code and high-level programming is

closer to natural languages.


1.2. Programming Language
6

Machine Language
 All computers have a native programming language
that they understand, commonly referred to
as machine code or language.
 Any computer can directly understand only its own

machine language.
 Machine language is the “natural language” of a

computer
 Machine languages generally consist of strings of

numbers (ultimately reduced to 1s and 0s) that


instruct computers to perform their most operations
one at a time.
 The instructions written in machine instructions are in

binary code.
1.2. Programming Language
7

A single instruction to a computer could look like this:


00000 10011110
A particular computer's machine language program
that allows a user to input two numbers, adds the two
numbers together, and displays the total could include
these machine code instructions:
00000 10011110
00001 11110100
00010 10011110
00011 11010100
00100 10111111
00101 00000000
1.2. Programming Language
8

Assembly programming language


Instead of using the string of numbers that
computers could directly understand,
programmers began using English like
abbreviations to represent elementary operations.
 These abbreviations formed the basis of assembly

languages.
 Translator programs called assembler were

developed to convert assembly language programs


to machine language.
 Each line of assembly code produces one machine

instruction (One-to-one correspondence).


1.2. Programming Language
9

Here's how to add two numbers in assembly:


LUI R1, #1
LUI R2, #2
DADD R3, R1, R2

 This just did the calculation 1 + 2 = 3.


1.2. Programming Language
10

High-level languages
 High level language look more like natural

language with mathematical operations.


 High level language which use statements

consisting of English-like keywords such as "FOR",


"PRINT" or “IF“, ... etc.
 These languages require more translation before

the computer will understand them, but they are


much easier to write.
 Each statement corresponds to several machine

language instructions (one-to-many


correspondence).
1.2. Programming Language
11

Here's what the same program might look like in a


high-level language:
x = 1 + 2;
For example: In C++:
#include <iostream.h>
int main()
{
int x;
x = 1+2;
cout<<“the sum of 1 and 2 is = “<<x;
return 0;
}
1.3. Stages of Program Development
12

There are five stages of program


development, such as:
 Analysis,

 Algorithm design,

 Coding,

 Implementation, and

 Maintenance.
I. Analysis
13

Analysis is the study of current procedures and


information systems.
Analysis stage requires a thorough
understanding of:
 the problem at hand and
 analysis of the data and procedures needed to

achieve the desired result.


The following questions must answers at the
end:
 What input data are needed to the problem?
 What procedures needed to achieve the result?

 What outputs data are expected?


II. Algorithm design
14

Once the requirements of the program are


defined, the next stage is to design an algorithm
to solve the problem
An algorithm is a step by step process that
describes how to solve a problem and/or complete
a task, which will always give the correct result.
It is sequence of steps to be performed in order to
solve a problem by the computer.
It is a map or an outline of a solution which shows
the precise order in which the program will
execute individual functions to arrive at the
solution.
II. Algorithm …
15

An algorithm can be expressed in many ways.


we only consider two design tools:
1. Pseudo code (Narrative )
 English is often used to describe or narrate the
algorithm.
 The programmer can describe the algorithm

without being restricted by any programming


rules.
 There is no need to follow any rules about how to

write it.
 The Pseudo-code can easily be translated into the

programming language
Examples of pseudo code
16

1. Algorithm to multiply two 2. Algorithm to check


numbers. whether an integer is
positive or negative.
Step 1: Start
Step 2: Read two numbers X Step 1: Start
and Y. Step 2: Read N
Step 3: Result = X * Y
Step 4: Display Result Step 3: If N >= 0, Display
Step 5: End “N is positive number”, Go
to step 5

Step 4: Else, Display “N is


negative number”

Step 5: End
II. Algorithm …
17

2. Flowchart
 Flowchart is a graphical or symbolic
representation of an algorithm.
 It is the diagrammatic representation of the

step-by-step solution to a given problem.


 It is logic diagram to describe each step that

the program must perform to arrive at the


solution.
 A popular logic tool used for showing an

algorithm in graphics form.


 Programmer prepares flowchart before coding.
18
Example of flowchart
19
III. Coding
20

The flowchart is independent of programming


language.
Now at this stage we translate each steps
described in the flowchart (algorithm
description) to an equivalent instruction of
target programming language.
For example, if we want to write in FORTRAN
program language, each step will be
described by an equivalent FORTRAN
instruction (Statement).
Example of C++ code
21

//Sample C++ code for add two numbers


#include<iostream.h>

int main ( )
{
int x, y, z;
cout<<“Enter two number : “;
cin>>x;
cin>> y;
z = x + y;
cout<<”The sum of two numbers is = “<<z;
return 0;
}
IV. Implementation
22

Once the program is written, the next step is to


implement it.
Program implementation involves three steps:
 Debugging (the process of removing errors),
 Testing (a check of correctness), and
 Documenting the program (to aid the maintenance
of a program during its life time).
Every program contains bugs that can range:
 From simple:- mistakes in the language usage

(syntax errors)
 Up to complex:- flaws in the algorithm (logic errors).
V. Maintenance
23

There are many reasons why programs must


be continually modified and maintained, like
changing conditions, new user needs,
previously undiscovered bugs (errors).
Maintenance may involve all steps from
requirements analysis to testing.
1.4. Compilation process of C++
24

C++ systems generally consist of three parts:


 Program development environment (IDE),
 the language, and
 the C++ Standard Library.

An integrated development environment


(IDE) is a software application that provides
comprehensive facilities to computer
programmer for software development.
An IDE normally consists of a source code
editor, build automation tools and a debugger.
2.4. Compilation …
25

The most popular C++ development tools


are:
 Borland C++ Builder,
 Microsoft Visual C++ 6,
 Turbo C++ Developer, and
 Microsoft Visual C++ .NET.
The C++ programs typically go through six

phases: edit, preprocess, compile, link, load


and execute.
2.4. Compilation …
26

Phase 1: Creating a Program


 Consists of editing a file with an editor

program (normally known simply as an editor).


 You type a C++ program (typically referred to

as source code) using the editor,


 make any necessary corrections and save the

program on a secondary storage device, such as


your hard drive.
 C++ source code file names often end with

the .cpp.
2.4. Compilation …
27

Phases 2 and 3: Preprocessing and Compiling


a C++ Program
 The programmer gives the command to compile the
program.
 In a C++ system, a preprocessor program executes
automatically before the compiler's translation phase
begins (so we call preprocessing phase 2 and compiling
phase 3).
 The C++ preprocessor obeys commands
called preprocessor directives, which indicate that
certain manipulations are to be performed on the
program before compilation.
 These manipulations usually include other text files to
be compiled and perform various text replacements.
2.4. Compilation …
28

Phase 4: Linking
 C++ programs typically contain references to
functions and data defined elsewhere, such as
in the standard libraries.
 The object code produced by the C++ compiler
typically contains “hollow place" due to these
missing parts.
 A linker links the object code with the code for
the missing functions to produce an executable
image (with no missing pieces).
 If the program compiles and links correctly, an
executable image is produced.
2.4. Compilation …
29

Phase 5: Loading
 Before a program can be executed, it must
first be placed in memory.
 This is done by the loader, which takes the

executable image from disk and transfers it to


memory.
 Additional components from shared libraries

that support the program are also loaded.


2.4. Compilation …
30

Phase 6: Execution
 Finally,
the computer, under the control of
its CPU, executes the program one
instruction at a time.
IDE …
31

You might also like