Professional Documents
Culture Documents
Applied in C#:
private int intFactorial (int aIntNumber) { if (aIntNumber == 0) return 1; else return aIntNumber * intFactorial (aIntNumber--); }
<SSSS><YY><CCCCC><UU>
Recursive Algorithms
Author: Ulysses R. Gotera
Where: SSSS YY CCCCC UU stands for a given system code, such as Business Object Reports System (BORS). the last two numbers in year (ie 2011, the last two number is 11) th th the CIRF count. A value of 10 means the 10 CIRF for the year, a value of 800 as the 800 CIRF for the year and so on. manually assigned by the user. A 00 means that the CIRF is for production while a manually assigned 01 up to 99 is the number of times an application system was submitted for UAT (User Acceptance Testing).
We would require that the YY and CCCCC are system generated. tblCounter is created in our database to store the current year and the last given value for CCCCC. In generating our reference number, we need to check for a duplicate in another table that holds CIRF data (tblCIRF) and regenerate a new one to ensure uniqueness. This is also to address split second data entry where two or more users can input new CIRF records. The pseudocode for this requirement is as follows:
Function GenerateRefNum is Input: SSSS and UU Output: <SSSS><YY><CCCCC><UU> 1. Get the year and count in tblCounter. 2. Get the machines (PC) year and compare it with tblCounters year. 3. If year is different then replace the value year with that of the machine and update the year and reset the counter to 1 of tblCounter, else proceed to 4. 4. Increment by 1 and update tblCounters counter field. 5. Concatenate <SSSS><YY><CCCCC><UU>. 6. Search in tblCIRF <SSSS><YY><CCCCC><UU> in its RefNum field. 7. If found then call GenerateRefNum (<SSSS>,<UU>) 8. Otherwise, return <SSSS><YY><CCCCC><UU>
I will shorten its implementation on C#, showing only numbers 7 and 8 of the pseudocode.
private string fnStrGenRefNum (string aStrSysCode, int aIntUsrCnt) { //Your data reader here reads tblCIRF with a Select //statement that looks for the existence of your newly created Reference Number. if (dtaReader.Read()) { strRefNum = fnStrGenRefNum (aStrSysCode, aIntUsrCnt); } else return strRefNum; }
The above algorithm expressed in C# shows how you can apply recursion on your program. Learning to apply recursive algorithm gives a developer a divide-and-conquer approach technique and creates a more elegant code that is easily digested by your CPU and can effortlessly understood by you and other programmers as well. Practice writing recursive algorithms today! Note: In real practice the code above was done in a stored procedure and a tblCounter was not present. The purpose of the sample above is to give you the idea in using a recursive function.
Page 2 of 2