design an algorithm in a c++ function that takes as input a list of n integers and find the location of the last even integer in the list or returns 0 if there are no even integers in the list
Thanks for the question.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer.
Thanks!
===========================================================================
#include<iostream>
using namespace std;
// function takes in an array of integers and the size of
the array
// function returns the lasst even number index
// if there are no even numbers in the array returns 0
int lastEvenIndex(int arr[], int size){
// iterate over the integers in the array
// from the last check if its an even then return the
index
for(int index = size-1; index>=0; index--){
// check if its even; return index
if true
if(arr[index]%2==0) return
index;
}
// if all the elements are odd then it will return
0
return 0;
}
int main(){
int arr[] = {1,2,3,4,5,6,7,8,9};
cout<<"Last Even Index = " <<
lastEvenIndex(arr,9) << endl;
int odds[] = {1,3,5,7,9,1,3,15,17,21};
cout<<"Last Even Index = " <<
lastEvenIndex(odds,10) << endl;
return 0;
}
================================================================
Get Answers For Free
Most questions answered within 1 hours.