Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a void function called repeatArray that takes two parameters - a reference

ID: 3826159 • Letter: W

Question

Write a void function called repeatArray that takes two parameters - a reference to a pointer to a dynamically allocatedarray of doubles, and the size of that array. The pointer is passed by reference because you want to change the value of the pointer. The function should replace the array with one that is twice as large, with the values from the original array appearing twice. For example, if array that was passed in was {3.1, 4.2, 5.3}, then it should be replaced by {3.1, 4.2, 5.3, 3.1, 4.2, 5.3}.  The function should prevent any memory leaks. Remember to also prevent memory leaks in the main you use for testing.

For example, it could be used like this:

For the following projects (and all future projects in this course):

Do not include a main method in the files you submit - just the definitions of the assigned functions. I will compile your code with my own main method for testing, and there can only be one main method in a program. You will of course need to have a main method for testing purposes - just make sure you comment it out (or delete it) before submitting your file.

Do not use any global variables. Global variables and their drawbacks are discussed on pages 357-9 of the textbook. If the assignment description doesn't specify a return value for a function, then it should have a return type of void.

The file must be named repeatArray.cpp.

Explanation / Answer

//if my solution is helpful , please rate..

//repeatArray.cpp,

void repeatArray(double **ptr, int size)

{
   double *tmp;
   //dynamically allocate array of double with double the size of original array pointed by ptr
   tmp = new double[size * 2];

   //now copy the array elements pointed by ptr to tmp array
   for (int i = 0, j = 0; i < size * 2; i++, j++)
   {
       if (j >= size)
       {
           j = 0;
       }
       tmp[i] = (*ptr)[j];
   }
   //delete original array
   delete[] * ptr;
   //now assign original pointer ptr with address of tmp so that it points to new array which has repeated elements
   *ptr = tmp;
}

//in main function ,

//declare function repeatArray function as extern as to indicate compiler that definition of the function is in other file .

extern void repeatArray(double **ptr, int size);

-----------------------------------------------------------

//output

2
4
6
2
4
6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote