-3

I need to calculate array sum, but attribute must be ONLY this particular array.

bool solution(int arr[]) {
    int counter = 0;
    int len = sizeof(arr) / sizeof(arr[0]);
    std::cout << len << std::endl;
    for (int i=0; i < len; i++){
        counter += arr[i];
    }
    if (counter == 21)
        return true;

    return false;
}

It won't work, I need to pass an array length from outside. how to reach this without passing array length as an attribute?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
kAldown
  • 610
  • 2
  • 8
  • 27

1 Answers1

3

how to reach this without passing array length as an attribute?

You cannot unless your array holds a sentinel value that marks the end of valid numbers.

Use std::vector if you have the option to. Then, the size information comes along for the ride.

R Sahu
  • 204,454
  • 14
  • 159
  • 270