-5

I want to make a sort statement by using a function. The problem is, I cannot return it back to the main() function. What's the problem? I also tried the void.

int sort_func(int sort[]) {
    int swap = 0;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 4; i++) {
            if (sort[j] > sort[j + 1]) {
                swap = sort[j];
                sort[j] = sort[j + 1];
                sort[j + 1] = swap;
            }
        }
    }
    return sort[5];
}

What is the problem in the code?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Please post your code as text and not as an image. – SimonC Dec 19 '22 at 16:59
  • 2
    [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/) – Remy Lebeau Dec 19 '22 at 17:00
  • 3
    That's not how you do it in C++, you are still using "C" style arrays. Use std::vector or std::array and you can use [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort) – Pepijn Kramer Dec 19 '22 at 17:00
  • why do you want to return an `int` from the function? – 463035818_is_not_an_ai Dec 19 '22 at 17:00
  • 1
    [Arrays decay to pointers](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay/) and are always passed by reference. – user4581301 Dec 19 '22 at 17:01
  • You do not have return anything. You passed array by reference and argument was changed. – Marek R Dec 19 '22 at 17:02
  • Is this correct way to output the array from function? sort_func(grades); for (int i = 0; i <= 4; i++) { cout << grades[i]; } – Vladimiros Orfanov Dec 19 '22 at 17:03
  • What if you wanted to sort an array that didn't have exactly 5 elements? – Nathan Pierson Dec 19 '22 at 17:11
  • @PepijnKramer you can use `std::sort()` even with C-style arrays. – Remy Lebeau Dec 19 '22 at 17:23
  • `return sort[5];` This is an example of the belief that if you put the size of an array within [] then it means the whole array. This is a weird belief and completely wrong. `sort[5]` for an array of size 5 is just an error. – john Dec 19 '22 at 17:24
  • As a side comment, if sort[] has 5 elements, then those are numbered 0 through 4, and you should return sort[4]. I agree this function is pure C, with C++ you should use std::array or std::vector. If all you want is the 5th element, you don't need to sort the array, you can call std::nth_element. If you need the maximum (5th of five), you can find it with a single loop over the elements. – Uri Raz Dec 19 '22 at 19:27

1 Answers1

1

You don't need to return anything. The array is being passed into the function via an int* pointer, so the code is directly manipulating the elements of the caller's array.

Also, your 2nd for loop is incrementing i when it should be incrementing j instead.

How is the caller's array declared, exactly? The loops inside your function require the sort parameter to point at an int[] array with at least 5 elements. However, the function's declaration does not convey or enforce that requirement, so the caller is free to pass in however many elements it wants, and if that size is fewer than 5 then your code will have undefined behavior. You should at least have the caller pass in the actual number of elements as another parameter, eg:

void sort_func(int sort[], int size) {
    int swap = 0;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < (size-1); j++) {
            if (sort[j] > sort[j + 1]) {
                swap = sort[j];
                sort[j] = sort[j + 1];
                sort[j + 1] = swap;
            }
        }
    }
}
int arr[5];
...
sort_func(arr, 5);

Otherwise, if you strictly require 5 elements, then enforce that by taking a pointer to an array with exactly 5 elements, eg:

void sort_func(int (*sort)[5]) {
    int swap = 0;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 4; j++) {
            if ((*sort)[j] > (*sort)[j + 1]) {
                swap = (*sort)[j];
                (*sort)[j] = (*sort)[j + 1];
                (*sort)[j + 1] = swap;
            }
        }
    }
}
int arr[5];
...
sort_func(&arr);

Or, use a reference instead of a pointer, eg:

void sort_func(int (&sort)[5]) {
    int swap = 0;
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 4; j++) {
            if (sort[j] > sort[j + 1]) {
                swap = sort[j];
                sort[j] = sort[j + 1];
                sort[j + 1] = swap;
            }
        }
    }
}
int arr[5];
...
sort_func(arr);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I made it void, now how to cout the array into the console? sort_func(grades); for (int i = 0; i <= 4; i++) { cout << grades[i]; } – Vladimiros Orfanov Dec 19 '22 at 17:18
  • You already know the answer to that. And in the 20 minutes it took you to ask that (twice), you could have simply tested it yourself. I will say, however, that your output loop is missing one thing, though - it is not outputting any whitespace/punctuation in between each grade value to separate them, so you are going to end up with 1 long number in your console display. – Remy Lebeau Dec 19 '22 at 17:23