Assume you have a swap routine in place for doubles. Write a routine calls swap that reverses the entries in an array of doubles. Use this function declaration. void reverse( double a, int size); Carry out your reversal in place, do not make a local copy of the array
What will be an ideal response?
The answer is in bold face embedded in a complete test program.
```
include
void swap(double& lhs, double& rhs) { double tmp = lhs;
lhs = rhs;
rhs = tmp;
}
void reverse( double a[], int size)
{
using namespace std;
for (int i = 0; i < (size - 1)/2; i++)
swap(a[i], a[size - i - 1]);
}
int main()
{
using namespace std;
double foo[SIZE] = {0,1,2,3,4,5,6,7};
for (int i = 0; i < SIZE; i++)
cout << foo[i] << " ";
cout << endl;
reverse(foo, SIZE);
for (int i = 0; i < SIZE; i++)
cout << foo[i] << " ";
cout << endl;
}
```
Note the (size - 1)/2 in the for loop test. If this is size-1, the routine reverses the array twice, undoing the reverse.
You might also like to view...
You measure font size using pixels.
Answer the following statement true (T) or false (F)
Under a work for hire contract, the contracting company, not the creator, own the copyright
Indicate whether the statement is true or false
When you receive a compressed file, you must zip it.
Answer the following statement true (T) or false (F)
Which of the following is NOT stored in the Windows registry?
A. configuration settings about your hardware and software B. file names C. application default settings D. operating system information