I am looking to modify a counter which is a field in a channel when i submit or update an entry. I am looking to count the number of matrix rows and then add that into the input field to act as the counter.

What is the best way for me to do this?

What hook should i use?

And what files should i create?

Thanks


Question: Euclid's Algorithm (Greatest Common Divisor)
Answer: Instead of the using the while(true) construct, it's clearer and readable to state a condition necessary for the loop to continue. Also, gcd is defined for negative integers as well and they can be handled easily. 

The base condition is:

gcd(m, n) = gcd(n, m%n)   when n != 0
          = m             when n = 0


When both m and n are zero, then gcd is not defined and we return -1.

#include &lt;stdio.h&gt;

int gcd(int m, int n) {
    if(m == 0 &amp;&amp; n == 0)
        return -1;

    if(m &lt; 0) m = -m;
    if(n &lt; 0) n = -n;

    int r;
    while(n) {
        r = m % n;
        m = n;
        n = r;
    }
    return m;
}