comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: algorithm, two implementations that act the same, same type etc
Date: Mon, 15 Feb 2021 15:02:53 +0200	[thread overview]
Message-ID: <i8v9ntFqpitU1@mid.individual.net> (raw)
In-Reply-To: <b77499f6-18e9-4a53-b115-4b6f772ce5d1n@googlegroups.com>

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.

  parent reply	other threads:[~2021-02-15 13:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-14 19:35 algorithm, two implementations that act the same, same type etc Mehdi Saada
2021-02-15 11:07 ` AdaMagica
2021-02-15 13:02 ` Niklas Holsti [this message]
2021-02-15 16:34   ` Mehdi Saada
2021-02-15 19:15     ` Niklas Holsti
2021-02-17 10:50       ` Mehdi Saada
2021-02-17 12:14         ` Mehdi Saada
2021-02-17 17:10           ` Niklas Holsti
2021-02-17 21:13             ` Mehdi Saada
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox