25 views

Uploaded by Secret Squirrel

MatLab

- ANDRIOD
- Seminar Networking
- Looping
- Car Database SQL
- Seminar on Networking
- CAR RENTAL & HOTAL BOOKING APPLICATION
- common bus system
- Java Programs
- Cryptography
- Car Rental
- Advanced Networking and Communication
- Basic Networking Topics
- Command Line Utilities - DNS,DHCP,TCP/IP
- PLsql nested tables
- Instruction Cycle
- Bus Reservation
- TCP /IP
- Hackingzones.com-Extended LoopForeach Program in Java
- Matter for Bus Reservation System Final report
- SRS- Smart City

You are on page 1of 7

by John R. Glover A software pattern is a reusable solution to a recurring problem that arises within a certain context and system of forces. Patterns can be described at three levels: architectural patterns, design patterns, and programming patterns. Programming patterns (idioms , coding patterns ) are low-level patterns specific to a programming language. Experienced programmers automatically call upon these patterns when they see that one is appropriate to the task. A pattern has (at least) a name, a problem to be solved, a standard solution, and examples that illustrate it. (For these low-level patterns, we will omit the context and forces.) Let's look at a few. Since these are all loop programming patterns, for simplicity they will be expressed in terms of problems involving values in an array. Name: Loop Condition Problem: A test must be applied to each value in a vector. Solution: Use an if statement to apply the test inside a for loop. Example: % Display all negative values in the vector for n = 1:length(x) if x(n) < 0 disp(x(n)) end end Name: Counter Problem: It is necessary to count occurrences of some condition in the loop. Solution: Maintain a separate counter variable. Example: % Count the negative values counter = 0; %initialize counter for n = 1:length(x) if x(n) < 0 count = count + 1; %increment counter end end

Page 1 of 7

4/27/01 Name: Compute from Index Problem: A value must change with each pass through the loop, but that value is not the loop variable. Solution: Compute the value using the loop variable. Example: % Compute values of sin(t) for n = 1:101 t = (n-1)*2*pi/100; %compute from loop variable y(n) = sin(t); end Name: Separate Index Problem: The loop variable is not suitable for an array index, but one is needed. Solution: Maintain a separate array index. Example: % Compute values of sin(t) n = 1; %initialize separate index for t = 0:2*pi/100:2*pi y(n) = sin(t); n = n + 1; %increment index end Name: Second Index (similar to Separate Index) Problem: It is necessary to index through two arrays at a different rate, so the loop variable can be used for only one. Solution: Maintain a second array index incrementing at the different rate. Example: % Copy just the negative values into a new vector index2 = 1; %initialize second index for n = 1:length(x) if x(n) < 0 y(index2) = x(n); index2 = index2 + 1; %incr second index end end

Page 2 of 7

4/27/01 Name: Flag Variable Problem: It is necessary to mark the occurrence of an event in the loop so that you know it has occurred, either on the next pass through the loop or when you exit the loop. Solution: Maintain a Boolean flag variable. The flag's state indicates whether the event has occurred Example (On exit from loop, the flag indicates whether the event occurred in the loop.): % Look for a specified value in the vector target = input('Enter value to be found:'); found = 0; %not found yet for n = 1:length(x) if x(n)==target found = 1; %found end end if found do something end Example (On each pass through the loop, the flag indicates whether the event occurred on a previous pass through the loop, perhaps putting the code in a different "mode."): % How many times does a positive follow a negative % in the vector x? count = 0; negfound = 0; % flag=1 if negative found last pass for n = 1:length(x) % First, was a negative found on previous pass? if negfound if x(n)>0 %...yes, so is this now a positive? count = count + 1; end end % In any case, set flag for next pass negfound = x(n)<0; % true only if this value negative end disp(count)

Page 3 of 7

4/27/01 Name: Early Exit Problem: Part way through the loop, there is no longer a need to continue it. Solution: Exit early when the exit condition is detected (Flag Variable is probably needed so that you will know that the early exit occurred). Example: % Look for a specified value in the vector target = input('Enter value to be found:'); found = 0; %not found yet for n = 1:length(x) if x(n)==target found = 1; %found break; end end if found do something end

Name: Early Return Problem: Part way through execution of a function, the answer is computed and there is no need to continue the function. Solution: Return immediately without executing the remainder of the code. Example: function result = sumsquares(x) % SUMSQUARES Return avg of squares of x values unless % one of them is negative, in which case return zero total = 0; for n = 1:length(x) if x(n)<0 result = 0; return % oops, a zero, immediate return else total = total + x(n)^2; end result = total/length(x)

Page 4 of 7

