Kat H.

asked • 05/10/21

Swap Ends. The existing code reads in a size to NUM_VALUES, makes an array called values

The existing code reads in a size to NUM_VALUES, makes an array called values (I am cheating... do not make variable sized arrays!), and then reads in values for it. Your task is to swap the first and last item in the array. Existing code will print out the results of your code.


Input of

4
5 10 15 20


Will make an array 5, 10, 15, 20. You should modify that so it is 20, 10, 15, 5.


Hint: NUM_VALUES represents the length of the array... what expression represents the index of the last element?


Code:


#include <iostream>

#include <iomanip>


using namespace std;


int main()

{

int NUM_VALUES; //cheating to make this flexible

cin >> NUM_VALUES;

double values[NUM_VALUES];

//Read in values

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

cin >> values[i];

}



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

//YOUR_CODE_BELOW



//YOUR_CODE

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

if (values[i] < 0) {

values[i] = 0;

}

}



//YOUR_CODE_ABOVE

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

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

cout << values[i] << " ";

}

return 0;

}

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.