Question

design an algorithm in a c++ function that takes as input a list of n integers...

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

Homework Answers

Answer #1

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;
}
================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Describe an algorithm that takes as input a list of n integers and finds the location...
Describe an algorithm that takes as input a list of n integers and finds the location of the first even integer or returns -1 if there is no even integer in the list. Here is the operation's header: procedure locationOfFirstEven(a1, a2, a3, ..., an : integers)
Pseudocode an algorithm that takes as input a list of n integers and finds the number...
Pseudocode an algorithm that takes as input a list of n integers and finds the number of even integers in the list.
1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average...
1). Describe an algorithm that takes a list of n integers a1,a2,...,an and find the average of the largest and smallest integers in the list.
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
Write an OCaml function named is_square that takes as input an integer, say n, (which may...
Write an OCaml function named is_square that takes as input an integer, say n, (which may be positive or negative) and returns true if that number is square. That is, it returns true if there exists an an integer, say m, if m * m = n. Next, write a function named count_squares that calls count from above to determine the number of squares in a list of integers. For example, count_squares [2;3;5] = 0 count_squares [1;2;3;4;5;6;7;8;9] = 3
Devise an algorithm that takes a list of n > 1 integers and finds the largest...
Devise an algorithm that takes a list of n > 1 integers and finds the largest and smallest values in the list.
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
In Python language: Create a function that takes as input two numbers, m and n, m<n,...
In Python language: Create a function that takes as input two numbers, m and n, m<n, and returns an m×n list-of-list-of-numbers. Each element of the outer list will be a list of consecutive integers, beginning with 1 and ending with n−1. If you're feeling bold, try to use list comprehension.
a) Give a recursive algorithm for finding the max of a finite set of integers, making...
a) Give a recursive algorithm for finding the max of a finite set of integers, making use of the fact that the max of n integers is the larger of the last integer in the list and the max of the first n-1 integers in the list. Procedure power(x,n): If (n=0): return 1 Else: return power(x,n-1) · x b) Use induction to prove your algorithm is correct
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT