I have this code that I need help in understanding how the function in the program knows the lowest value in an array. It’s basically a for loop in the function that I quite don't understand. Can you please look at the code and look at the function located at the end of the code, inorder to help me understand that particular function. Thanks in advance
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
double& lowest(double values[], const int& length); // Function prototype
for(auto value : data)
cout << setw(6) << value;
cout << endl;
return 0;
}
// Function returning a reference
double& lowest(double a[], const int& len)
{
int j(0); // Index of lowest element
for(int i = 1; i < len; i++)
if(a[j] > a[i]) // Test for a lower value...
j = i; // ...if so update j
return a[j]; // Return reference to lowest element
}
What specifically are you having trouble with? The function lowest takes an array of doubles and a length parameter, finds the lowest value in the array, then returns a reference to it.