Question :
Write a function named lastEven that returns the index of the last
even value in its array argument. For example, lastEven will return 3 if the array is {3, 2, 5, 6, 7}, because that is the index of 6 which
is the last even value in the array.
If the array has no even numbers, the function should return -1.
If you are programming in Java or C#, the function signature is
   int lastEven
(int[ ] a)
If you are programming in C or C++, the function signature is
   int lastEven (int a[ ], int len) where len is the
number of elements in a.
Solution :
  public static int
lastEven(int [] a)
        {
            int
lastIndex = 0;
            for (int i = a.Length - 1; i > 0;i-- )
            {
                if (a[i] %
2 == 0)
                {
                    lastIndex = i;
                    break;
   
            }
            }
            return lastIndex;
        }



 04:00
04:00


 

It doesn't return -1
ReplyDeletemake it the last index -1
ReplyDelete