converting some simple pseudocode to c++

Discussion in 'C++' started by Joah Senegal, Dec 17, 2007.

  1. Joah Senegal

    Joah Senegal Guest

    Hello all,

    I;m a beginner C++ and I;m trying to convert some pseudocode into C++. Its
    pseudo code of the peterson algorithm for N-processes.

    I almost converted the whole code. But the last lines are very hard.

    This is the pseudo code:

    for [ k=1 to n except k==i ]

    This is pretty simple code although I don;t know how to convert in into c++
    because of the except in the pseudocode. for [k=1 to n] to c++ is for
    (k=1;k<n;k++). i dont understand it because of the except.

    Is there anyone who can tell me what the C++ code is for the above pseudo
    code?

    I;ve another little bit more complex part of pseudocode as well

    wait until ((for all k<>i q[k]<j) or (turn[j]<>i)).

    Is there anyone who can give me the c-code for one of this pseudo codes ? I
    would be very gratefull if anyone can translate one of the pseudocodes. this
    one is the most important

    for [ k=1 to n except k==i ]

    many many thanks!!!!
     
    Joah Senegal, Dec 17, 2007
    #1
    1. Advertising

  2. Joah Senegal

    Lance Diduck Guest

    On Dec 17, 10:04 am, "Joah Senegal" <> wrote:
    > Hello all,
    >
    > I;m a beginner C++ and I;m trying to convert some pseudocode into C++. Its
    > pseudo code of the peterson algorithm for N-processes.
    >
    > I almost converted the whole code. But the last lines are very hard.
    >
    > This is the pseudo code:
    >
    > for [ k=1 to n except k==i ]
    >
    > This is pretty simple code although I don;t know how to convert in into c++
    > because of the except in the pseudocode. for [k=1 to n] to c++ is for
    > (k=1;k<n;k++). i dont understand it because of the except.
    >
    > Is there anyone who can tell me what the C++ code is for the above pseudo
    > code?
    >
    > I;ve another little bit more complex part of pseudocode as well
    >
    > wait until ((for all k<>i q[k]<j) or (turn[j]<>i)).
    >
    > Is there anyone who can give me the c-code for one of this pseudo codes ? I
    > would be very gratefull if anyone can translate one of the pseudocodes. this
    > one is the most important
    >
    > for [ k=1 to n except k==i ]
    >
    > many many thanks!!!!


    for(int k=1,k!=n;++k){
    if(k==i)continue;
    //do stuff
    }
     
    Lance Diduck, Dec 17, 2007
    #2
    1. Advertising

  3. Joah Senegal wrote:
    > I;m a beginner C++ and I;m trying to convert some pseudocode into
    > C++. Its pseudo code of the peterson algorithm for N-processes.
    >
    > I almost converted the whole code. But the last lines are very hard.
    >
    > This is the pseudo code:
    >
    > for [ k=1 to n except k==i ]
    >
    > This is pretty simple code although I don;t know how to convert in
    > into c++ because of the except in the pseudocode. for [k=1 to n] to
    > c++ is for (k=1;k<n;k++).


    Note that in Lance's answer the condition for the loop is k<=n, not
    k<n like you gave here.

    > i dont understand it because of the except.
    >
    > Is there anyone who can tell me what the C++ code is for the above
    > pseudo code?
    >
    > I;ve another little bit more complex part of pseudocode as well
    >
    > wait until ((for all k<>i q[k]<j) or (turn[j]<>i)).
    >


    Since C++ does not have "wait" equivalent, you might want to give
    more context to see if the translation is possible. Of course,
    most likely, due to 'for all' a single-line translation isn't what
    you'd naturally come up with. 'for all' needs a loop. Even if
    you manage to utilise standard function like 'for_each' or some
    such, you would still most likely need a functor, which will be
    written as a separate class. For now this is what it looks like
    to me:

    wait_here:
    // wait somehow

    bool all_q_are_less_than_j = true;
    for (int k = start_k; k <= end_k; ++k) { // start_k, end_k???
    if (k != i && !(q[k] < j)) {
    all_q_are_less_than_j = false;
    break;
    }
    }
    // here is your 'until'
    if (all_q_are_less_than_j || turn[j] != i)
    // do something, like move forward or whatever...
    else
    goto wait_here;

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 17, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan
    Replies:
    0
    Views:
    1,803
    Stefan
    Apr 15, 2004
  2. Pseudocode Help

    , Mar 12, 2005, in forum: C++
    Replies:
    3
    Views:
    564
    Victor Bazarov
    Mar 13, 2005
  3. Krisitne

    Pseudocode For Statement

    Krisitne, Sep 13, 2003, in forum: C Programming
    Replies:
    2
    Views:
    544
    Malcolm
    Sep 13, 2003
  4. Replies:
    8
    Views:
    2,504
    Sunnan
    Apr 2, 2005
  5. Ivan Van Laningham

    Re: Pseudocode in the wikipedia

    Ivan Van Laningham, Apr 1, 2005, in forum: Python
    Replies:
    2
    Views:
    348
    Ivan Van Laningham
    Apr 2, 2005
Loading...

Share This Page