erykkk
erykkk

Reputation: 119

C - using to pointer to function yield no result

I am creating a program which uses a pointer to function to allow the user to select from 2 options to perform on an array of integers: reverse or randomise. However, the program yield no output when executed. Where did I go wrong?

void reverseArray(int arraySize, int a[]);
void randomiseArray(int arraySize, int a[]);
void printArray(int arraySize, int a[], void (*arrayFunction)(int arraySize, int a[]));


int main ()
{

    srand(time(NULL));

    int selection;
    int myArray[] = {2,4,6,8,10,12,14,16,18,20};

    printf("Enter 0 for reverse, 1 for randomise.\n");
    scanf("%d",&selection);

    if (selection == 0)
        printArray(sizeof(myArray)/sizeof(myArray[0]),myArray,reverseArray);
    else if (selection == 1)
        printArray(sizeof(myArray)/sizeof(myArray[0]),myArray,randomiseArray);
    else
        printf("Please make a valid selection\n");

    return 0;

}

void reverseArray(int arraySize, int a[])
{
    int i,j=arraySize,swap=0;

    for (i=0;i<arraySize/2;i++)
    {
        swap = a[i];
        a[i] = a[j];
        a[j] = swap;
        --j;
    }

}

void randomiseArray(int arraySize, int a[])
{
    int i,j,swap;

    for (i=0;i<arraySize;i++)
    {
        j = rand()%(i+1);

        if (j!=i)
        {
            swap = a[i];
            a[i] = a[j];
            a[i] = swap;
        }
    }
}

void printArray(int arraySize, int a[], void (*arrayFunction)(int arraySize, int a[]))
{

    arrayFunction(arraySize, a);

    for (int i = 0; i < arraySize; i++) 
    {
        printf("%d ", a[i]);
    }
    printf("\n");

}

The user selects when function they want to perform by typing either 0 for reverse or 1 for randomise. Based on the decision, a printArray function is called with the respective choice.

reverseArray is just a generic swap function, and randomiseArray uses a Fisher-Yates shuffling algorithm.

I am assuming that the source of my error lies in the printArray fucntion, yet I cannot correct it.

Upvotes: 0

Views: 63

Answers (3)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You are accessing outside the bounds of your array, causing UB and in this case "no output":

In reverseArray, you must initialize j=arraySize-1. Without the -1 you go out of bounds.

Upvotes: 0

Jim Lahey
Jim Lahey

Reputation: 84

In reverseArray() your index j is out of range, it should he j=arraySize-1

Other than that it works for me.

Upvotes: 0

Stephen Docy
Stephen Docy

Reputation: 4788

Two errors

in reverseArray, j should start at arraysize - 1 or you will be writing one element past the end of the array

void reverseArray(int arraySize, int a[])
{
    int i, j = arraySize - 1, swap = 0;

    for (i = 0; i<arraySize / 2; i++)
    {
        swap = a[i];
        a[i] = a[j];
        a[j] = swap;
        --j;
    }

}

And in randomizearray(), you assign to a[i] twice, instead of using a[j] for the final assignment.

void randomiseArray(int arraySize, int a[])
{
    int i, j, swap;

    for (i = 0; i<arraySize; i++)
    {

        j = rand() % (i + 1);
        printf("%d %d\n", i, j);
        if (j != i)
        {
            swap = a[i];
            a[i] = a[j];
            a[j] = swap;
        }
    }
}

The first error causes undefined behavior when reversing, the second causes the array to stay the same. Other than that, the function pointers are setup fine and work correctly.

Upvotes: 3

Related Questions