0

Solution is probably pretty easy but I can't figure it out. How do I make a permanent change to the vector and not only a change that takes effect inside the function? I debugged it and I can see that "numbers" is updated with the value I want it to have but it disappears as soon as it is executed.

using namespace std;
bool checkVal();
vector<int> getNumbers();
void setPowerball(vector<int> numbers);

int main()
{
    srand(time(0));
    vector<int> numbers = getNumbers();
    setPowerball(numbers);

    for (int i = 0; i < 5; i++)
    {
        cout << numbers[i];
        cout << " ";
    }
}

bool checkVal(vector<int> numbers, int size, int value)
{
    for (int i = 0; i < size; ++i)
    {
        if (numbers[i] == value)
        {
            return true;
        }
    }
    return false;
}

void setPowerball(vector<int> numbers)
{
    for (int i = 4; i < 5; i++)
    {
        int last = rand() % 26 + 1;
        if (checkVal(numbers, i, last))
        {
            i--;
        }
        else
        {
            numbers.push_back(last);
        }
    }
}

vector<int> getNumbers()
{
    vector<int> numbers;
    for (int i = 0; i < 4; i++)
    {
        int num = rand() % 69 + 1;
        if (checkVal(numbers, i, num))
        {
            i--;
        }
        else
        {
            numbers.push_back(num);
        }
    }

    sort(numbers.begin(), numbers.end());
    return numbers;
}
Gabriel
  • 11
  • 1
  • 1
    Because you're passing a _copy_ of the `numbers` vector, so the changes are made only to that copy. Make the functions take a reference instead. – jkb Jan 14 '22 at 03:13
  • Contrary to your subject line, there is no function named `powerNumber()` in your code. In any event, if a function modifies an argument that has been passed to it by value, the caller has no visibility of the changes. Passing by value means that a *copy* of the object supplied by the caller is passed, so the function modifies the copy without touching the original, and that copy ceases to exist when the function returns. All of your functions have their argument passed by value. – Peter Jan 14 '22 at 03:32
  • 1
    @Tanishq-Banyal Point is that, if the OP can't get the specifics of their question, they can't expect a good answer. In software (development, code, etc) the cause of most problems is in small details, and getting small details wrong in a question substantially increases chance that the question misleads people who try to help and reduces chances of getting useful/relevant answers. – Peter Jan 14 '22 at 03:49

1 Answers1

2

You are passing arguments to setPowerBall function by value. So whenever it's called, it gets its private copy of the vector, which gets destructed at the end of function scope.

Instead you should be passing the arguments by reference in this case.

void setPowerball(vector<int>& numbers)
{
    // do stuff
}

MAIN POINTS :-

  1. Notice the & in function declaration. It implies taking a lvalue reference.
  2. References allow you to modify the original variable without making a copy.
  3. References are also cheaper to pass in case of objects like std::string or std::vector. But for primitive types like int or float, pass by value is faster.

Note : You could also have used pointers to do the same but using References is recommended and safer way. Rule of the thumb is Use references when you can, and pointers when you must

0xB00B
  • 1,598
  • 8
  • 26