introduction to Algorithm, Lecture notes of Algorithms and Programming

Problem solving using algorithm

Typology: Lecture notes

2018/2019

Uploaded on 05/09/2019

muhammed-hassen
muhammed-hassen 🇪🇹

4

(1)

1 document

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 1
Problem Solving Using Computers
1.1 Computer Fundamental
A computer is a device that can perform computations and make logical decisions
phenomenally faster than human beings can. Many of today‘s personal computers can perform
billions of calculations in one secondmore than a human can perform in a lifetime. Computers
process data under the control of sets of instructions called computer programs.
In precise definition A computer is an electronic device that stores and processes data. It includes
both hardware and software. In general, hardware comprises the visible, physical elements of the
computer, and software provides the invisible instructions that control the hardware and make it
perform specific tasks. Writing instructions for computers to perform some task is called
computer programming. Knowing computer hardware isn‘t essential to your learning a
programming language, but it does help you understand better the effect of the program
instructions. This section introduces computer hardware components and their functions. A
computer consists of the following major hardware components
1) Input Devices
2) Storage Devices
3) Output Devices
4) Central Processing Unit (CPU)
Input Device:
Input devices are used to enter information into computer. They convert the data we give them
into the form that can be manipulated in the computer (electronic format). Most information is
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download introduction to Algorithm and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Chapter 1

Problem Solving Using Computers

1.1 Computer Fundamental

A computer is a device that can perform computations and make logical decisions phenomenally faster than human beings can. Many of today‘s personal computers can perform billions of calculations in one second—more than a human can perform in a lifetime. Computers process data under the control of sets of instructions called computer programs.

In precise definition A computer is an electronic device that stores and processes data. It includes both hardware and software. In general, hardware comprises the visible, physical elements of the computer, and software provides the invisible instructions that control the hardware and make it perform specific tasks. Writing instructions for computers to perform some task is called computer programming. Knowing computer hardware isn‘t essential to your learning a programming language, but it does help you understand better the effect of the program instructions. This section introduces computer hardware components and their functions. A computer consists of the following major hardware components

  1. Input Devices
  2. Storage Devices
  3. Output Devices
  4. Central Processing Unit (CPU)

Input Device :

Input devices are used to enter information into computer. They convert the data we give them into the form that can be manipulated in the computer (electronic format). Most information is

entered into computers through keyboards, touchscreens and mouse devices. Other forms of input include speaking to your computer, scanning images and barcodes, reading from secondary storage devices (like hard drives, DVD drives, Blu-ray Disc™ drives and USB flash drives—also called ―thumb drives‖ or ―memory sticks‖), receiving video from a web cam and having your computer receive information from the Internet (such as when you download videos from YouTube™ or e-books from Amazon).

Newer forms of Input include reading position data from a GPS device, and motion and orientation information from an accelerometer in a smartphone or game controller.

Output Device:

Output devices are used to get data out of a computer so that it can be examined, analyzed or distributed to others. It converts information from machine-understandable form to a human understandable form. The outputs are of two types: Softcopy : displayed on monitor, projector, or similar devices and Hardcopy : printed on paper

Central Processing unit(CPU):

The central processing unit (CPU) is the computer‘s brain. It retrieves instructions from memory and executes them. The CPU usually has two components: a control unit and an arithmetic/logic unit. The control unit controls and coordinates the actions of the other components. The arithmetic/logic unit performs numeric operations (addition, subtraction, multiplication, division) and logical operations (comparisons).

Today‘s CPU is built on a small silicon semiconductor chip having millions of transistors. Every computer has an internal clock, which emits electronic pulses at a constant rate. These pulses are used to control and synchronize the pace of operations. The higher the clock speed, the more instructions are executed in a given period of time. The unit of measurement of clock speed is the hertz ( Hz ), with 1 hertz equaling 1 pulse per second. The clock speed of a computer is usually stated in megahertz (MHz) (1 MHz is 1 million Hz). CPU speed has been improved continuously. Intel‘s Pentium 3 Processor runs at about 500 megahertz and Pentium 4 Processor at about 3 gigahertz (GHz) (1 GHz is 1000 MHz). Many of today‘s

Computers have multiple CPUs and, hence, can perform many operations simultaneously. A multi- core processor implements multiple processors on a single integrated-circuit chip—a dual-core

Faster than secondary memories. A computer cannot run without the primary memory.

2. Secondary Storage

