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
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
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.
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 algorithm (in pseudocode, with proper indentation for blocks of code in loops or conditional...
Write an algorithm (in pseudocode, with proper indentation for blocks of code in loops or conditional statements) that finds and returns the location of the first negative integer in a sequence of integers.
Algorithm problem 2 [2.3-7] Describe a Θ(nlgn) algorithm that, given a set S of n integers...
Algorithm problem 2 [2.3-7] Describe a Θ(nlgn) algorithm that, given a set S of n integers and another integer ,determines whether or not there exist two elements in S whose sum is exactly x.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT