C++

Brock T.

asked • 02/15/22

Largest Two The existing code is trying to call a function printLargestTwo that should take in an array of ints and its size and prints the two largest values from the array. Write that function.

Largest Two

The existing code is trying to call a function printLargestTwo that should take in an array of ints and its size and prints the two largest values from the array. Write that function.


Hint: 

Start by finding just the largest item.

Then modify your code to also look for second largest.You can either:

  1. Modify your existing loop to track the second largest as well. Each number you look at is either:
  2. A) the new largest item (and the old largest is now the second largest)
  3. B) the new second largest (and the largest is unchanged)
  4. C) neither the largest or the second largest
  5. Add another loop to look for the largest thing that is not the largest.



Code:


#include <iostream>


using namespace std;


//Do not modify anything on or above the line below this

//YOUR_CODE_BELOW


//YOUR_CODE


//YOUR_CODE_ABOVE

//Do not modify anything on or below the line above this



int main()

{

int NUM_VALUES; //cheating to make this flexible

cin >> NUM_VALUES;

int values[NUM_VALUES];

//Read in values

for(int i = 0; i < NUM_VALUES; i++) {

cin >> values[i];

}


printLargestTwo(values, NUM_VALUES);

}




1


1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.