From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: algorithm, two implementations that act the same, same type etc Date: Mon, 15 Feb 2021 15:02:53 +0200 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net GZmxKfuVqwnb7av11zJV+ADegHXzk1j5IIjrvH0/JTPUpKLua6 Cancel-Lock: sha1:cm07j2s87jwdYRr35195T0P6hWQ= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61353 List-Id: On 2021-02-14 21:35, Mehdi Saada wrote: > Hi, > I have an issue with an algorithm - self-imposed "homework" I guess, > the first one is mine, the wrong, the second is the "right" one, that > works. > > I can't figure out why they would logically be any different. > > basically instead of using a stack on access values, I stack the > values themselves. the rest is the same. > > except this function ALL the rest of the project is the same. they > are in related package so from the first I call the second to ensure > the result is right, and it shows its not. > > could someone throw an eye and tell if it seems identical or that > something gross escaped my sight ? First you should have told us what the function is supposed to do. I would also advise you to write that descriptions as comments in the code, BEFORE you start coding, so that (a) you know, while you are writing the code, what the code is meant to do; and (b) someone reading the code later has a chance of understanding the intent. Looking first at what you call the "good" code, it seems to be given a reverse-Polish (post-fix) integer expression, as a vector of objects, each of which can be either an operand (an integer) or an operator (+, -, *, /), and the purpose of the function is to evaluate the expression and return the resulting integer value. The evaluation function uses a stack of operands and results, in the normal way: traverse the post-fix expression items from first to last if the item is an operand, push its value if the item is an operation, pop the top two stack elements, apply the operation, and push the result. pop and return the (only) stack element. > my function: ... > Depiler(Une_Pile => Pile_operandes, > Elem => op2_calcul); ...> return op1_calcul; Have a good look at those two statements. You will see a discrepancy.