2

I am trying to do a selection sort using function called min().

This is my code:

#include <stdio.h>
#include <conio.h>

void main() {
    int i, temp, arr[20], n, loc;
    int min(int [], int, int);
    printf("Enter a range of the array");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Enter elements");
        scanf("%d", arr[i]);
    }
    for (i = 0; i < n; i++) {
        loc = min(arr, i, n);
        temp = arr[i];
        arr[i] = arr[loc];
        arr[loc] = temp;
    }
    min(int arr[], int i, int n) {
        int j, loc, temp;
        for (j = i + 1; j < n; j++) {
            if (arr[i] > arr[j]) {
                temp = j;
            }
        }
        return (temp);
     }
     getch();
}

the compiler is giving one error when compiling. it saying:

Error SELECTIONSORT.C 22: Expression Syntax.

my line number 22 is min(int arr[],int i, int n) according to my compiler Turbo C++.

Please guide me where I am going wrong. Thanks for any help.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Atanu Mondal
  • 61
  • 1
  • 12

1 Answers1

3

There are multiple problems in your code:

  • The function min must be defined outside the body of the main() function.

  • Note that it is considered bad style to declare function prototypes in a local scope. Either define the function before the main() function or put the prototype before the main() function.

  • Also the prototype for main() without arguments should be int main(void).

  • In function min, you must initialize temp to i, or use i directly.

  • You should print the array contents after the sort, otherwise the program has no effect.

Here is a corrected version:

#include <stdio.h>
#include <conio.h>

int min(int [], int, int);

int main(void) {
    int i, temp, arr[20], n, loc;
    printf("Enter a range of the array: ");
    if (scanf("%d", &n) == 1) {
        for (i = 0; i < n && i < 20; i++) {
            printf("Enter element %d: ", i);
            if (scanf("%d", &arr[i]) != 1)
                break;
        }
        n = i; // n is the actual number of inputs
        for (i = 0; i < n; i++) {
            loc = min(arr, i, n);
            temp = arr[i];
            arr[i] = arr[loc];
            arr[loc] = temp;
        }
        for (i = 0; i < n; i++) {
            printf("%d\n" array[i]);
        }
    }
    getch();
    return 0;
}

int min(int arr[], int i, int n) {
    int j;
    for (j = i + 1; j < n; j++) {
        if (arr[i] > arr[j]) {
            i = j;
        }
    }
    return i;
 }
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • @chqrile Thank You very much, sir, For the elaborated explanation and the source code. – Atanu Mondal Jul 16 '17 at 15:40
  • Make the `min` function `static inline` and the compiler will probably use it — but the definition should probably come before it is used. – Jonathan Leffler Jul 16 '17 at 15:49
  • 2
    @JonathanLeffler: I would do that, but it is an advanced concept beyond the frame of the question. Compilers with a global optimizer will inline this function even is defined after `main()`, or at least they should. – chqrlie Jul 16 '17 at 15:51
  • 1
    Fair enough; one reason (of many) it is a comment rather than an edit. If you make the function `static` (without the `inline`), that may also manage to remove the separate function from the code. As it stands, the compilation to object file has to generate the function because it might be called from outside the file. IMO, there's a lot of code that does not use the `static` storage class specifier enough. However, that's a bit beyond people having problems with the mechanics of writing code, though not very much beyond. – Jonathan Leffler Jul 16 '17 at 15:57
  • @JonathanLeffler: You are correct, but modern global optimizing compiler/linker combinations should be able to remove such dead code from the executable. The world does not seem to care about code bloat, which is a shame. Elegance is so rare nowadays. – chqrlie Jul 16 '17 at 16:00