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

2 Programming Concepts

The document discusses algorithms and program development. It defines an algorithm as a set of precisely defined steps to solve a problem. Developing an algorithm involves analyzing the problem, creating the step-by-step solution, and choosing an algorithm based on efficiency, accuracy, and clarity. It also discusses concepts like programs, data, problem types, flowcharts, and pseudocode for designing solutions. The document concludes by covering compilation, execution, and debugging of programs.

Uploaded by

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

2 Programming Concepts

The document discusses algorithms and program development. It defines an algorithm as a set of precisely defined steps to solve a problem. Developing an algorithm involves analyzing the problem, creating the step-by-step solution, and choosing an algorithm based on efficiency, accuracy, and clarity. It also discusses concepts like programs, data, problem types, flowcharts, and pseudocode for designing solutions. The document concludes by covering compilation, execution, and debugging of programs.

Uploaded by

paul logan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Basics of Computer

Sciences
By
Dr. Ali Kamel Abdel-Rahman
Mechanical Engineering Department
Algorithms and Program Development

• Solving a problem on a computer requires a


thorough analysis of the problem and the
potential data.
• Once the problem has been analyzed, a detailed
solution can be developed.
• One of the steps in developing a computerized
solution to a problem is to create an algorithm.

Dr. Ali K. Abdel-Rahman


Concept of Algorithm

• An algorithm is a procedure consisting of a finite


number of precisely defined steps of solving a
problem.
• Each step of an algorithm must be a clear
instruction which, when written in computer
language, can be executed by a computer.
• The order of steps is important, because most
computers can only execute one instruction at a
time.
• There may be several different algorithms that
can solve a single problem.

Dr. Ali K. Abdel-Rahman


Concept of Algorithm

• The programmer should choose an algorithm on


the basis of efficiency, accuracy, and clarity.
• The algorithm should be efficient with respect to
computational time, storage requirements, and
response time.
• The accuracy required is specified by the user.
• Clarity means an understandable programming
style.
• Compromise between these factors is necessary.
For example, efficiency may be sacrificed for the
sake of increased reliability.

Dr. Ali K. Abdel-Rahman


Concept of Algorithm

• Example of a high-level problem specification is:


Calculate the total amount of steel needed to
build a rectangular tank that has four cylindrical
steel columns 20 m long for support and that can
hold 50 m3 of water.
• This can be decomposed into the following parts:
– 1. Calculate the optimum dimensions of the tank.
– 2. Calculate the thickness of the steel needed in the tank.
– 3. Calculate the total compression load on each column.
– 4. Calculate the optimum size of the columns.
– 5. Calculate the total volume of steel needed to build the tank
and the support columns.

Dr. Ali K. Abdel-Rahman


Concept of Programs

• A program is a meaningful sequence of


executable, unambiguous instructions written in
a computer language.
• The computer can understand instructions of
various types:
– input/output instructions to enter data into the computer and
output answers from it;
– move instructions to rearrange data,
– control instructions to control selection and repetition of
actions;
– arithmetic instructions to perform calculations;
– logic instructions to help the computer make choices.

Dr. Ali K. Abdel-Rahman


Concept of Data

• The collection of related information to be


processed by an application program is known as
data.
• Data can be numeric or both numeric and
alphabetic, known as alphanumeric.
– Numeric data can be written as numbers with fractional parts,
as integers, in scientific notation, or as complex numbers.
– Numeric data is treated as numeric values.
– Alphanumeric data is treated as strings of characters.
– Data can also represent the logic values true and false.

Dr. Ali K. Abdel-Rahman


Concept of Data

Numeric data Alphanumeric data


25 ‘JOHN SMITH’
-17.3 ‘(501) 327-5691’
0.029 ‘PX394’
45.37 ‘+45.37’
0.314159E+01 ‘33SE’
0.314159E-01 ‘$33’

Dr. Ali K. Abdel-Rahman


Problem Types

• There are three basic types of solutions.


– First, when a closed-form solution to a problem is known, the
appropriate equations can be used in a program.
– Second, when no closed-form solution is known or when the
mathematical solution involves trial and error, numerical
methods can be used.
– Third, some types of problems have so many independent
variables that rather than a single answer, a variety of possible
solutions is desired. In this case, mathematical modeling may
be appropriate.