4/27/01 Name: Extreme Value Problem: A value must be located that stands out from every other value encountered in the loop. Solution: Maintain a separate extreme so far variable, initialized to the first value. Example: % Find the largest value in the vector maxval = x(1); %largest so far for n = 2:length(x) %continue with 2nd value if x(n) > maxval maxval = x(n); %replace with new max value end end Name: Extreme Initial Value Problem: We want to know which value best meets a condition, but we cannot easily (or do not wish to) initialize to a first candidate. Solution: Initialize the answer to an extreme or impossible value, knowing that it will be updated on the first pass through the loop. Example: % Find the largest absolute value in the vector absmax = -1; %will be changed immediately for n = 1:length(x) %start with the first if abs(x(n)) > absmax absmax = abs(x(n)); %replace end end Name: Save Index Problem: The result desired is the index of the array value meeting the condition. Solution: Use a separate index variable to save the desired index. Example: % Clear the last negative value in the vector to zero saveIndex = 0; %no index yet for n = 1:length(x) if x(n) < 0 saveIndex = n; %save new index end end if saveIndex > 0 x(saveIndex) = 0; %clear value at last index end Note: This is combined with a variation of the Flag Variable pattern, since the nonzero value of saveIndex indicates that a negative value was found.

Page 5 of 7

4/27/01 Name: Factor Out Invariants Problem: In a loop it is necessary to repeatedly use the result of a function call or other computation, although that result does not change. Solution: Improve efficiency by taking the function call or computation outside the loop. Example, inefficient solution: % Find the value closest to target value: cos(pi/5) closest = x(1); %closest so far for n = 2:length(x) %continue with 2nd value if abs(x(n)-cos(pi/5)) < abs(closest-cos(pi/5)) closest = x(n); %save new closest value end end disp(['Closest is:' num2str(closest)]) Example, factoring out the invariant computations : % Find the value closest to target value: cos(pi/5) closest = x(1); %closest so far target = cos(pi/5); distance = abs(closest-target); for n = 2:length(x) %continue with 2nd value if abs(x(n)-target) < distance closest = x(n); %save new closest value distance = abs(closest-target); % and new distance end end disp(['Closest is:' num2str(closest)]) Note: The Extreme Value pattern is also used in this example.

Page 6 of 7

4/27/01 Name: Swap/Exchange Problem: We need to exchange two values in a vector. Solution: Perform the exchange through a separate swap variable. Example: % Reverse the elements in a vector for n = 1:fix(length(x)/2) m = length(x)-n+1; %index backwards from end swap = x(n); x(n) = x(m); x(m) = swap; end Note: The Second Index pattern is also used in this example. Note: Efficiency of this example could be improved by using the Factor Out Invariants pattern: % Reverse the elements in a vector len1 = length(x)+1; %precompute invariant for n = 1:fix(length(x)/2) m = len1-n; %index backwards from end swap = x(n); x(n) = x(m); x(m) = swap; end Name: Two Questions Problem: We want a complex result that is not easily answered by a single pattern. Solution: Recognize that two (or more) questions are really being asked, and devise the appropriate combination of patterns as the solution. Example: % Are there more than 25 values greater than 100? count = 0; %init counter found = 0; %init flag for n = 1:length(x) if x(n) > 100 count = count + 1; if count > 25 found = 1; break; end end end if found disp('Yes, more than 25 values were found.') end Note: Patterns used are Counter, Flag Variable , and Early Exit.

Page 7 of 7

- ANDRIODUploaded byAlapati Anudeep
- Seminar NetworkingUploaded byDeepak Rathore
- LoopingUploaded byKabka Futit
- Car Database SQLUploaded byRozzen Cadavero
- Seminar on NetworkingUploaded bySaurabh Vishnoi
- CAR RENTAL & HOTAL BOOKING APPLICATIONUploaded bySatyendraSingh
- common bus systemUploaded byPavan Kumar
- Java ProgramsUploaded byanil agarwal
- CryptographyUploaded byDeepak Rathore
- Car RentalUploaded bytony6111
- Advanced Networking and CommunicationUploaded bybirabrataganguly
- Basic Networking TopicsUploaded bySyed Imran Hashmi
- Command Line Utilities - DNS,DHCP,TCP/IPUploaded byMir Farhan Ali Abedi
- PLsql nested tablesUploaded byrajeshkalluri1
- Instruction CycleUploaded byRyan Paul Jaca
- Bus ReservationUploaded byPrashant Jachak
- TCP /IPUploaded bySandeep Singh
- Hackingzones.com-Extended LoopForeach Program in JavaUploaded bychiraghz
- Matter for Bus Reservation System Final reportUploaded byRahul Tonape
- SRS- Smart CityUploaded byAshish R Mallik
- ProgramsUploaded byChirag Dave
- Javascript IntroductionUploaded byRaghavendra Shivaram
- tcsUploaded byVineet Garg
- Unit 18 Software DesignUploaded byfranbimari
- 11266_PLSQL introduction final.pptUploaded byPraveen Verma
- PL-SQLUploaded byHardik Patel
- Conditionals, LoopsUploaded byFabio Martins
- Design PrincipleUploaded bydarkhole06
- LoopingUploaded byPrabir Padhy
- SQL ConnectionUploaded byXandra Lee

