H-2.pdf

Please download to get full document.

View again

of 5
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Categories
Published
ChE-101 H-2: Flowchart Diagrams (last updated April 05, 2005) This handout contains important information for the development of flowchart diagrams Common Symbols for Algorithms The first step before developing any program in Matlab or other programming software is to sketch the program through a flowchart. Building a flowchart or algorithm decreases the time employed to cons
   1 ChE-101 H-2: Flowchart Diagrams (last updated April 05, 2005) This handout contains important information for the development of flowchart diagrams Common Symbols for Algorithms The first step before developing any program in Matlab or other programming software is to sketch the  program through a flowchart. Building a flowchart or algorithm decreases the time employed to construct the program and reduces the number or errors that we make in the program. The algorithm must underline  the logic of the program. It is a way to make our thoughts clear before writing any lines in the code. Common Flowchart Symbols  The symbols in the flowchart diagram are connected by arrows which indicate the flow of the information in the program. All the input variables should be given at the beginning of the program. The answer of the  problem should be printed at the end of the program. It is important that you define the variables used in the flowchart diagram in a legend. The example given below shows the flowchart diagram for an algorithm to calculate the area of a circumference given the radius: Start or End Program (and/or loops) Input data (by user) Process, calculate, etc Making Decisions Print Startr  A = π r  2 Legend:r: radius of circumference, cm A: area of circumference, cm 2  AendStartStartr  A = π r  2  A = π r  2 Legend:r: radius of circumference, cm A: area of circumference, cm 2  A Aendend   2  Sometimes the flowchart diagrams are too long to fix in a continuous vertical line. If that is the case you can breakdown the diagram by using Greek letters and continue writing the diagram in the horizontal space. The example given below shows how to breakdown the flowchart diagram to calculate the area of a circumference to utilize the horizontal space of the paper. Everybody thinks in a different way, therefore, there are different ways to achieve a solution for a  problem when programming. The best way to learn how to build flowchart diagrams is by solving  problems. Let us solve the exercises given below together: 1.   Class Exercise: Simple Flowchart Diagram . Develop a flowchart diagram to calculate the volume of a gas using ideal gas law. Relational operators: Matlab uses the following relational operators. The relational operators are used only in decision making loops  and while loops . They are not arithmetic operators. <: less than, for example A < B means that A is less than B >: greater than, for example A > B means that A is greater than B <=: less than or equal to, for example A <= B means that A is less than or equal to B >=: greater than or equal to, for example A >= B means that A is greater than or equal to B = =: equal to, for example A = = B means that A is equal to B ~ =: not equal to, for example A ~ = B means that A is not equal to B Decision making (logic commands) . Most of the time the user has to choose among multiple options, in this case making decisions commands must be used. The decision symbol should be used (this command is used to select from among 2 or more options). This structure is known in Matlab as the if/else syntax. We will practice the syntax structure in the computer lab. During the lecture we are going to focus on the development of the flowchart diagram only. An example of a decision making loop is given below. Startr  A = π r  2 Legend:r: radius of circumference, cm A: area of circumference, cm 2 α  Aend α StartStartr  A = π r  2  A = π r  2 Legend:r: radius of circumference, cm A: area of circumference, cm 2 α  Aend Aend A Aendend α   3   Can you tell what the algorithm will do by looking at the above flowchart diagram?  Notice that the decision making loop starts and finishes with specific symbols. Would the algorithm work if the flowchart diagram is written like this? Justify your answer 2.   Class Exercise: Use of decision making . Develop a flowchart diagram that will allow the user to calculate any of the following information by using ideal gas law: temperature, volume, and  pressure. While loops. This structure is used to repeat a set of commands an undetermined number of times. The number of times the loop will be repeated (loop counter variable) should be defined in the program in the “while” command. It is a good practice to set a default variable to the loop counter variable to guarantee that your loop will run. The “while loop” starts and ends with the symbol IMPORTANT: The loop counter variable should change inside the while loop otherwise the loop will run forever. Below are given three different cases of the use of the while loop. Can you tell what the algorithm will If x= 0yesnoSiHolameans hello in Spanishend if x = 1If x= 0yesnoSiHolameans hello in Spanishend if If x= 0If x= 0yesnoSiSiHolameans hello in SpanishHolameans hello in Spanishend if x = 1 If x== 0yesnoSiHolameans hello in Spanishend if x = 1If x== 0yesnoSiHolameans hello in Spanishend if If x== 0If x== 0yesnoSiSiHolameans hello in SpanishHolameans hello in Spanishend if x = 1   4  print by looking at the different flowchart diagrams? 3.   Class Exercise: Use of while loop . The ideal gas law is only valid at low pressures. Develop a flowchart diagram that will allow the user to calculate the volume of the gas using ideal gas low only is the pressure is lower than 2 atm. Use a while loop to do this. If the pressure is higher than 2 atm the program should print an error message “Pressure higher than 2 atm, ideal gas law is not valid” for loops. This structure is used to repeat a set of commands a determined number of times. The number of times the loop will be repeated (loop counter variable) should be defined in the program in the “for” command. The “for loop” starts and ends with the symbol The range for the loop counter is set as follows:  for n=1:5  (This means that n will start at 1 and the loop will be repeated until n = 5. Each time the loop runs it automatically adds 1 to the loop counter “n” If you want a different increment instead of 1 you should use the following  for s = 1.0: -0.1: 0.0  This means that the loop will start with s = 1.0, it will decrease the value of s until s = 0 by subtracting -0.1 each time the loop is repeated. Can you tell what the algorithm will print by looking at the flowchart diagram? N=1 t=2While (t<4)t=t*2N=N+1End WhileN, tN=1 t=2While (t<=4)t=t*2N=N+1End WhileN, tN=1 t=2While (t<=4)N=N+1End WhileN, tCase ACase BCase CN=1 t=2While (t<4)t=t*2N=N+1End WhileN, tN=1 t=2N=1 t=2While (t<4)While (t<4)t=t*2N=N+1t=t*2N=N+1End WhileEnd WhileN, tN=1 t=2While (t<=4)t=t*2N=N+1End WhileN, tN=1 t=2N=1 t=2While (t<=4)While (t<=4)t=t*2N=N+1t=t*2N=N+1End WhileEnd WhileN, tN=1 t=2While (t<=4)N=N+1End WhileN, tN=1 t=2N=1 t=2While (t<=4)While (t<=4)N=N+1N=N+1End WhileEnd WhileN, tCase ACase BCase C Length = 2for (n=1:0.5:length)y=y+nEnd for n, yLength = 2Length = 2for (n=1:0.5:length)for (n=1:0.5:length)y=y+ny=y+nEnd for End for n, y
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks