logo CodeStepByStep logo


Language/Type: C++ Vector collections
Related Links:
Author: Marty Stepp (on 2016/06/20)

Write a function named stretch that accepts as a parameter a reference to a Vector of integers, and modifies it to be twice as large, replacing every integer with a pair of integers, each half the original. If a number in the original vector is odd, then the first number in the new pair should be one higher than the second so that the sum equals the original number. For example, if a variable named v refers to a vector storing the values {18, 7, 4, 24, 11}, the call of stretch(v); should change v to contain {9, 9, 4, 3, 2, 2, 12, 12, 6, 5}. (The number 18 is stretched into the pair 9, 9, the number 7 is stretched into 4, 3, the number 4 is stretched into 2, 2, the number 24 is stretched into 12, 12, and 11 is stretched into 6, 5.)

Type your C++ solution code here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.