V
V
Vladimir Korshunov2021-04-18 00:16:57
recursion
Vladimir Korshunov, 2021-04-18 00:16:57

How to terminate a recursive algorithm?

It is necessary to check whether all elements of the array pass the condition cos(x[i]) > 0, while using a recursive function, each time dividing the array into two parts - 1/3 and 2/3, checking first the first, then, if necessary, second. Stop recursion when 1 - 2 elements are left. Here is what I was able to write, while the code is conditional, so far the function does not return anything, it's all very roughly:

prov (double *a, int N, int border, int border2, flag, exit)
{
if (!exit)
    {
        if (N == border) {
              if (cos(a[N]) > 0) 
              { 
                    flag = true; 
              }
              else{
                   exit = true;
              }
        }
        if (!flag)
        {
            prov (a, (N - border)/3, border, N, flag, exit);
        }
        else
        {
            border++;
        }
    }
}


Well, I’ll get to the moment when I have one element left, I’ll check it, how can I check the other 2/3? So I decided to shift the left border when I get to this element, but I don’t understand what to do next.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question