2

What I am trying to do is to a variable in an array after sorting (ascending order) it through a function.

The function I have is:

void xpPoints_Sorting ()
{
  int xpPoints[] = { 620, 500, 250, 1000, 830, 100, 370 };
  int temp;

  for(int i=1;i< 7;++i)
    {
        for(int j=0;j<(7-i);++j)
            if(xpPoints[j] > xpPoints[j+1])
            {
                temp = xp[j];
                xpPoints[j] = xpPoints[j+1];
                xpPoints[j+1] = temp;
            }
    }
}

The problem is that when I type xpPoints[0], in int main(), the output is still "620" and not "100".

  int main()
  {
  xpPoints_Sorting ();
  int xpPoints[] = { 620, 500, 250, 1000, 830, 100, 370 };
  cout <<  xpPoints[0];
  }

However, when I take I don't put it in a function and put it in int main(), it works perfectly fine and outputs "100".

How can I get it to work while keeping the function for sorting the array?

2
  • xpPoints is local to main isnt't it? where you passed array to function? Commented May 12, 2017 at 4:46
  • 1
    What gave you the idea that it's the same array in main and xpPoints_Sorting? Commented May 12, 2017 at 4:53

2 Answers 2

2

You seem to be confused about how scope works in C++. You defined an array in main() called xpPoints, but it is local to that function. Simply calling a function which sorts another array has no effect on your local xpPoints array. Hence, when you print the first element nothing has changed.

To remedy this, you can refactor xpPoints_Sorting() such that it takes an array as input and then sorts it, e.g.:

void xpPoints_Sorting (int xpPoints[], int length)
{
    int temp;

    for (int i=1; i < length; ++i)
    {
        for (int j=0; j < (length-i); ++j)
            if (xpPoints[j] > xpPoints[j+1])
            {
                temp = xp[j];
                xpPoints[j] = xpPoints[j+1];
                xpPoints[j+1] = temp;
            }
        }
    }
}

And then in your main() function:

int main()
{
    int xpPoints[] = { 620, 500, 250, 1000, 830, 100, 370 };
    xpPoints_Sorting(xpPoints, 7);
    cout << xpPoints[0];
}
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried doing that but an error shows up and says "declaration of 'int xpPoints []' shadows a parameter"
@Confused.Student Remove the unnecessary declaration of the xpPoints array from your sorting function, q.v. my update code above.
Oh, I see. Thank you very much! I understand it now.
I think it's important to note, that it is (at least) useful to pass the length of the array to the function as well, instead of hardcoding it.
@slawekwin Good thinking.
1

In idomatic C++ what you're trying to do can be achieved like so using the std::sort algorthim:

  #include <algorithm>

  int main()
  {
     int xpPoints[] = { 620, 500, 250, 1000, 830, 100, 370 };
     std::sort(xpPoints,xpPoints + 7);
     cout << xpPoints[0];
  }

1 Comment

I am not sure if my teacher will allow me to use that but thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.