Which isn’t very useful in calculating factorials. Now in Ada, integers generally are in the range: –(2^31) → (2^31)-1 But it overflowed in a nice way, and just exited the program, as opposed to crashing. The program overflowed because the number being calculate was too large to be stored as an integer. Raised CONSTRAINT_ERROR : factorial.adb:13 overflow check failed For example, if we compile and run this program, entering 20 for the number of factorials we wish to calculate, we get the following output: n! ? Now the interesting things about Ada is its ability to stop certain things happening, that languages would just basically ignore. This essentially stops loop indexes being used for something they should be used for. Warning: declaration hides "i" declared at line 5 Warning: for loop implicitly declares loop variable If you accidentally declare the loop index outside the loop (say on line 5 of the program), you will get a compiler warning of the following form: warning: variable "i" is never vad and never assigned Although in modern versions of C you can choose to declare the loop index within the loop, in Ada the loop itself takes care of that. The last point is particularly worth noting. The loop index is implicitly declared by the loop.The terminator “ end loop” is used to terminate the loop body.The keyword loop is used to start the actual loop body.There are three things you have to know when dealing with loops in Ada: To specify the values of the looping index, the loop uses the phrase “ i in 2.num“, which is different to the way languages like C achieve this. This is a for loop, there are others in Ada, we’ll loop at them briefly towards the end of this post. There is nothing much different with this program than any other language that uses a loop, except for the syntax of the loop. Put_line(integer'image(i) & "! = " & integer'image(fact)) The program looks like this: with ada.Text_IO use Ada.Text_IO Let’s now turn our attention to loops in Ada, with the help of our trusty factorial algorithm. By naming the loops, it is possible to use exit when to jump from the innermost loop to the outside of the nested structure (in this case the loop Outer), regardless of its depth. In most languages, it would not be possible to completely exit a nested loop structure from the innermost loop (without using a goto, or a boolean flag of some sort). Use of a normal exit would only exit the loop it resides in. With ada.Integer_Text_IO use Ada.Integer_Text_IO Let’s take the factorial program from the last post and modify it so that it uses a while loop. If you don’t need a bounded loop, then a while is the right choice. The most common loop next to the for loop, is of course the while loop. Int he last post we looked at the classic for loop. Here’s a Fortran program which reflects this: recursive function fact(n) result(r) The special case of negative numbers is handled by passing positive value for n.The base operation is fact := n * fact(n-1).The overall problem n! can be represented in terms of subproblems (n-1)!. Are there any special cases? handle special cases in addition to the terminal case.Does the recursion converge? Ensure that the algorithm moves closer to a terminal condition, and therefore a solution.What stops the recursion? This condition should be tested before a recursive call is made. How are the smaller, similar subproblems defined? If an iterative solution can be created that uses less resources, then it should be strongly considered over the recursion algorithm. By using the solutions of each subproblem, the overall problem is solved. Is recursion better than iteration? Recursion is appropriate if the problem being solved can be expressed as a series of smaller problems which are very similar.How does one develop a recursive algorithm? Here are some steps: Why not just use Python instead of Fortran?.A Q&A about learning in a university environment.Star Wars droids offer a good road forward for AI.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |