











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Problem solving using algorithm
Typology: Lecture notes
1 / 19
This page cannot be seen from the preview
Don't miss anything!












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
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 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
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.
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:
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.
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.
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
The expression inside decision structure is a logical expression like A>B it describes a condition we want to test if A>B is true (if A is greater than B) we take the action on left of
if A>B is false (if A is not greater than B) we take the action on right
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
Display C
End
NESTED IFS
One of the alternatives within an IF–THEN–ELSE statement may involve further IF–THEN–ELSE statement
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
NOTICE: your assignment should submit in the next week.
START
Sum=0, count=
Sum=sum+count er
Count=count+
End