Problem Solving and Program Design using “C” lanaguage Class: B.tech Integrated CSE Semester : I Course Outcomes 1. Design algorithm, flowchart and write pseudo code for given problem statements. 2.Use basic ‘C’ data concepts for given problem statements. 3.Design algorithms and Develop C programs using selection structures. 1. Design algorithms and Develop C programs using Loops Unit I Algorithms and Flowchart Prof. Sulakshana S.Malwade Department of Computer Engineering School of Polytechnic & Skill Development Course Outcome CO1: Design algorithm, flowchart and write pseudo code for given problem statements. Course Outcomes: CO 1: Apply Software Development Method to solve the given problem statements Unit Outcomes Points to be Covered Software Development Method Generation of programming languages Applying Software Development Method Algorithm: Expressing algorithm, Benefits of using Algorithm Flowchart: Advantages of using flowchart, Limitations of using flowchart, Flowchart symbols and Guidelines Introduction to Pseudocode Computer Systems A computer is a system made of two major components: Hardware : The computer hardware is the physical equipment. Software : The software is the collection of programs (instructions) that allow the hardware to do its job. Computer Hardware Hardware of a computer consist of the following components: Main memory Secondary memory, which includes storage devices such as hard disks, CDs, DVDs, and flash drives. Central processing unit. Input devices, such as keyboards, mouse, touch pads , scanners, joysticks Output devices, such as monitors, printers, and speakers. Computer Software Computer Languages To write a program for a computer, we must use a computer language. Computer Languages First Generation Language : The first generation languages are also called machine languages/ 1G language. This language is machine-dependent. The machine language statements are written in binary code (0/1 form) because the computer can understand only binary language Advantages Disadvantages . 1. Fast & efficient as statements are directly written in binary language 1. Difficult to learn binary codes. 2. No translator is required 2. Difficult to understand – both programs & where the error occurred Computer Languages Second Generation Language : The second-generation languages are also called assembler languages/ 2G languages. Programmers can write the code using symbolic instruction codes that are meaningful abbreviations of mnemonics. Assembly language contains human-readable notations that can be further converted to machine language using an assembler. . Computer Languages Second Generation Language : Advantages . Disadvantages 1. It is easier to understand if compared to machine language 1. Assembler is required 2. Modifications are easy. 2. This language is architecture /machine-dependent, with a different instruction set for different machines. 3. Correction & location of errors are easy. Computer Languages Third Generation Language : The third generation is also called procedural language /3 GL. It consists of the use of a series of English-like words that humans can understand easily, to write instructions. Its also called High-Level Programming Language. For execution, a program in this language needs to be translated into machine language using Compiler/ Interpreter. . Computer Languages Third Generation Language : Advantages . 1. Use of English-like words makes it a human-understandable language. 2. Lesser number of lines of code as compared to above 2 languages. 3. Same code can be copied to another machine & executed on that machine by using compiler-specific to that machine. Disadvantages 1. Compiler/ interpreter is needed. 2. Different compilers are needed for different machines. Computer Languages Fourth Generation Language : The fourth-generation language is also called a non – procedural language/ 4GL. It enables users to access the database. Examples: SQL, Foxpro, Focus, etc. These languages are also human-friendly to understand. Advantages Disadvantages Easy to understand & learn. 1. Memory consumption is high. Less time required for application creation. . It is less prone to errors. 2. Has poor control over Hardware. 3. Less flexible. Computer Languages Fifth Generation Language : The fifth-generation languages are also called 5GL. It is based on the concept of artificial intelligence. It uses the concept that that rather than solving a problem algorithmically, an application can be built to solve it based on some constraints.Ex-Prolog,LISP,OPS5,Mercury programming lang Advantages Disadvantages 1.Machines can make decisions. 1.Complex and long code. 2.Programmer effort reduces to solve a .problem. 2.More resources are required & they are expensive too. 3.Easier than 3GL or 4GL to learn and use. Easy to understand & learn. Software Development Method Programming is a problem-solving activity. Phases of Software Development Method: 1. Specify the problem requirements. 2. Analyze the problem. 3. Design the algorithm to solve the problem. 4. Implement the algorithm. 5. Test and verify the completed program. 6. Maintain and update the program. 1. Specify the problem requirements. (PROBLEM) 1. Specify the problem requirements. Specifying the problem requirements forces you to state the problem clearly and unambiguously and to gain a clear understanding of what is required for its solution. 2. Analyze the problem. ( ANALYSIS) Analyzing the problem involves identifying the problem (a) inputs: that is, the data you have to work with; (b) outputs: that is, the desired results; and (c) any additional requirements or constraints on the solution. At this stage, you should also determine the required data format problem variables and their relationships (formulas). If steps 1 and 2 are not done properly, you will solve the wrong 3. Design the algorithm to solve the problem. (DESIGN) Design to solve the problem requires you to develop a list of steps called an algorithm. Use top-down design (also called divide and conquer ): First list the subproblems, Then solve each of those subproblems. Most computer algorithms consist of at least the following subproblems. ALGORITHM FOR A PROGRAMMING PROBLEM 1. Get the data. 2. Perform the computations. 3. Display the results 4. Implement the algorithm. ( IMPLEMENTATION) Implementing the algorithm involves writing it as a program. You must convert each algorithm step into one or more statements in a programming language. 5. Test and verify the completed program. . (TESTING) Testing and verifying the program requires testing the completed program to verify that it works as desired. Don’t rely on just one test case. Run the program several times using different sets of data to make 6. Maintain and update the program.( MAINTENANCE) Modifying a program To remove previously undetected errors And to keep it up-to-date as company policies change Or to keep it up-to-date as government regulations Applying Software Development Method CASE STUDY: Converting Miles to Kilometers 1. PROBLEM Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion. Applying Software Development Method 2. ANALYSIS The problem input is distance in miles and the problem output is distance in kilometers . DATA REQUIREMENTS : Problem Input : miles /* the distance in miles*/ Problem Output : kms /* the distance in kilometers */ Relevant Formula : 1 mile = 1.609 kilometers Applying Software Development Method 3. DESIGN ALGORITHM WITH REFINEMENTS 1. Get the distance in miles. 2. Convert the distance to kilometers. ◦ 2.1 The distance in kilometers is 1.609 times the distance in miles. 3. Display the distance in kilometers. Applying Software Development Method 4. IMPLEMENTATION Writing a program code Applying Software Development Method 5. TESTING In this sample run, a distance of 10.0 miles is converted to 16.09 kilometers, as it should be. To verify that the program works properly, enter a few more test values of miles. Algorithm Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. Input Finite ordered list of steps Output Characteristics of an Algorithm Characteristics of an Algorithm Precise Input Output Finiteness Independent Algorithm Advantages of Algorithms: It is a step-wise representation of a solution to a given problem, which makes it easy to understand. An algorithm uses a definite procedure. It is not dependent on any programming language, so it is easy to understand for anyone even without programming knowledge. Every step in an algorithm has its own logical sequence so it is easy to debug. By using algorithm, the problem is broken down into smaller pieces or steps hence, it is easier for programmer to convert it into an actual program. Algorithm Disadvantages of an Algorithm Algorithms is Time consuming. Difficult to show Branching and Looping in Algorithms. Big tasks are difficult to put in Algorithms. Flowchart Flowchart is a graphical representation of an algorithm. It makes use of symbols which are connected among them to indicate the flow of information and processing. Advantages of Flowchart Disadvantages of Flowchart Effective Communication Complex logic Effective Analysis Alterations and modifications Easy debugging and efficient testing Efficient Coding Proper documentation Efficient Program Maintenance Flowchart Symbols Symbol Description Terminal The oval symbol indicates Start, Stop and Halt in a program’s logic flow. Terminal is the first and last symbols in the flowchart. Input/Output Used for denoting program input and output Processing A box represents arithmetic instructions. All arithmetic processes such as adding, subtracting, multiplication and division are indicated by action or process symbol. Flowchart Symbols Symbol Description Decision Diamond symbol represents a decision point. Decision based operations such as yes/no question or true/false are indicated by diamond in flowchart. Connector Connects two or more parts of a flowchart, which are on the same page.. Guidelines for Preparing Flowchart Standard symbols should be used while drawing flowchart. Ensure that flowchart has START (or BEGIN) and STOP (or END). Flowchart should be neat, clean and easy to follow. The usual direction of flowchart is from top to bottom or from left to right. The terminal symbol, that is, START/BEGIN or STOP/END should have only one flow line. Only one flow line should come out from process symbol. Only one flow line should enter a decision symbol, but two or three flow-lines, one for each possible answer, can leave the decision symbol. Guidelines for Preparing Flowchart Avoid intersection of flow lines. Use annotation symbol to describe steps more clearly. Algorithm and flowchart Step 1: Start. Step 2: Read A, B. Step 3: Sum=A+B. Step 4: Print Sum. Step 5: Stop Pseudocode Pseudo means not real. Pseudocode means not real code. It is a kind of structured English for describing algorithm. Pseudocode is a mix of technical terms and common English that facilitates computer programmers to write computer algorithms. Pseudocode can not be executed or compiled by any compiler, interpreter, or assembler. With the help of pseudocode, we can write an algorithm using simple English. It allows a specific programming language developer or programmer could to understand the logic of the program and implement it in a specific programming language. Pseudocode The main constructs of pseudocode These constructs — also called keywords —are used to describe the control flow of the algorithm. 1.SEQUENCE :represents linear tasks sequentially performed one after the other. For eg Start Action 1 …………. 2 . . . Action N Stop Pseudocode The main constructs of pseudocode These constructs — also called keywords —are used to describe the control flow of the algorithm. 2.Selection(Decision) :The execution of a set of statements is done according to a pre-specified condition. The selection structure is also known as decision-making structure because the decision to execute a particular set of the statement is made on the basis of the conditional statement. Three categories are there: If-then construct If-then-else construct Case construct Pseudocode If-then construct This construct uses if and Then clauses to represent a condition as well as a set of statements. In the If clause, the conditional statement is written, while in Then clause the set of statements to be executed are specified. If-else construct This construct is very much similar to the If-Then selection structure. The only difference between the two is that in If-Then-Else selection structure two sets of statements are specified. Pseudocode IF (condition is True) THEN List of Actions ELSE List of Different Actions ENDIF IF HoursWorked > NormalMax THEN Display overtime message ELSE Display regular time message ENDIF Case Construct: In this selection construct, multiple sets of statements are specified. The selection of a particular set of statements is made on the basis of the value of the variable given at the beginning of the selection structure. Pseudocode The general form is: CASE expression OF condition 1 : sequence 1 condition 2 : sequence 2 ... condition n : sequence n OTHERS: default sequence ENDCASE CASE grade OF A: points = 4 B: points = 3 C: points = 2 D: points = 1 OTHERS : points = 5 ENDCASE Pseudocode 3. Repetition(Looping) Construct: Looping construct in pseudo-code is used when some particular task is to be repeated for a number of times according to the specified condition. Do-While looping construct, a set of statements is given in the Do block and a condition is given in the Do block are executed till the given condition is true. If the condition is true, then only the block statements are executed, otherwise, the repetition structure is terminated. DO WHILE Condition is True 1st Statement 2nd Statement . . Nth Statement ENDDO Pseudocode 3. Repetition(Looping) Construct: The Repeat-Until looping construct is similar to the Do-While repetition structure. In this structure, the repetitive execution of statements given in the Repeat clause occurs only when the condition given in the Until clause is false. Pseudocode: REPEAT 1st Sequence 2nd Sequence . . Nth Sequence UNTIL Condition is False Pseudocode Advantages of Pseudocode Pseudo code is easier and simpler to understood by the programmers of all types. It cannot be compiled into an executable program. Compared to a flowchart, it is easier to modify the pseudo-code of program logic whenever program modifications are necessary. Converting a pseudo code to a programming language is very easy as compared with converting flowchart to a programming language. To develop a pseudo-code it requires less time and effort than other programming tools such as flowchart. Pseudocode Disadvantages of Pseudocode Pseudocode is textual representation of an algorithm. It does not provide a graphical or visual representation. . There are no standard rules to follow in using the Pseudo code Pseudocode Special Keyword: START: To begin the pseudocode. Common Action Keywords: Input: READ OBTAIN GET Output: PRINT DISPLAY SHOW Compute: COMPUTE CALCULATE DETERMINE Initialize: SET INIT Add one: INCREMENT BUMP Pseudocode Example. Add Two Numbers BEGIN NUMBER s1, s2, sum OUTPUT("Input number1:") INPUT s1 OUTPUT("Input number2:") INPUT s2 sum=s1+s2 OUTPUT sum END Pseudocode START 2. DISPLAY "Enter the Number - " 3. READ number 4. IF number MOD 2 = 0 THEN DISPLAY "Number is Even" ELSE DISPLAY "Number is Odd" END IF 5. STOP THANK YOU