
Patrick B. answered 05/11/21
Math and computer tutor/teacher
int iTemp = values[NUM_VALUES-1];
values[NUM_VALUES-1] = values[0];
values[0] = iTemp;
Kat H.
asked 05/10/21The 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
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;
}
Patrick B. answered 05/11/21
Math and computer tutor/teacher
int iTemp = values[NUM_VALUES-1];
values[NUM_VALUES-1] = values[0];
values[0] = iTemp;
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.