Sometimes you want to know index of the minimal value of an array. I’ve frequently seen programmers using two data structures to accomplish this task: one to hold the index and the other to hold the value (i.e. current minimum) while iterating. Unless you need to optimize your code for speed you really only need an integer for this task.

int find_min(int* vals, int len)
{
    int mindx= 0;
    for (int i = 1; i < len; i++)
        if (vals[i] < vals[mindx]) mindx= i;
    return mindx;
}

0 comment(s) :

Newer Post Older Post Home

Blogger Syntax Highliter