0

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.

Ali Padida
  • 1,763
  • 1
  • 16
  • 34
  • why you are not using void SortArray(int *Arr) ? – Osmani Oct 15 '15 at 20:54
  • Another way to do *what*? What is the problem that you're trying to solve? It's not clear how an array of reference is related to the code that you show. – eerorika Oct 15 '15 at 20:57
  • 1
    `main` must return `int` in c++. – eerorika Oct 15 '15 at 20:59
  • Your question is clear actually, but title is misleading & most of code irrelevant. You probably should reduce your question to the 'Array of Reference' issue – Lol4t0 Oct 15 '15 at 21:02
  • Another way to create a bubble sort function, and I want to refer Arr to the Array being sorted. – Ali Padida Oct 15 '15 at 21:02
  • @AliPadida so, what you want is a reference to an array? That's allowed in c++ unlike array of reference which is not the same thing. In what way should the another way to create a bubble sort function differ from yours? – eerorika Oct 15 '15 at 21:07
  • I guess I should study pointers and references more. I'll check the answers and mark the solution. @user2079303 yes exactly, reference to an array which is going to be sorted. And I guess int main should return something not void. – Ali Padida Oct 15 '15 at 21:15
  • @lol4t0 Thanks, I will also change the title. – Ali Padida Oct 15 '15 at 21:15
  • Why not use actual C++ and go with `std::vector`? – user4520 Oct 15 '15 at 21:34
  • @szczurcio not familiar with that one, I'll google it. I got it working with template mentioned in other answers. – Ali Padida Oct 15 '15 at 21:52
  • 1
    You should read about it, and if your teacher is doing actual C++, they should've told you about it. It's basically a dynamic array, you can add elements to it, remove them etc. It can be passed as reference (since it's a regular C++ object) which is kind of a more convenient, hidden pointer. The C-way of doing it would be to pass a pointer to the first element and the total number of elements in the array. – user4520 Oct 16 '15 at 06:49

4 Answers4

2

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]) {
    ...
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
2

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)
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • So why not "using namespace std;"? – Ali Padida Oct 15 '15 at 21:43
  • 1
    @AliPadida I suppose your teacher told you to do this, and in class it's _probably_ fine. Basically this allows you to use all names within a given namespace without qualifying them, so `cout` instead of `std::cout`. It is, however, frowned upon in general due to possible name conflicts (imagine you have two namespaces that have the same name inside; if you use `using` on both of them, your code will not compile if it uses one of them). For more details see http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4520 Oct 16 '15 at 06:53
1

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.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • 1
    Do not overrate code bloat. It is usually lot smaller than people think :) – SergeyA Oct 15 '15 at 21:07
  • "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." Do you have any solution to this? I just want a clean, optimized code. This template works perfectly, but "Could result in massive bloat". – Ali Padida Oct 15 '15 at 21:50
  • 1
    @AliPadida - No. There's really no way around it short of using iterators or pointer/size parameters. Chances are that either would be as optimized as far as speed goes. – Edward Strange Oct 15 '15 at 22:37
0

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{}()});
}
tahsmith
  • 1,643
  • 1
  • 17
  • 23