Home > Back-end >  GOTO command in C
GOTO command in C

Time:03-08

I've tried to sort 5 integers array using GOTO (using in for the first time) and if statement only, but I've debugged this code and I couldn't figure why that's not working, I don't fully understant how to use the GOTO and why I've got stuck inside the swapp loop, any help ?

int* sort5integersIFGOTO(int* arr)
{
    int i = 0, j = 1;
start:
    if (arr[i] > arr[j])
        goto swapp;

swapp:
    swap(&arr[i], &arr[j]);
    if (j < 4)
    {
        j  ;
        goto start;
    }
    if (i < 3)
    {
        i  ;
        j = i 1;
        goto start;
    }
    return arr;
}

CodePudding user response:

This piece of code:

    if (arr[i] > arr[j])
        goto swapp;
swapp:
    swap(&arr[i], &arr[j]);

alwayds does the same thing -- if the test is true, it jumps to the label (on the very next line) and if the test is false, it falls through to the label. So the result of the comparison is irrelevant, and it always swaps. You probably want

    if (arr[i] > arr[j])
        swap(&arr[i], &arr[j]);

with no label or goto here at all...

CodePudding user response:

For starters try always to write more general functions that for example can sort arrays of various sizes.

Also the return type int * of the function sort5integersIFGOTO does not make a great sense. Usually such functions have the return type void.

Your function is incorrect. For example if arr[i] is not greater than arr[j] nevertheless the control is passed to the statement under the label swapp that follows the if statement and in any case two elements of the array are swapped.

start:
    if (arr[i] > arr[j])
        goto swapp;

swapp:
    swap(&arr[i], &arr[j]);

Also within the function there are used unclear magic numbers as 3 and 4 though the passed array as it follows from the description has 5 elements.

The function can be defined the following way as it is shown in the demonstration program below.

#include <stdio.h>

void swap( int *a, int *b )
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void sort5integersIFGOTO( int *arr, size_t n )
{
    size_t i = 0;
    
    start:
    if ( i < n )
    {
        size_t j = i;
        
        next:
        if (   j < n )
        {
            if ( arr[j] < arr[i] )
            {
                swap( arr   i, arr   j );
            }
            goto next;
        }
        else
        {
              i;
            goto start;
        }
    }       
}

int main(void) 
{
    int arr[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    const size_t N = sizeof( arr ) / sizeof( *arr );
    
    for ( size_t i = 0; i < N; i   )
    {
        printf( "%d ", arr[i] );
    }
    putchar( '\n' ); 

    sort5integersIFGOTO( arr, N );
    
    for ( size_t i = 0; i < N; i   )
    {
        printf( "%d ", arr[i] );
    }
    putchar( '\n' ); 
}

The program output is

9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9

As you can see neither magic number is used within the function. The function can sort arrays of various sizes.

It is not difficult to rewrite the function using for loops instead of goto statements. For example

void sort5integersIFGOTO( int *arr, size_t n )
{
    for ( size_t i = 0; i < n; i   )
    {
        for ( size_t j = i;   j < n; )
        {
            if ( arr[j] < arr[i] )
            {
                swap( arr   i, arr   j );
            }
        }
    }
}
  • Related