LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...

70.2K

Verified Solution

Question

Programming

LargestTwo problem: Based upon the pseudocode code below,implement in C++ a divide-and-conquer algorithm that finds thelargest and second largest value from a vector of ints.

void LargestTwo (vector l, int left, int right, int &largest, int & secondLargest)

• Please write comment for your functions, similar to the one inthe pseudocode, to include both pre- and post-conditions.

• Comment on the logic of your code, e.g., what is true afterthe two recursive calls?

• Answer the three questions in the comment of your code. •

Test the function in main(), by calling the function to find outlargest and second largest values in a vector of length 2, 3 and 4,and make sure you include cases where the largest and secondlargest values are the same, e.g., for vector {3,4,4}, the top twolargest values are both 4.

Pseudocode

/* return largest and second largest element in listl[left...right] precondition: left+1<=right, i.e., the listcontains at least two elements postcondition: the largest andsecondLargest will be set to the largest element and second largestelement respectively */

LargestTwo (l[], left, right, largest, secondLargest)

{ //Write base case(s) and solutions here if (left+1==right)//i.e., there are two elements in the list

//Fill in the blank else

if (left+2==right) //i.e., there are three elements in thelist

//Fill in the blank, i.e., try to set largest, secondLargest

// to the largest and secondLargest values inl[left...right]

mid = (left+right)/2

LargestTwo (l, left, mid, leftL, leftSL)

LargestTwo (l, mid+1, right, rightL, rightSL)

//Fill in the rest of the code

// Note that based upon the postcondition of LargestTwo, wehave

// leftL and leftSL are largest and secondLargest value froml[left...mid] // rightL and rightSL are largest and secondLargestvalue from l[mid+1...right] }

Answer & Explanation Solved by verified expert
3.7 Ratings (318 Votes)
c Codeincludeusing namespace stdfunction the LargestTwovoid LargestTwo vector l int left int right int largest int secondLargest base casesif left1 right ie there are    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students