- Previous ECE Exam-1Uploaded bySecret Squirrel
- Reversible Phase Change CompositionUploaded bySecret Squirrel
- Descriptive WordsUploaded bySecret Squirrel
- Withdrawals and Medical School SuccessUploaded bySecret Squirrel
- Low Carb RecipesUploaded byJeff McMann
- Documenting DisabilityUploaded bySecret Squirrel
- Worst C project ever!Uploaded bySecret Squirrel
- Chemical KineticsUploaded bySecret Squirrel
- MRI Procedures and Medical DevicesUploaded bySecret Squirrel
- Chemical Kinetics Part 2Uploaded bySecret Squirrel
- ph131ln10Uploaded bySecret Squirrel
- ph131ln9part2Uploaded bySecret Squirrel
- Chapter 16 Notes: OscillationsUploaded bySecret Squirrel
- Best Crib Sheet for ECE/EE/Electrical EngineeringUploaded bySecret Squirrel
- ph131ln11Uploaded bySecret Squirrel
- Exam 6 VocabUploaded bySecret Squirrel
- Exam 6 VocabUploaded bySecret Squirrel
- Ph131 Ch4 SupplementUploaded bySecret Squirrel
- ph131ln7Uploaded bySecret Squirrel
- ph131ln9Uploaded bySecret Squirrel
- Physics_Mech-ch2suppf03Uploaded bySecret Squirrel
- ph131ln4Uploaded bySecret Squirrel
- ph131ln6Uploaded bySecret Squirrel
- ph131ln2Uploaded bySecret Squirrel
- PolymersUploaded bySecret Squirrel
- ph131ln8Uploaded bySecret Squirrel
- ph131ln3Uploaded bySecret Squirrel
- ph131ln5Uploaded bySecret Squirrel
- Transition Metal ComplexesUploaded bySecret Squirrel

- Tor List of WebsitesUploaded byGaben Newell
- Classic CryptographyUploaded byPranav Doshi
- SQL InjectionUploaded bytinku300g
- Anatomy of Business Logic VulnerabilitiesUploaded byEdDave
- FOR_NP_CAT_2013Uploaded byFrederick Gella
- VMware View AntiVirusDeployment WP EnUploaded bySanjeev Kumar
- SEBI Practice Questions of AptitudeUploaded byPrabakar Natraj
- LockedShields13 AARUploaded byjohn
- Sap Abap Alv TipsUploaded byvnod613
- Pioneer Pdp505hd Plasma [ET]Uploaded byGene Leal
- Mgt Responsibility Internal Information AssetsUploaded byadmac57
- contrafficUploaded bygershwinou
- EFF Amicus Brief in FBI Mass Hacking CaseUploaded byJoseph Cox
- Dawn Raid ProcedureUploaded byHiro Ng
- Question Analysis- Information Technology Application LevelUploaded byHossain Ashik
- Policy for the Use of Remote Access and VPN Services (2006)Uploaded byapi-3705994
- Financial Markets and InstitutionsUploaded byhakim_9569
- Hashing Concepts in DBMS.pdfUploaded bykaramthota bhaskar naik
- 019Uploaded byMohini Mohit Batra
- 10. Development of Fingerprint Biometric Attendance System for Non-Academic Staff in a Tertiary Institution.pdfUploaded byimdad ali
- How to Configure Site-To-Site IKEv2 IPSec VPN Using Pre-Shared Key AuthenticationUploaded byÑito Rodriguez
- Cryptome FilesUploaded byAl_Aurans
- st math canyon oaks elementary parent letterUploaded byapi-264259173
- Ebilling Project ReportUploaded bymeenalpandey
- SENATE HEARING, 108TH CONGRESS - REVIEW OF THE CAN-SPAM ACT AND NEW ANTI-SPAM INITIATIVESUploaded byScribd Government Docs
- 11g Installation on RedHatUploaded byVikas Sinha
- Educational Credential Assessment CDN Immigration Programs September 2015a (1)Uploaded byMaissa Hassan
- Edina Police Google Search WarrantUploaded bySNN.BZ
- Cyber SOSSamplePolicyUploaded bymuzaffar30
- Operations Security Module Test.txtUploaded byMike Lio