doubleArray takes an int array and the arrays size as argume

doubleArray: takes an int array and the array\'s size as arguments. It should

create a new array that is twice the size of the argument array. The function

should copy the contents of the argument array to the first half of the new array,

and again to the second half of the new array. The function should return a

pointer to the new array.

Solution

/**

C ++ program that tests the function doubleArray method.
*/
#include <iostream>
using namespace std;
//function prototype
int* doubleArray (int* arr, int SIZE);
//main function
int main()
{
   const int SIZE = 10;                                          
   int arr [SIZE] = {5,6,7,8,9,10,11,12,13,14};              
  

   //copy the address of arr to pointer num
   int* pInteger = arr;                                                  

   cout<<\"array elements \"<<endl;
   for(int index= 0;index<SIZE;index++)
       cout<<pInteger[index]<<\" \";
  
   cout<<endl;
   cout<<\"Calling doubleArray function \"<<endl;

   pInteger = doubleArray(arr, SIZE);  

   for(int index=0;index<2*SIZE;index++)
       cout<<pInteger[index]<<\" \";
       
   system(\"pause\");
   return 0;
}
/*************************************************************
The function doubleArray accepts an int array and the
size as arguments. The function then creates*
a new array that is twice the size of the argument array.*
*************************************************************/
int* doubleArray(int* arr, int size)
{
   //Create a dynamic array of double the size of integer array,arr
   int* darr=new int[size*2];

   //copy the elements of arr to darr
   for (int index1=0;index1<size;index1++)
       darr[index1]=arr[index1];

   //set start=0
   int start=0;
   for   (int index2=size;index2<size*2;index2++,start++)
       darr[index2]=arr[start];


   //return darr
   return darr;
}

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

Sample output:

array elements
5 6 7 8 9 10 11 12 13 14
Calling doubleArray function
5 6 7 8 9 10 11 12 13 14 5 6 7 8 9 10 11 12 13 14
.

doubleArray: takes an int array and the array\'s size as arguments. It should create a new array that is twice the size of the argument array. The function shou
doubleArray: takes an int array and the array\'s size as arguments. It should create a new array that is twice the size of the argument array. The function shou

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site