0

I want to find the index of the last element containing an even number in an array, A, with 'length' number of elements.
For example, for A = {1, 2, 4, 5, 7} we would return 2 as the value 4 is the last even number and it is at index 2 in the array. I am required to use a for loop where the fewest number of elements need to be tested, where the first line follows the format: for ( i = ??; i > ??; i = ??)

I think there's something wrong with the first line of my code but I don't know how to fix it.

for ( i = length-1; i >= 0; i =  i-1) { 
if ( A[i] % 2 == 0) { // value mod 2 = 0 indicates even
    return i;
}
error: subscripted value is neither array nor pointer nor vector
     'if (A[i]% 2==0) { // value mod 2 = 0 indicates even'
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
user672518
  • 85
  • 2
  • 12
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] to show us, and also copy-paste the *full* and *complete* error output to show us. – Some programmer dude May 16 '19 at 07:29
  • 2
    *"subscripted value is neither array nor pointer nor vector"* Well, what is `A`, then? Please post your whole code, not just a snippet. – Blaze May 16 '19 at 07:30
  • The compiler tells you that your "array A" is not an array. – Gerhardh May 16 '19 at 07:36
  • Aside:: if your loop supposed to follow this format `for ( i = ??; i > ??; i = ??)` then `for ( i = length-1; i >= 0; i = i-1)` should be `for ( i = length-1; i > -1; i = i-1)` – kiran Biradar May 16 '19 at 07:40
  • That's a weird question: "I am required to use a for loop where the fewest number of elements need to be tested": either you use a for-loop and test all entries, or you use a while-loop and have the fewest number of elements tested. Or is it the intention to make you write a while-loop using a for-clause? – Dominique May 16 '19 at 12:45

1 Answers1

0

Here is how I would write it:

int num = -1;
for ( i = length-1; i > -1; i = i-1) {
  //Check if the array is even
  if( A[i] % 2 == 0 )

    // If it hasn't found an even number yet, automatically set num equal to the index
    // This avoids accessing the element in the "-1" position in the array which would
    //    give an error
    if( num == -1 )
      num = i;

    // If the element at the 'i' position is greater than the previous greatest element
    //   then set num equal to the index
    else if( A[i] > A[num] )
      num = i;
}

// It will return -1 if an even number isn't found
return num;

This will work if A is an array. It sounds like A currently isn't the array you're thinking it is; it may be an array of arrays or something similar. If you post the rest of your code it will help to resolve the error.

This answer may help you with your error