42-School-Exam-Rank-02 icon indicating copy to clipboard operation
42-School-Exam-Rank-02 copied to clipboard

Patch max.c

Open yas0nia opened this issue 2 months ago • 0 comments

type of var result should match the output type of the function (int), otherwise it might fail some tests.

Example fail: max( {-42;0;-5;-64;-2;-68;-1} , 7) <- here, instead of returning 0, current version of a function returns random wrong value.

Why does it happen? unsigned int can only represent non-negative numbers. If you try to store a negative number in an unsigned int variable, it will actually store a large positive number due to underflow. In above mentioned case, when result was an unsigned int and we assigned it a negative value from the array (e.g., result = tab[i]; where tab[i] is -42), result would not actually hold -42. Instead, it would hold a large positive value due to underflow.

yas0nia avatar Apr 02 '24 20:04 yas0nia