Log In Start studying!

Select your language

Suggested languages for you:
Answers without the blur. Sign up and see all textbooks for free! Illustration

Q14E

Expert-verified
Discrete Mathematics and its Applications
Found in: Page 370
Discrete Mathematics and its Applications

Discrete Mathematics and its Applications

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095

Answers without the blur.

Just sign up for free and you're in.

Illustration

Short Answer

Give a recursive algorithm for finding a mode of a list of integers. (A mode is an element in the list that occurs at least as often as every other element.)

The recursive algorithm is,

procedure moda1,a2,,an:integers with n1)L:=a1,a2,,anif n=1 thenreturn a1elseL:=Lank:=1 for i:=1 to n=1 if an=ai then L:=Laik:=k+1if k=n thenreturn anelsex:=mod(L)m:=0 for i:=1 to n1 if an=ai then m:=m+1 if km then return anelsereturn x

See the step by step solution

Step by Step Solution

Step 1: Describe the given information

The objective is to write the recursive algorithm for finding a mode of a list of integers.

Step 2: Give a recursive algorithm for computing finding a mode of a list of integers

Call the algorithm "mod" and the input is a list of integers.

procedure moda1,a2,,an : integers with n1

When the list contains only 1 integer, then the mode is the remaining integer.

L:=a1,a2,,anif n=1 thenreturn a1

If the list contains more than 1 value, then check, if the last value in the list is repeated somewhere in the list. Remove this value along with all other repetitions of the value form the list, while represents the number of values that were deleted. If , then all elements in the list were deleted and therefore, there should be return of an. Otherwise, apply the algorithm to determine the mode of the reduced list,

which occurs times. If k > m , then am is the mode. If k < m, then the mode is the mode of the reduced list.else

L:=Lank:=1 for i:=1 to n=1 if an=ai then L:=Laik:=k+1

And,

x:=mod(L)m:=0 for i:=1 to n1 if an=ai then m:=m+1 if km then return an else return x

By combining all the steps, the required algorithm will be as follows.

procedure moda1,a2,,an:integers with n1)L:=a1,a2,,anif n=1 thenreturn a1elseL:=Lank:=1 for i:=1 to n=1 if an=ai then L:=Laik:=k+1if k=n thenreturn anelsex:=mod(L)m:=0 for i:=1 to n1 if an=ai then m:=m+1 if km then return anelsereturn x

Therefore, the recursive algorithm is shown above.

Recommended explanations on Math Textbooks

94% of StudySmarter users get better grades.

Sign up for free
94% of StudySmarter users get better grades.