I need help in understanding a function in the code

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

int main(void)
{
double data[] = { 3.0, 10.0, 1.5, 15.0, 2.7, 23.0,
4.5, 12.0, 6.8, 13.5, 2.1, 14.0 };

int len(_countof(data)); // Number of elements

for(auto value : data)
cout << setw(6) << value;

lowest(data, len) = 6.9; // Change lowest to 6.9
lowest(data, len) = 7.9; // Change lowest to 7.9
cout << endl;

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
}
When posting code use code tags http://www.cplusplus.com/articles/jEywvCM9/

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.
Topic archived. No new replies allowed.