Question

Describe an algorithm that takes as input a list of n integers and finds the location...

  1. 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)

Homework Answers

Answer #1

Answer :

Algorithm to  finds the location of the first even integer

locationOfFirstEven(a1, a2, a3, ..., an : integers)
   FOR i, 0 to n
       IF a[i]%2==0
           RETURN i

   RETURN -1

Explanation:

  • Iterate i from 0 to n
  • check if a[i] is even. If it is, return the i
  • If there is no even number return -1

Hope you like it

Any Query? Comment Down!

I have written for you, Please upvote the answer as it encourage us to serve you Best !

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
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.
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.
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
. For any integer n ≥ 2, let A(n) denote the number of ways to fully...
. For any integer n ≥ 2, let A(n) denote the number of ways to fully parenthesize a sum of n terms such as a1 + · · · + an. Examples: • A(2) = 1, since the only way to fully parenthesize a1 + a2 is (a1 + a2). • A(3) = 2, since the only ways to fully parenthesize a1 + a2 + a3 are ((a1 + a2) + a3) and (a1 + (a2 + a3)). • A(4)...
Describe an algorithm that, given a set S of n integers and another integer x, determines...
Describe an algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. Your algorithm must be nlogn. Evaluate how long each step in the algorithm takes to demonstrate that it meets this requirement.
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.
USING PYTHON do all the he problems using while loop , continue and break 1-This problem...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the...
Recursively computing sums of cubes, cont. (a) Use induction to prove that your algorithm to compute...
Recursively computing sums of cubes, cont. (a) Use induction to prove that your algorithm to compute the sum of the cubes of the first n positive integers returns the correct value for every positive integer input.
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please