-2
void sort(int* A,int l)
{
    int j;
    int B[l];
    for(int i=0;i<l;i++)
    {
        j = largest(A,l);
        B[l-i-1] = A[j];
        A[j] = -1;
    }
    A = B;
 }
  int main()
  {
   .
   int C[3] = {x,y,z};
   ...
    sort(C,3);
     cout<<C[0]<<C[1];
    } 

output is coming to be -1-1 But if we assign A[0] = B[0] and so on, then we are getting the right answer. PS: I've tried using *A = *B, which is only giving the first element to be correct.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Glaedr
  • 11
  • 2
  • You need to initialize array `int B[l];`. Try `int B[l] ={};` – Himanshu Dec 02 '14 at 06:34
  • @Himanshu is it really? – Sourav Ghosh Dec 02 '14 at 06:36
  • 2
    `A = B` doesn't do anything sensible here. Both variables go out of scope after you return from the function. You should copy the contents of `B` into `A` in a loop or with `std::copy`. – M Oehm Dec 02 '14 at 06:37
  • do not try to use a function local variable outside function scope. – Sourav Ghosh Dec 02 '14 at 06:37
  • why are you taking largest element from 0 onword's each time j = largest(A,l);, rather you should use like this. j = largest(A[i],l); – Amol Bavannavar Dec 02 '14 at 06:41
  • @AmolBavannavar: `A = &B`, really? Also, your suggested call should be `largest(&A[i], l - i)`. Please be more careful with your suggestions. Wrong suggestions are not helpful. – M Oehm Dec 02 '14 at 06:49
  • It depends on largest() function @M Oehm – Amol Bavannavar Dec 02 '14 at 06:56
  • @AmolBavannavar: No, it does not. The signature of the largest function is clear from how it is called in the example code. It is `largest(const int *A, int l)`. Your call does not match this signature. If you are in doubt, write a little example program. – M Oehm Dec 02 '14 at 06:58
  • Everyone is missing the point here, i don't bother about the sorting and anything else, my only point is how to assign A = B since A has been passed by reference i want to use the pointer, that's all. And i've also tried passing B by reference. It still didn't work – Glaedr Dec 02 '14 at 16:19
  • _Everyone is missing the point here._ We can't read your mind. If the sorting is not important, reduce your example. Sorting aside, it has been pointed out that you must copy the contents from the local array `B` to the array `A` that is passed to your function by reference. You seem to have gotten to that conclusion yourself. That's the solution to your problem, like it or not. – M Oehm Dec 02 '14 at 16:57

2 Answers2

1

When you assign A = B, you re-assign a local variable that holds a pointer to the first element of your array. This assignment will not change anything in main. In particular, the contents of A will not be affected.

You must copy all the elements from B to A after you have finished your sorting:

void sort(int *A, int l)
{
    int j;
    int B[l];

    // sort into temporary array B    
    for (int i = 0; i < l; i++) {
        j = largest(A, l);
        B[l - i - 1] = A[j];
        A[j] = -1;
    }

    // copy temporary array B to result array A
    for (int i = 0; i < l; i++) A[i] = B[i];
}

But if you look at it, Amol Bavannavar was basically right: You don't have to check the whole array for the largest element each time. It is enough to check the remaining elements. So instead of assigning a low value to "used" elements, you could swap the largest elements to the end. When you do that, you'll see that the processed elements are at the end, the unprocessed elements are at the beginning. Then you can do your sorting in place without the need of a temporary array:

void sort2(int *A, int l)
{
    while (l) {
        int j = largest(A, l--);
        int swap = A[j]; A[j] = A[l]; A[l] = swap;
    }
}
M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • I wasn't bothered about the sort nor the code, i just wanted to know about the assigning A = B, well i got it thanks – Glaedr Dec 03 '14 at 05:53
0

There are many wrong uses of code in your example, for instance:

int B[l];

cannot be done, if you do it like this l must have a constant value.

A = B;

will perform a shallow copy instead of a deep copy. You can see the diffrence here: What is the difference between a deep copy and a shallow copy?

cout<<C[0]<<C[1];

will print the numbers joined together without parsing.

As to how to fix this code one implementation you might be aiming towards can be:

#include <iostream>

using namespace std;

int largest(int* A, int l)
{
int big=-1;
int i;
int index=0;
for(i=0;i<l;i++)
{
    if(A[i]>big)
    {
        big=A[i];
        index=i;
    }
}
return index;
}

void sort(int* A,int l)
{
int j;
int *B=new int[l];
for(int i=0;i<l;i++)
{
    j = largest(A,l);
    B[l-i-1] = A[j];
    A[j] = -1;
}
for(int i=0;i<l;i++)
{
    A[i]=B[i];
}
}
int main()
{
 int C[3] = {2,5,1};
 sort(C,3);
 cout<<C[0]<<" "<<C[1];
 return 1;
} 
Community
  • 1
  • 1
Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59