Search code examples
pythoncpseudocode

Rewrite pseudocode in C and Python


Rewrite the following pseudocode segment using a loop structure in the specified languages:

k = (j + 13) / 27
Loop:
If k > 10 then goto out
k = k + 1
i = 3 * k – 1
goto loop
out: ...

a. in C

b. in Python

I think in C is like:

for(k=(j+13)/27; k<=10; k++; i=3*k-1)

but i'm not sure. Can anyone help me with this?


Solution

  • For Python:

    k = (j + 13) / 27
    while k > 10:
        k += 1
        i = 3 * k - 1
    

    Though the assignment statement for i seems pointless each time, and could be placed outside of the loop, and work all the same.

    Your above C code will work, but it is a tad on the un-readable side. Cleaner it might look:

    k = (j + 13) / 27;
    while(k <= 10)
    {
        k++;
        i = 3 * k - 1;
    }
    

    If you use that C code is personal opinion, over what you already have made; and the Python code will work none-the-less.