-4

I have the following C code :

void BubbleSort(int a[], int array_size)
{
    int i, j, temp;
    for (i = 0; i < (array_size - 1); ++i)
    {
        for (j = 0; j < array_size - 1 - i; ++j)
        {
            if (a[j] > a[j+1])
            {
                temp = a[j+1];
                a[j+1] = a[j];
                a[j] = temp;
            }
        }
    }
}

How do I rewrite this code in terms of goto only ?

Quentin
  • 62,093
  • 7
  • 131
  • 191
yaron0
  • 57
  • 1
  • 4
  • 1
    Wait..am I reading it correctly? You willingly want to add some `goto` statement? – Sourav Ghosh May 29 '15 at 11:02
  • `goto` is not a function, it's a statement to jump to some label. – Abhineet May 29 '15 at 11:04
  • 6
    We are going to replace you with a goto function. You're welcome. – Karoly Horvath May 29 '15 at 11:05
  • is it an academic interest? – Quicky May 29 '15 at 11:08
  • What else can you use in addition to `goto`? – Juraj Blaho May 29 '15 at 11:14
  • I don't see why a goto statement would be need for a simple bubble sort algorithm. You can maybe try rewriting your algorithm using a while loop and a flag to indicate if there are anymore needed permutations to sort your array... – Module May 29 '15 at 11:14
  • 1
    "How do I rewrite this code in terms of goto only" - remove the for loops and replace them with if-tests, gotos, target labels, and increments. yeah, its that simple. and you'll hopefully appreciate structure loops when you're done with it and gaze at both versions side by side. Maybe fix the code to include swap detection, that way it isn't lying with its claimed name. – WhozCraig May 29 '15 at 11:16
  • Breaking news! [Goto considered harmful!](http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html) Mankind discovered this before landing on the moon. – Lundin May 29 '15 at 11:22
  • goto some other site – Martin James May 29 '15 at 16:56

4 Answers4

7

Since semantics of while and for changed a little in C++ comparing to C, I think it's OK to refer C++ standard. It provides straight rules on how for and while are convertible to goto (they discussed in section 6.5):

while

while (T t = x) statement is equivalent to

label:
{
    T t = x;
    if (t) {
        statement
        goto label;
    }
}

for

for (for-init-statement; condition; expression) statement is equivalent to

{
    for-init-statement
    while ( condition ) {
        statement
        expression ;
    }
}

Thus it equivalent to:

{
    for-init-statement
    label:
    if ( condition ) {
        statement
        expression ;
        goto label;
    }
}
myaut
  • 11,174
  • 2
  • 30
  • 62
4
void BubbleSort(int a[], int array_size)
{
    int i, j, temp;

    // for (i = 0; i < (array_size - 1); ++i)
    i = 0;
BEGINFOR:
    if (i >= (array_size - 1)) goto ENDFOR;

    // for (j = 0; j < array_size - 1 - i; ++j)
    j = 0;
BEGININNERFOR:
    if (j >= array_size - 1 - i) goto ENDINNERFOR;

    if (a[j] > a[j+1])
    {
        temp = a[j+1];
        a[j+1] = a[j];
        a[j] = temp;
    }

    ++j;
    goto BEGININNERFOR;
ENDINNERFOR:

    ++i;
    goto BEGINFOR;
ENDFOR:
}
Quentin
  • 62,093
  • 7
  • 131
  • 191
pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    Thanks for the edit, @Quentin ... and yes! I am in the middle of changing batch files and didn't fully switch gears when writing my answer :) – pmg May 29 '15 at 11:23
2

To completely remove all blocks but the code looks ugly compared to the original one IMHO

void BubbleSort(int a[], int array_size)
{
    int i, j, temp;

    // for (i = 0; i < (array_size - 1); ++i)
    i = 0;
    BEGINFOR:
    if (i >= (array_size - 1)) goto ENDFOR;

    // for (j = 0; j < array_size - 1 - i; ++j)
    j = 0;
    BEGININNERFOR:
    if (j >= array_size - 1 - i) goto ENDINNERFOR;

    if (a[j] <= a[j+1]) goto ENDOFCOMPARISON
    temp = a[j+1];
    a[j+1] = a[j];
    a[j] = temp;
    EDNOFCOMPARISON:
    ++j;
    goto BEGININNERFOR;
   ENDINNERFOR:
   ++i;
   goto BEGINFOR;
   ENDFOR:
}
Quicky
  • 403
  • 1
  • 4
  • 10
0

Ehm.. You shouldn't really be using goto. Ever. At all. The sturcture is

i = 0;
label:
//loop action here
i++;
if (i < somevalue)
goto label;

Here's your code...

void BubbleSort(int a[], int array_size)
{
    int i, j, temp;
    i=0;
    first_loop:
        j = 0;
        second_loop:
            if (a[j] > a[j+1])
            {
                temp = a[j+1];
                a[j+1] = a[j];
                a[j] = temp;
            }
            j++;
            if (j < array_size - 1 - 1) {
                goto second_loop;
            }
         i++;
         if (i < array_size - 1) {
            goto first_loop;
        }
}
XapaJIaMnu
  • 1,408
  • 3
  • 12
  • 28
  • You code is not safe. In case array_size <= 1 you will perform bad memory accesses due to your tests at the end of the loop. You also forgot to remove the second for instruction – Quicky May 29 '15 at 11:25
  • Ye fixed, the extra for statement. Yeah, you shouldn't be invoking the function with array_size = 0, but I can't be bothered to fix that – XapaJIaMnu May 29 '15 at 13:43