Main Memory is volatile, because information is lost when the power is turned off. Programs and data are permanently stored on storage devices and are moved, when the computer actually uses them, to main memory, which is much faster than storage devices. CPU directly does not access these memories; instead they are accessed via input-output routines. The contents of secondary memories are first transferred to the main memory, and then the CPU can access it.

There are four main types of storage devices: ■ Disk drives ■ CD drives (CD-R, CD-RW, and DVD) ■ Tape drives ■ USB flash drives Characteristics of Secondary Memory

These are magnetic and optical memories. It is known as the backup memory. It is a non-volatile memory. Data is permanently stored even if power is switched off. It is used for storage of data in a computer. Computer may run without the secondary memory. Slower than primary memories.

1.2 Programming and Programming Languages

Computer programs , known as software , are instructions to the computer, telling it what to do. Computers do not understand human languages, so you need to use computer languages in computer programs. Programming is the creation of a program that is executable by a computer and performs the required tasks.

A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. It is defined through the use of syntactic and semantic rules, to determine structure and meaning respectively. Programming languages are used to facilitate

communication about the task of organizing and manipulating information, and to express algorithms precisely.

Programmers write instructions in various programming languages, some directly understandable by computers and others requiring intermediate translation steps. Hundreds of such languages are in use today. These may be divided into three general types:

  1. Machine languages
  2. Assembly languages
  3. High-level languages

Machine languages

Any computer can directly understand only its own machine language, defined by its Hardware design. Machine languages generally consist of strings of numbers (ultimately reduced to 1s and 0s) that instruct computers to perform their most elementary operations one at a time. Machine languages are machine dependent (a particular machine language can be used on only one type of computer).

These instructions are in the form of binary code, so in telling the machine what to do, you have to enter binary code. Programming in machine language is a tedious process. Moreover, the programs are highly difficult to read and modify. For example, to add two numbers, you might have to write an instruction in binary like this:

1101101010011010

Assembly Languages

Programming in machine language was simply too slow and tedious for most programmers. Instead of using the strings 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. Assembly language is a low-level programming language in which a mnemonic is used to represent each of the machine-language instructions. For example, to add two numbers, you might write an instruction in assembly code like this:

ADDF3 R1, R2, R

■ Delphi (Pascal-like visual language developed by Borland) ■ C++ (an object-oriented language, based on C) ■ C# (a Java-like language developed by Microsoft)

A program written in a high-level language is called a source program or source code. Since a computer cannot understand a source program, a program called a compiler or translator is used to translate it into a machine-language program.

Compiler is a program that translates source code from a high-level programming language to a lower level language (e.g., assembly language or machine language). The machine-language program is then linked with other supporting library code to form an executable file, which can be run on the machine, as shown in Figure 1.4. On Windows, executable files have extension .exe.

Interpreter is a program that translates and executes source language statements one line at a time.

1.3 Program development process

Program development is often a difficult task. There is no complete set of rules, no algorithm to tell you how to write programs. Program development is a creative process. Still, there is the outline of a plan to follow. As indicated there, the entire program development process can be divided into two phases, the problem-solving phase and the implementation phase. The result of the problem-solving phase is an algorithm , expressed in English, for solving the problem. To produce a program in a programming language such as C++, the algorithm is translated into the programming language. Producing the final program from the algorithm is called the implementation phase.

Problem solving Phase

It produces an ordered sequence of steps that describe solution of problem; this sequence of steps is called an algorithm. Example of an Algorithm: a recipe, to assemble a brand new computer...

What else?.

The first step is to be certain that the task—that you want your program to do—is completely and precisely specified. Do not take this step lightly. If you do not know exactly what you want as the output of your program, you may be surprised at what your program produces. Be certain that you know what the input to the program will be and exactly what information is supposed to be in the output, as well as what form that information should be in. For example, if the program is a bank accounting program, you must know not only the interest rate, but also whether interest is to be compounded annually, monthly, daily, or whatever. If the program is supposed to write poetry, you need to determine whether the poems can be in free verse or must be in iambic pentameter or some other meter. Implementation phase

