-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
  • 31,209
  • 16
  • 94
  • 140
kAldown
  • 580
  • 1
  • 7
  • 26

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
  • 200,579
  • 13
  • 144
  • 260