In: Computer Science
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
// C++ program to create a function that will take in an array (of type double), and will return the array with all of the elements doubled
#include <iostream>
using namespace std;
// function that takes input the array and size of the array
// and returns elements of the array doubled
void elementsDoubled(double array[], int size)
{
// loop to double the elements of the array being doubled
for(int i=0;i<size;i++)
{
array[i] = array[i]*array[i];
}
}
int main()
{
int size;
// input the number of elements in the array
cout<<"Enter the number of elements : ";
cin>>size;
double array[size];
// loop to input the elements of the array
for(int i=0;i<size;i++)
{
cout<<"Enter element-"<<(i+1)<<" : ";
cin>>array[i];
}
elementsDoubled(array,size); // call the function to double the elements of the array
// display the array elements
for(int i=0;i<size;i++)
cout<<array[i]<<" ";
cout<<endl;
return 0;
}
//end of program
Output: