Write a function that receives an array of floats of 5 elements. The function (called sqrt_float) should return an array of floats that contains the square root of each value in the first array. If the value of the square root is less than 2, then replace it with the value 0. Print the new values back in the main function after calling the function.
Program :-
#include <iostream>
#include <cmath>
using namespace std;
void sqrt_float(float*);
// We cannot return an entire array
from a function instead what we can do is we can pass the address
of the array into the function in this way we do not need to retrun
anything or we can return a pointer to the array from the
function.
int main() {
float arr[5];
for(int i = 0; i <= 4; i++) {
cout << "Enter value "
<< i+1 << ": ";
cin >> arr[i];
}
sqrt_float(arr);
cout << "Values in the array are: " <<
endl;
for(int i = 0; i <= 4; i++) {
cout << arr[i] << "
";
}
cout << endl;
}
void sqrt_float(float *a) {
for(int i = 0; i < 5; i++) {
a[i] = sqrt(a[i]);
if(a[i] < 2) {
a[i] = 0;
}
}
}
Output:-
Hope this helps. If you have any queries or suggestions regarding the answers please leave them in the comments section so I can update and improve the answer. Thank you.
Get Answers For Free
Most questions answered within 1 hours.