Dr. Ali K. Abdel-Rahman


Procedure for Solution Design

• Once the problem has been clearly stated and


analyzed, the solution must be developed.
• This involves the actual design of the input,
output, and internal data, the development of an
algorithm, and the development of test data for
testing the algorithm.
• For example, the computer must know whether
the data consists of integer or real numbers.
• At the design stage, the algorithm can be
represented by flowcharts, pseudocode, or by
other logic representation techniques.

Dr. Ali K. Abdel-Rahman


Flowcharts

• Before you begin to write any moderately


complicated program, some sort of flowchart is
essential as a guide to the construction of the
code.
• A flowchart is especially useful if you intend to
discuss your code with colleagues or your
instructor. It is usually much easier to read
someone else’s flowchart than his or her
FORTRAN.

Dr. Ali K. Abdel-Rahman


Flowcharts

• A flowchart is a method of diagramming the logic


of an algorithm using standardized set of
symbols to indicate the various elements of the
program.
• The most common symbols used in flowcharts
are shown.

Input/ Do loops
Process Terminator Decision
Output I=I1, I2…In

Dr. Ali K. Abdel-Rahman


Flowcharts

• As an example of a flowchart representation of a


simple program, consider the problem of
computing the wages for a worker who has
worked a given number of hours in a week and is
paid at a rate of PAYRAT in dollars per hour for
the first 40 hours and at a higher rate of OVTRAT
for time exceeding 40 hours.

Dr. Ali K. Abdel-Rahman


Flowcharts

• The flowchart for


this rather simple
problem is shown.
• The key element of
the algorithm is the
decision structure
represented by the
diamond-shaped
symbol.

Dr. Ali K. Abdel-Rahman


Pseudocode

• Before a program can be written, the problem to


be solved must be mapped out and some form of
outline is constructed.
• One procedure for doing this is the flowchart.
• However, a great many scientists and engineers
feel that flowcharts are too formal and resort
instead to a highly informal procedure called
pseudocode.
• The idea is to describe the operation of the
program using a simplified mix of FORTRAN and
English.

Dr. Ali K. Abdel-Rahman


Pseudocode

• A pseudocode outline of the salary problem


follows:
– READ in hours worked, base pay rate, and overtime rate
– Echo PRINT
– Compute pay using base rate
– PAY = PAYRAT*HOURS
– IF (hours worked > 40) THEN
– Multiply excess of 40 by overtime bonus rate
– (HOURS – 40) * (OVTRAT – PAYRAT)
– Add to base pay
– ELSE
– Skip overtime computation
– PRINT pay
– STOP

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Compilation Process

• The first step in testing a program on the


computer is compilation.
• During this process, the computer checks
whether the program is understandable and
accurately typed.
• If it appears to be all right, it is translated to
machine code.
• All FORTRAN 77 programs are written in
symbolic code known as FORTRAN source code.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Compilation Process

• The source code must be processed by a


FORTRAN 77 compiler in order to be translated
into machine code, which is designed for the
specific computer being used.
• The machine code generated during compilation
is called object code.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Compilation Process

• The compilation process is shown.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Program Execution

• After the main program module and any sub-


modules have been written and compiled
correctly, the resulting object modules are linked
along with any mathematical or other library
routines needed, and a single executable module
is built.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Program Execution

• The process of
building an
executable module,
or linking, is shown.
• The linkers and
loaders of the
operating system
are responsible for
building the
executable module
and executing it.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Errors and Debugging Methods

• Program development is almost never error free.


• Errors may be of many types;
– specification and program design errors,
– implementation errors,
– typing errors,
– logic errors,
– data input errors.
• Errors are detected at different stages as
– specification and design errors,
– compilation errors,
– linkage errors, or
– execution errors.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Errors and Debugging Methods

• The errors in a program are called bugs, and the


process of detecting and removing them is called
debugging.
• Logic errors are the hardest to detect, since
programs may produce answers without
producing correct answers.
• To check the accuracy of the logic, it is necessary
to know in advance what the answers should be
for certain sets of test data.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Program Testing and Documentation

