Ali Padida
Ali Padida

Reputation: 1939

Bubble Sorting Function in C++

Array of Reference is not Allowed. I know in C++ it is illegal. But, is there another way to do this? I'm sure there is, but I cannot figure it out.

#include <iostream>
using namespace std;

#define UBound(n)   sizeof(n) / sizeof(n[0]);

void SortArray(int & Arr[]) {
    int ArrayLength;
    ArrayLength = UBound(Arr);
    int Min, Temp;
    for (int i = 0; i = ArrayLength; i++) {
        for (int j = 0; j = ArrayLength; j++) {
            if (Arr[i+1] < Arr[i]){
                Temp = Arr[i+1];
                Arr[i+1] = Arr[i];
                Arr[i] = Temp;
                }
        }
    }
} 
void main() {
    int numArray[9] = { 9, 7, 6, 8, 4, 5, 3, 2, 1 };
    SortArray(numArray);
} 

Final Function:

 template < size_t I >
    void SortArray(int(&Arr)[I]) {
        int Min, Temp;
         for (int i = 0; i < I - 1; i++) {
            for (int j = 0; j < I - 1; j++) {
                if (Arr[j+1] < Arr[j]){
                    Temp = Arr[j+1];
                    Arr[j+1] = Arr[j];
                    Arr[j] = Temp;
                }
            }
        } 
    } 

Thanks everyone for your answers.

Upvotes: 0

Views: 1122

Answers (4)

tahsmith
tahsmith

Reputation: 1723

You can use reference_wrapper to simulate references. This example, taken from http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper, show how to use it to access a container with multiple indices. I assume that is what you want your sort function to do: sort references to items in another container.

#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <numeric>
#include <random>
#include <functional>

int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);

    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
    // can't use shuffle on a list (requires random access), but can use it on a vector
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
}

Upvotes: 0

Edward Strange
Edward Strange

Reputation: 40859

There is, but it's not necessarily one you should use:

template < size_t I >
void SortArray(int (&Arr)[I]) {}

Now you'll be able to use I and sizeof Arr will report as if it was on the function's local variable stack.

Reason you probably don't want to do this is that every size array will create a new copy of the function. Could result in massive bloat.

But I used this technique to make a constexpr string type.

Upvotes: 1

SergeyA
SergeyA

Reputation: 62563

Your code has multiple issues. Let me list them

  • using namespace std; -- NEVER do this.
  • #define UBound -- first of all, you never need this macro. Second of all, this definition is buggy.
  • SortArray are trying to receive an array of references. It should be either template <size_t N> void SortArray(int (&Arr)[N]) -- receiving a reference to array; or void SortArray(int Arr[], size_t len)

Upvotes: 2

melpomene
melpomene

Reputation: 85767

You can do this with a function template (and a reference to an array, not an array of references (note the parens)):

template<size_t ArrayLength>
void SortArray(int (&Arr)[ArrayLength]) {
    ...
}

Upvotes: 2

Related Questions