0

I'm new in C, and learning recursive algorithms.

I'll like to assign an ID to each time the execution goes into the IF statement, to retrieved it, and print it as a new execution ID, but I don't know how to achieve it.

int recursion = 0;
void merge_sort_recursion(int arr[], int left, int right)
{
  if (left < right)
  {

    int mid = left + (right - left) / 2;
    recursion++;
    printf("[%d]  NEW RECURSION ID\n", recursion);
    printf("     variables:\tleft: %d mid: %d right: %d\n", left, mid, right);

    printf("[%d] recursive LEFT  \t\n", recursion);
    merge_sort_recursion(arr, left, mid);

    printf("[%d] recursive RIGHT \t\n", recursion);
    merge_sort_recursion(arr, mid + 1, right);

    printf("[%d] recursive MERGE \t\n", recursion);
    merge_sorted_arrays(arr, left, mid, right);
  }
}

So far, all in print is this:

[1]  NEW RECURSION ID
[1] recursive LEFT  
[2]  NEW RECURSION ID
[2] recursive LEFT  
[2] recursive RIGHT 
[2] recursive MERGE 
[2] recursive RIGHT 
[3]  NEW RECURSION ID
[3] recursive LEFT  
[3] recursive RIGHT 
[3] recursive MERGE 
[3] recursive MERGE 

But, I'm looking for something like this:

[1]  NEW RECURSION ID
[1] recursive LEFT  
[2]  NEW RECURSION ID
[2] recursive LEFT  
[2] recursive RIGHT 
[2] recursive MERGE 
[1] recursive RIGHT 
[3]  NEW RECURSION ID
[3] recursive LEFT  
[3] recursive RIGHT 
[3] recursive MERGE 
[1] recursive MERGE 

Thank you,

Newbie
  • 11
  • 2

1 Answers1

1

You can do:

void merge_sort_recursion(int arr[], int left, int right)
{
  static int recursion = 0;
  if (left < right)
  {
    recursion++;
    ...
  }
}

This will effectively count the number of times the if statement is entered.

To learn more about static variables in C, check out What does "static" mean in C?

yano
  • 4,827
  • 2
  • 23
  • 35
  • That would give the same results as using a global variable which is what OP currently does. – interjay Jun 23 '22 at 19:59
  • @interjay using a `static` in the function provides better scoping, but yes, based on the code presented it would give identical results. I admit on closer inspection of the actual vs expected output (which doesn't match the code), I cannot quite figure out what OP is trying to achieve, and should have withheld answering. Jumped the gun on IMO the best way to "assign an ID to each time the execution goes into the IF statement". – yano Jun 23 '22 at 20:34
  • Hi, sorry if there's any misunderstanding (not native speaker); I'm trying to give and ID to each execution (each has a PRINTF of " NEW RECURSION ID", "recursive LEFT", "recursive RIGHT", "recursive MERGE"). In the desired output, you can see line 7 and 12, have [1], because they correspond to the first time the `if (left < right)` was called. Also, the code doesn't match because I took a snippet instead of the whole function. – Newbie Jun 23 '22 at 21:54
  • @Newbie You're saying you want to individually count the LEFT, RIGHT, and MERGE paths? I initially thought that, but then why does line 5 in desired output have [2] for recursive RIGHT. Shouldn't that be [1] for the first time recursive RIGHT is called? Same for lines 6, 11, and 12 for recursive MERGE. – yano Jun 23 '22 at 22:03
  • I've run it on https://pythontutor.com/c.html, and line 5 in desired output appear as part of the second time the recursive function is called have [2] for recursive RIGHT. Here's the full code so far: https://replit.com/@PalaGato76219/SaddlebrownAcademicExtensions – Newbie Jun 23 '22 at 22:29
  • @Newbie Sorry, I'm not following, I'll probably delete this answer. Hopefully someone else can understand. – yano Jun 23 '22 at 22:34
  • 1
    Thank you @yano, I'll review it to express myself better; even though the `static` documentation is very much appreciated. – Newbie Jun 24 '22 at 10:18