Unit-I Introduction to Problem Solving : Problem Solving Aspect, Problem Identification, Problem Understanding, Algorithm Development, Solution Planning, characteristics of algorithm, Top-down approach, Bottom-up approach, Time and space complexities of algorithms, Flowchart. Overview of C : History Of C, C Language Elements, Basic Structure of C Program Tokens-Variables and Data Types, Operators, Expressions and Type Conversions .
Overview of Computer The word “computer” comes from the word “compute”, which means to calculate. Hence, a computer is normally considered to be calculating device, which can perform arithmetic (addition, subtraction…, etc ) operations at enormous speed. Computer is a fast electronic calculating machine that accepts digitized information from the user, processes it according to a sequence of instructions stored in the internal storage, and provides the processed information to the user. Sequence of instructions are stored in the internal storage is called computer program. And internal storage is called computer memory. The cycle of operation of computer is known as the INPUT – PROCESS – OUTPUT and is shown as following figure.
Introduction
Block Diagram of Computer The Fig shows these five functional units of a computer and its physical units of a computer and its physical locations in the computer. The input unit accepts the digital information from the user with the help of input unit devices such as keyboard, mouse, microphone, etc. the information received from the input unit is either stored in the memory for later use or immediately used by the arithmetic and logical unit to perform the desired operations. The program stored in the memory decides the processing steps and the processed output sent to the user with the help of output devices or it is stored in the memory for later reference. All the above maintained activities are coordinated and controlled by the control unit. The arithmetic and logic unit in conjunction with control unit is commonly called central processing unit (CPU).
Basic Parts of Computer
Definition
Characteristics of Computer
Types of Computers
Analog Computers
Digital Computer
Hybrid Computer
Classification of Computers
Generation of Computers First Generation (1945-1954) The main function unit of first generation computers is “Vacuum Tubes” .It is used for developing the circuitry. It comprised of glass and filaments. These computers occupy very large space and consume large amount of electricity power. These computation times was calculated in milliseconds. Advantages : It was only electronic device First device to hold memory Disadvantages : Too bulky i.e large in size Vacuum tubes burn frequently They were producing heat Maintenance problems
Second generation (1955-1964) In this generation, vacuum tubes are replaced by transistor, which is a solid-state device. Transistors are more reliable than vacuum tubes. It occupies less space compare to vacuum tubes. These computation times was calculated in microseconds. These are less expensive and faster than the first-generation computers. Advantages : Size reduced considerably The very fast Very much reliable Disadvantages : They over heated quickly Maintenance problems
Third generation (1964-1980) Third generation computer were used Integrated Circuits(IC). IC is silicon chip that embeds an electronic circuit comprising of several components such as transistors, diodes and resistors etc., these computation times was calculated in nanoseconds. These are more reliable and less expensive. In this generation, computers having high storage capacity and high processing speed. Advantages : ICs are very small in size Improved performance Production cost cheap Disadvantages : ICs are sophisticated
Fourth generation (1981-1990) This computer used Large Scale Integrated Circuits (LSI) and Very Large-Scale Integrated Circuits (VLSI).These circuits are also termed as “Microprocessor chip”. It integrates thousands of components on a single chip. These computers are very small in size. These are used for several applications like mathematical, modulating, computer aided designing etc Advantages : Increased storage capacity Improved performance Production cost cheap Disadvantages : Complexity Dependence on Electricity.
Fifth generation (1991- till date) The fifth-generation computers are developed based on a technique called Artificial Intelligence (AI). These are mainly used in robots. The input and output information for these computers will be in the form of speech and graphic images etc. These computers are used for some specialized applications. Advantages Disadvantages Artificial Intelligence Cost Speed Complexity Power Efficiency Dependence on Artificial Intelligence Multitasking Security Risks Natural Language Processing Ethical Concerns
Introduction to Problem Solving Today, computers are all around us. We use them for doing various tasks in a faster and more accurate manner. For example, using a computer or smartphone, we can book train tickets online. India is a big country and we have an enormous railway network. Thus, railway reservation is a complex task. Making reservation involves information about many aspects, such as details of trains (train type, types of berth and compartments in each train, their schedule, etc.), simultaneous booking of tickets by multiple users and many other related factors. It is only due to the use of computers that today, the booking of the train tickets has become easy. Online booking of train tickets has added to our comfort by enabling us to book tickets from anywhere, anytime. Computers are used for solving various day-to-day problems and thus problem solving is an essential skill that a Engineering student should know.
It is pertinent to mention that computers themselves cannot solve a problem. Precise step-by-step instructions should be given by us to solve the problem. Thus, the success of a computer in solving a problem depends on how correctly and precisely we define the problem, design a solution (algorithm) and implement the solution (program) using a programming language. Thus, problem solving is the process of identifying a problem, developing an algorithm for the identified problem and finally implementing the algorithm to develop a computer program.
Steps for Problem Solving Suppose while driving, a vehicle starts making a strange noise. We might not know how to solve the problem right away. First, we need to identify from where the noise is coming? In case the problem cannot be solved by us, then we need to take the vehicle to a mechanic. The mechanic will analyze the problem to identify the source of the noise, make a plan about the work to be done and finally repair the vehicle in order to remove the noise. From the above example, it is explicit that, finding the solution to a problem might consist of multiple steps. When problems are straightforward and easy, we can easily find the solution. But a complex problem requires a methodical approach to find the right solution. In other words, we have to apply problem solving techniques. Problem solving begins with the precise identification of the problem and ends with a complete working solution in terms of a program or software. Key steps required for solving a problem using a computer are shown in Figure.
Analyzing the problem It is important to clearly understand a problem before we begin to find the solution for it. If we are not clear as to what is to be solved, we may end up developing a program which may not solve our purpose. Thus, we need to read and analyze the problem statement carefully in order to list the principal components of the problem and decide the core functionalities that our solution should have. By analyzing a problem, we would be able to figure out what are the inputs that our program should accept and the outputs that it should produce.
Developing an Algorithm It is essential to device a solution before writing a program code for a given problem. The solution is represented in natural language and is called an algorithm. We can imagine an algorithm like a very well-written recipe for dish, with clearly defined steps that, if followed, one will end up preparing the dish. We start with a tentative solution plan and keep on refining the algorithm until the algorithm is able to capture all the aspects of the desired solution. For a given problem, more than one algorithm is possible and we have to select the most suitable solution
Algorithm A set of sequential steps usually written in Ordinary Language to solve a given problem is called Algorithm . It may be possible to solve to problem in more than one ways, resulting in more than one algorithm. The choice of various algorithms depends on the factors like reliability, accuracy and easy to modify. The most important factor in the choice of algorithm is the time requirement to execute it, after writing code in High-level language with the help of a computer. The algorithm which will need the least time when executed is considered the best.
Steps involved in algorithm development An algorithm can be defined as “a complete, unambiguous, finite number of logical steps for solving a specific problem “ Step1. Identification of input : For an algorithm, there are quantities to be supplied called input and these are fed externally. The input is to be identified first for any specified problem. Step2: Identification of output : From an algorithm, at least one quantity is produced, called for any specified problem. Step3 : Identification the processing operations : All the calculations to be performed in order to lead to output from the input are to be identified in an orderly manner. Step4 : Processing Definiteness : The instructions composing the algorithm must be clear and there should not be any ambiguity in them.
Step5 : Processing Finiteness : If we go through the algorithm, then for all cases, the algorithm should terminate after a finite number of steps. Step6 : Possessing Effectiveness : The instructions in the algorithm must be sufficiently basic and in practice they can be carries out easily. An algorithm must possess the following properties 1. Finiteness : An algorithm must terminate in a finite number of steps 2. Definiteness : Each step of the algorithm must be precisely and unambiguously stated 3. Effectiveness : Each step must be effective, in the sense that it should be primitive easily convert able into program statement) can be performed exactly in a finite amount of time. 4. Generality : The algorithm must be complete in itself so that it can be used to solve problems of a specific type for any input data. 5. Input/output : Each algorithm must take zero, one or more quantities as input data produce one or more output values. An algorithm can be written in English like sentences or in any standard representation sometimes, algorithm written in English like languages are called Pseudo Code
Some of the words used while writing the algorithm are: Some of the words used while writing the algorithm are: Start/Begin Read/Accept Print/Write Compute/Calculate Stop/End
Start/Begin The start/Begin is the words used at the beginning of the algorithm. 2. Read/Accept These words are used to input the data from keyboard or to data from backup storage devices. 3. Print/Write These words are used to output the information onto the screen or to write the information onto the backup storage devises. 4. Compute/Calculate These words are used at expression evaluation or mathematical calculations. 5. Stop/End These words are used at the end of the algorithm.
Analyzing an Algorithm Analyzing an algorithm refers to calculating or guessing the resources such as computer memory, processing time, logic gate and so on. The analysis can also made by reading the algorithm for logical accuracy, tracing the algorithm, implementing it, and checking with some data and with mathematical technique to conform its accuracy. It is very essential to consider the factors such as time and space requirements of an algorithm. An efficient algorithm must be developed utilizing minimum system resources such as CPU time and memory.
Example:1 Suppose we want to find the average of three numbers, the algorithm is as follows Step 1 Read the numbers a, b, c Step 2 Compute the sum of a, b and c Step 3 Divide the sum by 3 Step 4 Store the result in variable d Step 5 Print the value of d Step 6 End of the program
Example:2 Area of Triangle : Write an algorithm to find the area of the triangle. Let b, c be the sides of the triangle ABC and A the included angle between the given sides. Step 1: Input the given elements of the triangle namely sides b, c and angle between the sides A. Step 2: Area = (1/2) *b*C* sin A Step 3: Output the Area Step 4: Stop.
Example 3 Write an algorithm to calculate the perimeter and area of rectangle. Given its length and width. Step 1: Read length of the rectangle. Step 2: Read width of the rectangle. Step 3: Calculate perimeter of the rectangle using the formula perimeter = 2*(length + width) Step 4: Calculate area of the rectangle using the formula area = length *width. Step 5: Print perimeter. Step 6: Print area. Step 7: Stop.
Example 4 Write an algorithm to calculate the simple interest using the formula Simple interest = P*N* R/100 Where P is principle Amount, N is the number of years and R is the rate of interest. Step 1: Read the three input quantities’ P, N and R. Step 2 : Calculate simple interest as Simple interest = P* N* R/100 Step 3: Print simple interest. Step 4: Stop.
Example 5 Write an algorithm to determine whether the given year is a leap year or not Step 1: Start Step 2: Read an year value from the user. Step 3: Calculate the remainder of the year divided by 4(%) Step 4: If remainder is 0 go to step 5 else go to step 6 Step 5: Print year is a leap year and go to step 7 Step 6: Print year is not a leap year. Step 7: Stop.
Example 6 Write an algorithm to determine whether the given number is odd or not Write an algorithm for determining whether there is profit or loss during the selling of an item. Write an algorithm for finding the area of a circle.
Flow Chart A flow chart can be defined as the pictorial representation of a process, which describes the sequence and flow of control and information with in the process. The flow of information is represented inside the flow chart in a step by step form.
Symbols Used in Flowcharts The symbols that we make use while drawing flowcharts as given below are as per conventions followed by International Standard Organization (ISO). Oval : Rectangle with rounded sides is used to indicate either START/ STOP of the program.
Input and output indicators : Parallelograms are used to represent input and output operations. Statements like INPUT, READ and PRINT are represented in these Parallelograms. Process Indicators : - Rectangle is used to indicate any set of processing operation such as for storing arithmetic operations.
Decision Makers : The diamond is used for indicating the step of decision making and therefore known as decision box. Decision boxes are used to test the conditions or ask questions and depending upon the answers, the appropriate actions are taken by the computer. The decision box symbol is Flow Lines: Flow lines indicate the direction being followed in the flowchart. In a Flowchart, every line must have an arrow on it to indicate the direction. The arrows may be in any direction
On- Page connectors : Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use connectors to avoid any confusions. It is represented by a circle.
Advantages of Flowchart: Flowcharts are a better way of communicating the logic of the system. Flowcharts act as a guide for blueprint during program designed. With the help of flowcharts programs can be easily analyzed. It provides better documentation. Flowcharts serve as a good proper documentation. Easy to trace errors in the software. Easy to understand. The flowchart can be reused for inconvenience in the future. It helps to provide correct logic.
Disadvantages of Flowchart: It is difficult to draw flowcharts for large and complex programs. There is no standard to determine the amount of detail. Difficult to reproduce the flowcharts. It is very difficult to modify the Flowchart. Making a flowchart is costly. Some developer thinks that it is waste of time. It makes software processes low. If changes are done in software, then the flowchart must be redrawn.
Example 1 Print Hello world on Screen
Example 2 Calculate the average of two numbers
Example 3 Input Number And Check If they Are Odd Or Even
HISTORY OF C C language was developed by Dennis M Ritchie, in 1972, at AT&T Bell labs. C language was derived from B language which was developed by ken Thomson in 1970. B language was adopted from the language BCPL (basic combined programming language) which was developed by martin Richards at Cambridge University. The language B named as so by borrowing the first initial from BCPL language. BCPL was a response to difficulties with its predecessor Combined Programming Language (CPL), created during the early 1960s. Richards created BCPL by "removing those features of the full language which make compilation difficult". CPL was developed jointly between the Mathematical Laboratory at the University of Cambridge.
It was heavily influenced by ALGOL 60, but instead of being extremely small, elegant and simple, CPL was intended for a wider application area than scientific calculations and was therefore much more complex than and not as elegant as ALGOL 60. C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. Its design provides constructs that map efficiently to typical machine instructions, and therefore it found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.