Home > Net >  Function returns incorrect value inside for loop (but correct value outside)
Function returns incorrect value inside for loop (but correct value outside)

Time:06-04

I'm trying to write a basic selection-sort program in C. From debugging, I can see that under the first for loop (// Swap the values), selection(nums, size, i).index returns the same value as i. However, if I call the selection function with the exact same code outside of the for loop, it will correctly return the index (of the number in the array with the smallest value and to the right of the given index).

How can I make selection return the correct value inside the for loop?

#include <stdio.h>

typedef struct {

    int value;
    int index;

} sorted;

sorted selection(int integers[], int size, int idx);

int main(void) {

    int nums[] = {7, 2, 3, 0, 1, 4, 6, 5};
    int size = sizeof(nums)/sizeof(int);
    int temp;

    // Swap the values
    for (int i = 0; i < size; i  ) {
        temp = nums[i];
        nums[i] = selection(nums, size, i).value;
        nums[selection(nums, size, i).index] = temp;
    }

    // Print the array
    printf("[");
    for (int j = 0; j < size; j  ) {
        if (!(j == size - 1)) {
            printf("%i ", nums[j]);
        }
        else {
            printf("%i", nums[j]);
        }
    }
    printf("]\n");
}

sorted selection(int arr[], int size, int start) {

    sorted smallest;

    for (int i = start; i < size; i  ) {
        // If first element
        if (i == start) {
            smallest.value = arr[i];
            smallest.index = i;
        }

        // If smaller
        else if (arr[i] < smallest.value) {
            smallest.value = arr[i];
            smallest.index = i;
        }
    }

    return smallest;
}

CodePudding user response:

You need change loop "// Swap the values"

// Swap the values
for (int i = 0; i < size; i  )
{
    sorted select = selection(nums, size, i);
    temp = nums[i];
    nums[i] = select.value;
    nums[select.index] = temp;
}

CodePudding user response:

The reason why your version is failing is that you are calling selection a second time after starting to modify the array, when the index just needed to be retained after the first call.

The use of sorted and selection are, as @WhozCraig pointed out, completely unnecessary. You are also passing sorted by value which is not the convention in C.

I've renamed the function to indicate its purpose. This could be simplified to take only the elements right of the current index, but that would require some additional adjustments when handling the return value, so its a trade off.

#include <stdio.h>

int indexOfSmallest(int integers[], int size, int idx);

int main(void) {

    int nums[] = {7, 2, 3, 0, 1, 4, 6, 5};
    int size = sizeof(nums)/sizeof(int);
    int temp;

    // Swap the values
    for (int i = 0; i < size; i  ) {
        temp = nums[i];
        int smallest = indexOfSmallest(nums, size, i);
        nums[i] = nums[smallest];
        nums[smallest] = temp;
    }

    // Print the array
    printf("[");
    for (int j = 0; j < size; j  ) {
        if (!(j == size - 1)) {
            printf("%i ", nums[j]);
        }
        else {
            printf("%i", nums[j]);
        }
    }
    printf("]\n");
}

int indexOfSmallest(int arr[], int size, int start) {

    int smallest = start;

    for (int i = start; i < size; i  ) {
        if (arr[i] < arr[smallest]) {
            smallest = i;
        }
    }

    return smallest;
}
  • Related