• Testing is a process intentionally designed to find


errors in programs.
• This must be done systematically.
• Each module must be separately tested for errors
and when the modules are put together, the result
must be tested further.
• Not only must a program give correct answers to
correct input, it must be able to detect incorrect
input and avoid giving incorrect answers to
possible but unlikely input.

Dr. Ali K. Abdel-Rahman


Program Compilation, Debugging, and Testing
Program Testing and Documentation

• Documentation, an important part of software


development, should be carried out
simultaneously, with design.
• Documentation is used to keep track of the
design procedure and to keep track of
implementation and testing.
• It becomes part of the final system, where it is
used by programmers who will maintain or
modify the system.

Dr. Ali K. Abdel-Rahman


Program Processing Environment

• There are two ways to look at a processing


environment as:
– a computer system environment or
– a programming environment.
• The system environment in which the program
runs is characterized as
– single job,
– time-sharing,
– multiprogramming, or multiprocessing.
• The programming environment is
– batch,
– interactive, or real time.

Dr. Ali K. Abdel-Rahman


System Environment

• A single-job environment is one in which only


one program at a time can be loaded into the
computer.
• All the system resources in such an environment,
such as disk, memory, and processor, are
allocated to a single job.
• Once the job is completed, all the system
resources are released.

Dr. Ali K. Abdel-Rahman


System Environment

• Most mainframe computers and minicomputers


and some microcomputers support a time-
sharing environment.
• A time-sharing environment is one in which
several users can have access to the computer at
the same time, running different programs.
• Multiprogramming, multiprocessing, and parallel
processing are all different forms of time-sharing
environment.

Dr. Ali K. Abdel-Rahman


System Environment

• In a multiprogramming environment, several


executable programs can exist in memory at the
same time, but only one program is executed at
any given instant.
• The programs that are loaded for execution will
take turns using the time and resources available
within the time limit allocated to each.

Dr. Ali K. Abdel-Rahman


System Environment

• In a multi-processing environment there is more


than one CPU, making it possible to execute
several programs simultaneously.
• Several processors may be working on the same
program at the same time.

Dr. Ali K. Abdel-Rahman


Programming Environment

• Programming environments are designed for


different user needs.
• In batch processing, programs are executed
when it is convenient for the computer
installation. Usually large amounts of data are
involved and the actual time of execution is not
critical.
• In interactive processing, programs are executed
while the user waits for the output.
• Real-time processing is used to directly control
equipment.

Dr. Ali K. Abdel-Rahman


Programming Environment
Batch Processing

• In batch processing, the operating system takes


control of the program.
• The computer schedules and controls program
execution.
• Several jobs may be entered through terminals,
or loaded from disk files and left to be executed
when sufficient time is available.
• Batch processing is used when there are large
amounts of data to be processed, or when time is
not critical.

Dr. Ali K. Abdel-Rahman


Programming Environment
Interactive Processing

• In interactive processing, the user is in


communication with the computer system.
• Data is entered through terminals and the user
expects an immediate response from the system.
• Several terminals can be connected to a large
computer system, each one having access to the
computer hardware and software resources.
• If there are several users at the same time, the
CPU will share its time with all of them.

Dr. Ali K. Abdel-Rahman


Programming Environment
Real-time Processing

• In real-time processing, computer response must


be nearly instantaneous.
• The computer is used to directly control
equipment. Embedded computer systems are
real-time systems. Examples include:
– a computer on board an aircraft that controls the autopilot, or
– computers that control nuclear reactor cooling systems, space
shuttles, pacemakers, power fluctuations, and automobile
ignition systems.
• Real-time systems are online systems which
must respond immediately to changing needs.
Such systems are dedicated to single job
applications.

Dr. Ali K. Abdel-Rahman


Dr. Ali K. Abdel-Rahman
Dr. Ali K. Abdel-Rahman
Dr. Ali K. Abdel-Rahman
Dr. Ali K. Abdel-Rahman
Dr. Ali K. Abdel-Rahman
Dr. Ali K. Abdel-Rahman
Dr. Ali K. Abdel-Rahman

You might also like