In: Computer Science
C++ (cpp) PLEASE
Write a complete function (prototype and definition) that takes an integer array and the array dimensions as an input. The function should then square each value of the array in-place.
At the completion of the function return a Boolean which indicates the process has completed.
CODE:
#include <iostream>
using namespace std;
//function definition
bool function(int a[],int n){
//if no elements in the array
if(n==0){
//returning false as no updated done
return false;
}
//iterating each elements of the array
for(int i=0;i<n;i++){
//updating each element by its square
a[i] = a[i]*a[i];
}
//returning true after updating array
return true;
}
int main()
{
//to store the dimension of array
int n;
cout<<"Enter size of array : ";
//input of array dimension
cin>>n;
//array initialization
int a[10000];
cout<<"Enter array elements: ";
//input of array elements
for(int i=0;i<n;i++){
cin>>a[i];
}
// calling function, if it returns true, then code inside if will
execute
if(function(a,n)){
cout<<"Updated array elements are: ";
//printing updated array elements
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
// if no update is performed
else{
cout<<" Operation not performed ";
}
return 0;
}
OUTPUT:
Enter size of array : 5 Enter array elements: 1 2 3 4 5 Updated array elements are: 1 4 9 16 25 . Program finished with exit code 0 Press ENTER to exit console.