Write a method (not a complete program, only a method but you must include the m
ID: 3852151 • Letter: W
Question
Write a method (not a complete program, only a method but you must include the method signature) called halfSize that accepts an integer array as a parameter and returns a reference to a new integer array that is half (rounded down) as long and contains the elements of the first array in the same positions that fit within this shorter array. If the first array is only 1 element long, then just return a copy.
For example if the array contains the following integers:
The method halfSize will return:
Explanation / Answer
Here is a method (without complete program as requested by you).
long& halfSize(int *arr)
{
int size;
int length = sizeof(arr)/sizeof(*arr);
if (length == 1)
{
long *p = new long[1];
p[0] = arr[0];
return *p;
}
else
{
size = length/2;
long *p = new long[size];
for (int i = 0; i < size; i++)
{
p[i] = arr[i];
}
return *p;
}
}
I've not written the complete program because you have specifically asked for a method. You can test this and if you face any issue, do let me know. i'll be happy to help you. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.