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

Intro To CPP

The document discusses problem solving and programming. It begins by explaining that programming involves solving problems by developing software solutions. The software development process involves 4 phases: 1) analyzing the problem, 2) developing a solution, 3) coding the solution, and 4) testing the solution. Most effort is typically spent on testing. Documentation of the solution is also important. Proper documentation includes descriptions of the program, algorithms, code, test runs, and a user manual. Maintenance of the software may be needed to fix bugs and add new features. Pseudocode is used to describe algorithms before coding them into an actual programming language. Loops are also discussed as a way to repeat steps until a condition is met.

Uploaded by

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

Intro To CPP

The document discusses problem solving and programming. It begins by explaining that programming involves solving problems by developing software solutions. The software development process involves 4 phases: 1) analyzing the problem, 2) developing a solution, 3) coding the solution, and 4) testing the solution. Most effort is typically spent on testing. Documentation of the solution is also important. Proper documentation includes descriptions of the program, algorithms, code, test runs, and a user manual. Maintenance of the software may be needed to fix bugs and add new features. Pseudocode is used to describe algorithms before coding them into an actual programming language. Loops are also discussed as a way to repeat steps until a condition is met.

Uploaded by

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

CHAPTER ONE

Introduction to Programming
Introduction
No matter what field of work you choose, you may have to solve
problems. Many of these can be solved quickly and easily. Still others
require considerable planning and forethought if the solution is to be
appropriate and efficient. Problem in this sense is one that can be
solved using computers, some examples,
Requirements for:
 Handling and manipulating employee data
 Solving mathematical equations based on formula
 Handling and manipulation of information about students etc.
A problem may be real problem (existing problem), or anticipated
(potential) problem. The reasons (initiation) for studying (identifying) a
given problems may be
 Directives (in organizations)
 Opportunities (to do things better)
 Or other things.

4.1. Problem solving


Creating a program is no different because a program is a solution
developed to solve a particular problem. As such, writing a program is
almost the last step in a process of first determining what the problem
is and the method that will be used to solve the problem. In solving a
problem, we may get various solutions.
One technique used by professional software developers for
understanding the problem that is being solved and for creating an
effective and appropriate software solution is called the software
development procedure. The procedure consists of three overlapping
phases
 Development and Design
 Documentation
 Maintenance

Phase I: Development and Design


The development and analysis phase consists of four different
overlapping phases, which includes:
 analyzing a problem
 develop a solution
 coding the solution
 testing

I. analyzing the problem


Before starting any part of the solution, we have to be introduced
with the problem. First, we must understand thoroughly what the
problem is. This should lead us to complete specifications of the
problem to be addressed.

Department of Computing 1
 What part of the problem is going to be solved?
 What input data is required to solve the problem?
 What output data (result) is expected?
 What procedures are needed to achieve the result?

There are various methods to find these specifications, like interview,


observations, or for simpler problems in written form, read the
requirements carefully.
II. Developing the Solution
Programming is all about solving problems. This step involves the
following tasks:
 Divide the original problem into a number of sub problems.
These sub- problems, being necessarily smaller than the
original problem, are easier to solve and their solution will be
the components of our final solution. This method, by which a
complex problem is divided into smaller and easier sub-
problems is called “divide and conquer”
 Associating each task with a precise method to accomplish it,
which consists of a sequence of well defined steps which, when
carried out, perform the corresponding task. This sequence of
steps is called and Algorithm.
Algorithm is a sequence of steps that describes how the data are to
be processed to produce the desired outputs. Algorithms are usually
developed in pseudo code or using flowchart.
This stage is sketching the step-by-step activities that lead to the
solution. This is formulating a problem in terms of the steps to its
solution.

III. Coding the program


Algorithms cannot be executed (performed) directly by the
computer. They must first be translated into a kind of data that the
computer can understand. This step consists of translating the
algorithm into a computer program using a programming language
and the process is referred to as coding.
It is expressing the solutions (in the previous step) in one of the
various programming language.
There are many high-level programming languages like BASIC,
COBOL, Pascal, FORTRAN, C, C++, VB, etc. To get our program work,
we write it based on the syntax rules of the programming language.

IV. Testing the program


It is very rare that a program can be written correctly the first
time. Most programmers get used to the idea that there are errors in
their newly written programs, these errors are detected during testing
the program, and appropriate revision must be made and the tests
return. Testing provide a way to ensure whether a program provide
the intended solution to the problem.

Department of Computing 2
Figure below lists the relative amount of effort that is typically expended on
each of these four development and design steps in large commercial programming
projects.
Step Effort (%)
Analyzing the solution 10
Developing the solution 20
Coding the solution 20
Testing the solution 50

In general, the process of coding and testing is called implementation.

Phase II: Documentation


Documentation is compiling related documents throughout the
lifetime of the program development. Documentation requires
collecting critical documents during the analysis, design, coding, and
testing.
There are five documents for every program solution:
 Program description
 Algorithm development and changes
 Well-commented program listing
 Sample test runs
 User’s manual

Phase III: Maintenance


This phase is concerned with the ongoing correction of problems,
revisions to meet changing needs and the addition of new features.
Maintenance is often the major effort, and the longest lasting of the
three phases. While development may take days or months,
maintenance may continue for years or decades.
Algorithms and flowcharts
Programming
The computer is directed by a program, i.e., a sequence of
instructions that determine the operations to be carried out by the
machine. Human or the user writes programs. Computer
programming is the process of program design and implementation by
the use of the computer. The user must specify the operations to be
performed. Natural languages like English, used for human
communication are not fit for programming because of their ambiguity
and lack of precision. Therefore program can be written in machine
language, assembly languages or high-level languages.
A compiler is used to convert the program source code entered
in HLL to a machine code. Each statement in a high level language
can be converted into a number of machine code instructions.

