What is this function called?

Sep 1, 2014 at 12:36am
I'm doing a practice problem that asks what foo(dat, 7) returns, and it also asks what this function is called. As far as I can tell, it finds the smallest value in the array and returns its array position, but I'm unsure of it's technical name. The code is below.

Given: dat[] = { 3.14159, 2.71828, 9.81, 32.3, 0, 42, 2.52 };
1
2
3
4
5
6
7
int foo (double dat [ ] , int n ) {
int r = 0 ;
for ( int i = 1 ; i < n ; i++) {
if ( dat [ i ] < dat [ r ] ) r=i ;
}
return r ;
}



My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
int foo (double dat [ ] , int n ) ;
int main()
{
    double dat[] = { 3.14159, 2.71828, 9.81, 32.3, 0, 42, 2.52 };
    std::cout<<foo(dat,6);
return 0;
}
int foo (double dat [ ] , int n ) {
int r = 0 ;
for ( int i = 1 ; i < n ; i++) {
if ( dat [ i ] < dat [ r ] ) r=i ;
}
return r ;
}
Sep 1, 2014 at 1:19am
As far as I can tell, it finds the smallest value in the array and returns its array position


It looks the same to me. Looks like you have the function figured out correctly.

but I'm unsure of it's technical name


AFAIK there isn't an "official" name. I think the point of the assignment is to just come up with a your own name for the function, as long as that name appropriately represents what the function does.

Sep 1, 2014 at 6:41am
The standard library has a function (in <algorithm>) that returns an iterator to minimum element of range, i.e. essentially the same thing. Its name, well the standard uses some confusing names and some intuitive names.
Sep 1, 2014 at 6:49am
Small correction to line 8:
std::cout<<foo(dat,7);
Topic archived. No new replies allowed.