The implementation phase is not a trivial step. There are details to be concerned about, and occasionally some of these details can be subtle, but it is much simpler than you might at first think. Once you become familiar with C++ or any other programming language, the translation of an algorithm from English into the programming language becomes a routine task. As indicated in Display, testing takes place in both phases. Before the program is written, the algorithm is tested, and if the algorithm is found to be deficient, then the algorithm is redesigned. That desktop testing is performed by mentally going through the algorithm and executing the steps yourself. On large algorithms this will require a pencil and paper. The C++ program is tested by compiling it and running it on some sample input data. The compiler will give error messages for certain kinds of errors. To find other types of errors, you must somehow check to see whether the output is correct. The process diagrammed in Display is an idealized picture of the program design process.

  1. Deployment: Once the testing is done, the product is deployed in the customer environment or released into the market.
  2. Maintenance and evolution of the system: There are some issues which come up in the client environment. Also to enhance the product some better versions are released. Maintenance is done to deliver these changes in the customer environment.

1.5 Algorithm, flowchart and Pseudo code

When learning your first programming language it is easy to get the impression that the hard part of solving a problem on a computer is translating your ideas into the specific language that will be fed into the computer. This definitely is not the case. The most difficult part of solving a problem on a computer is discovering the method of solution. After you come up with a method of solution, it is routine to translate your method into the required language, be it C++ or some other programming language. It is therefore helpful to temporarily ignore the programming language and to concentrate instead on formulating the steps of the solution and writing them down in plain English, as if the instructions were to be given to a human being rather than a computer. A sequence of instructions expressed in this way is frequently referred to as an algorithm. A sequence of precise instructions which leads to a solution is called an algorithm.

How do you represent an algorithm? The most obvious representation: source code of a programming language. However, writing source code before you fully understand an algorithm often leads to difficult-to-find bugs. So, algorithms may be presented

1. In words/ Pseudo code

To present the algorithm in words we may describe the tasks step by step. Pseudo code is an artificial and informal language that helps programmers develops algorithms. Pseudo code is very similar to everyday English. Common key always used are following: READ, SET, COMPUTE/CALCULATE, DISPLAY.

2. As a flowchart

A familiar technique for overcoming those bugs involves flowcharts. A flowchart is a visual representation of an algorithm's control flow. That representation illustrates statements that need to execute, decisions that need to be made, logic flow (for iteration and other purposes), and terminals that indicate start and end points.

To present that visual representation, a flowchart uses various symbols, which Figure 1.1 and table 1.1 .shows.

Table 1.1 Flow chart symbols with their description

Fig 1.1 Flowchart symbol for statements, decisions, logic flows, etc.

a. Pseudo code o Read two numbers(A and B) o Add A and B o Assign the result to C o Display result(C) b. Algorithm o Read A,B o C=A+B o Display C

c. Flow Chart

START

READ A, B

C=A+B

DISPLAY

C

END

DECISION STRUCTURES

 The expression inside decision structure is a logical expression like A>B  it describes a condition we want to testif A>B is true (if A is greater than B) we take the action on left of

 print the value of A (see Fig. 1.2)

if A>B is false (if A is not greater than B) we take the action on right

 print the value of B (see Fig. 1.2)

Y A>B N

Print A (^) Print B

b. Algorithm o Read A,B o If A > B  Then C=A  Else C=B o End if o Print ―the largest value is‖, C c. Flowchart

START

Read A, B

A>B

C = A

Y N

C = B

Display C

End

NESTED IFS

 One of the alternatives within an IF–THEN–ELSE statement  may involve further IF–THEN–ELSE statement

Example: Write an algorithm that reads three numbers and prints the value of the

largest number.

Algorithm : Step 1: Input N1, N2, N

Step 2: if N1>N then if N1>N then MAX = N1 [N1>N2, N1>N3] else MAX = N3 [N3>N1>N2] end if else if N2>N then MAX = N2 [N2>N1, N2>N3] else MAX = N3 [N3>N2>N1] end if end if Step 3: Print “The largest number is”, MAX Exercise: Draw the flow chart and write pseudo code of the above Algorithm.

Loop / Iteration

Sometimes there are conditions in which it is necessary to execute a group of statements repeatedly. Until some condition is satisfied. This scenario is called a loop. Loop is a sequence of instruction, which is repeated until some specific condition occurs. A Loop normally consists of four parts. These are:

Initialization: Setting of variables of computation to their initial values

Computation: Processing

c. Flowchart

Assignment I(Group of five members)

I. Algorithms and Flowcharts

  1. What is the advantage of drawing a flow chart?
  2. Write an algorithm to count the number of odd, even and zero in a set of ‗n‘ numbers.
  3. Write an algorithm to read 100 numbers then display the largest.
  4. Draw a flow chart to find sum of natural numbers up to N.

NOTICE: your assignment should submit in the next week.

START

Count

Sum=0, count=

Sum=sum+count er

Count=count+

Display sum

End