Department of Computing 3
Design and Implementation of Algorithms:

An algorithm is a finite set of instruction that specify a sequence


of operations to be carried out in order to solve a specific problem or
class of problems. It is just a tool for solving a problem. All the tasks
that can be carried out by a computer can be stated as algorithms.
Once an algorithm is designed, it is coded in a programming language
and computer executes the program. Algorithms may be presented on
several levels of detail. It is difficult to use terms of machine code
instructions or high level language because the details would obscure
the essence of the procedure and due to difficulty to present long
programs in simple form. Therefore algorithm designers must start
with a much more concise representation

A. Pseudo code
Pseudo code is an artificial and informal language that helps
programmers to develop algorithms. Pseudo code is a language used
to describe the manipulations to be performed on data. This language
is a simple mixture of natural language and mathematical notations
and it is independent of programming language. Pseudo code has
some ways to represent sequence, decision and repetition in
algorithms.
Example: 1) Pseudo code to add two numbers.
Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: sum  n1 + n2
Step 4: Print sum
Step 5: Stop
Example: 2) pseudo code to find largest number from two numbers.
Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: If n1 > n2 then
Big  n1
else
Big  n2
Step 4: Print Big
Step 5: Stop
Example: 3) pseudo code to find largest number from three numbers.
Step 1: start
Step 2: Read three numbers n1, n2 and n3.
Step 3: If n1 > n2 and n1 > n3 then
Big  n1
Else
If n2 > n1 and n2 > n3 then
Big  n2
else
Big  n3
Step 4: Print Big
Step 5: Stop.

Department of Computing 4
Loops
Some times there is a situation in which it is necessary to
execute a group of statements repeatedly until some condition is
satisfied. This situation is called a loop. Loop is a sequence of
instruction, which is repeated until some specific condition occurs.
The general execution flow structure of loops looks like the following.
[Initialization]

[test] false [stop]

true

[Compute]

[Increment/Decrement]

The above simple structure tells that execution continues as far as the
test holds true.
Example 4) Pseudo code to find sum of N positive integer numbers.
Step 1: start
Step 2: Read N
Step 3: Sum  0,
Step 4: Count  0
Step 5: Read Num
Step 6: SumSum + Num
Step 7: count  count +1
Step 8: If Count < N then goto step 5
Step 9: Print Sum
Step 10: Stop
Assignment:
1) Write an algorithm to find the roots of quadratic equation
. Using the formula x= . Depending on
the values of D Where
2) Write an algorithm to check whether the given number is even or
odd and display the suitable message.

B. Flow charts:
Due to the detail required of them, programming languages are not
convenient tools for initial algorithm design. The means of notation
widely used for algorithm is a flowchart. Flowchart is a two-
dimensional representation of an algorithm; the predefined graphic
symbols of a flowchart are used to indicate the various operations and
the flow of control. The most significant advantage of flowcharts is a
clear presentation of the flow of control in the algorithm, i.e. the
sequence in which operations are performed.

Department of Computing 5
A basic set of established flowchart symbols is:
Decision
Processing Input/output

START & STOP

Connector
Annotation
Flow lines
The symbols have the following meanings:
Processing: one or more computational tasks are to be performed
sequentially
Input/Output: Data are to be read into the computer memory from
an input device or data are to be passed from the memory to an
output device. (Parallelogram)
Decision: two alternative execution paths are possible. The path to
be followed is selected during the execution by testing whether or not
the condition specified within the outline is fulfilled. (Rhombus)
Terminals: appears either at the beginning of a flowchart (and
contains the word “start”) or at its conclusion (and contains “stop”).
Annotation: contains comments that simplify the understanding of
the algorithm or description of data.
Connector: makes it possible to separate a flowchart into parts.
Identical cross reference symbols are placed in this outline where the
flow line is interrupted and where it resumes.
Flow lines: indicates the outline that is to be entered next.
Flowcharts allow the reader to follow the logic of the algorithm more
easily than would a linear description in English.
1. A flowchart to find factorial 2. A Flowchart to find
of a positive integer n largest of two numbers.
START
Start

Input n
Read A,
B.
F=1
Yes No
Is
No A>B ?
n> Display F
0? Yes
Display Display
END
F= F*n A is B is
Largest Largest
Flowchart versus pseudo code
Stop
n = n -1
Department of Computing 6
Since flowcharts are inconvenient to revise, they have fallen out of
favour by programmers. Nowadays, the use of pseudo code has gained
increasing acceptance.
Only after an algorithm has been selected and the programmer
understands the steps required can the algorithm be written using
computer-language statements. The writing of an algorithm using
computer-language statements is called coding the algorithm, which is
the third step in our program development process.

Exercise:
Algorithms:
1. Write an algorithm to find the smallest number from three
numbers.
2. Write an algorithm to find the sum of first N even numbers.
3. Write an algorithm to generate Fibonacci series.(a series which
goes like 1,1,2,3,5,8,13,…)
4. Write an algorithm to find the sum of digits of given number.
(Eg. If the given number is 251, the sum of digits is 2+5+1=8)
Flowcharts:
1. Draw a flowchart to find sum of N positive numbers.
2. Draw a flowchart to find the biggest among N numbers.

Department of Computing 7

You might also like