Ad
  • Default User Avatar

    Dont forget, this is Vector (not array), and it can contain negative numbers

  • Default User Avatar

    Thank you very much for timely help! I'm just learning and don't know how significant are the differences between array and vector. Kata is good for practice.

  • Custom User Avatar

    The code you showed above is invalid, it introduces UB due to use of an uninitialized variable (result is uninitialized and not written to, if the arr is empty). The passing tests are a result of handling the UB by compiler. It is difficult to write tests in a way to protect against such problems (it's UB after all), and I am not sure if this issue can be universally fixed.

  • Custom User Avatar

    Your solution has more than one problem:

    • The line c = sizeof arr / sizeof arr[0]; is wrong. It;s not how you get a size of a vector, and it returns wrong results.
    • Your solution works incorrectly if the input contains zeors. What does it return for following input: {1, 3, 0} ?
    • Your solution compiles with a warning, which points to an actual problem and even tells you why it crashed:
    In file included from main.cpp:6:
    ./solution.cpp:63:1: warning: control may reach end of non-void function [-Wreturn-type]
    }
    ^
    1 warning generated.
    solution.cpp:7:5: runtime error: execution reached the end of a value-returning function without returning a value
    

    You need to fix this bug. It's not a kata issue.

  • Custom User Avatar

    Please use proper code formatting when posting blocks of code.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution