Trying to Recursively match numbers in 2 hard-coded arrays, unable to go further than 1 successful cycle

36

I don't think recursion is the correct method for solving this task. Using two for-loops instead seems a better solution. Like:

for(i=0; i<6, ++i)
    for(j=0; j<6, ++j)
        if (nuts[i] == bolts[j])
        {
            // print …

        }
    }
}

Anyway - if you really want recursion for this task it could look like:

#include <stdio.h>

#define NUM_ELEMENTS 6

void recurMatch(int nuts[], int nuts_pos, int bolts[], int bolts_pos);

int main() {
    int nuts[NUM_ELEMENTS] = {4, 7, 9, 8, 2, 5};
    int bolts[NUM_ELEMENTS] = {5, 2, 9, 9, 7, 4};
    printf("Let's clean up this Storage Chest...\n");
    recurMatch(nuts, 0, bolts, 0);
    return 0;
}

void recurMatch(int nuts[], int nuts_pos, int bolts[], int bolts_pos) 
{
    if (nuts_pos == NUM_ELEMENTS)
        return;  // All done - just return

    if (bolts_pos == NUM_ELEMENTS) 
        return recurMatch(nuts, nuts_pos+1, bolts, 0); // Next nuts element

    if (nuts[nuts_pos] == bolts[bolts_pos])  // Check current elements
    {
        printf("Nut Size-'%d' found in Nut-Drawer #%d, is equal"
               " to Bolt Size-%d found in Bolt-Drawer #%d.\n",
               nuts[nuts_pos], nuts_pos, bolts[bolts_pos], bolts_pos);
    }

    return recurMatch(nuts, nuts_pos, bolts, bolts_pos+1);  // Next bolts element
}

Output:

Let's clean up this Storage Chest...
Nut Size-'4' found in Nut-Drawer #0, is equal to Bolt Size-4 found in Bolt-Drawer #5.
Nut Size-'7' found in Nut-Drawer #1, is equal to Bolt Size-7 found in Bolt-Drawer #4.
Nut Size-'9' found in Nut-Drawer #2, is equal to Bolt Size-9 found in Bolt-Drawer #2.
Nut Size-'9' found in Nut-Drawer #2, is equal to Bolt Size-9 found in Bolt-Drawer #3.
Nut Size-'2' found in Nut-Drawer #4, is equal to Bolt Size-2 found in Bolt-Drawer #1.
Nut Size-'5' found in Nut-Drawer #5, is equal to Bolt Size-5 found in Bolt-Drawer #0.
Share:
36

Related videos on Youtube

J Ben
Author by

J Ben

Updated on December 07, 2022

Comments

  • J Ben
    J Ben over 1 year

    I'm trying to recursively create a call that cycles through all the elements in the arrays, and compares each element to the other. Similar to sorting without actually altering the arrays, just pretending to using print statements.

    Here's what I have:

    #include <stdio.h>
    // Send each pair of array indexes to a recursive
    // Function that compares the values received and returns
    // if a corresponding 'index' (will be named drawer within storage chest) 
    matches,
    // "A match was found for nut 7' and bolt 7' in drawers 2 and 2."
    
    void recurMatch(int nuts[], int bolts[], int position);
    
    int main() {
    
        int nuts[6] = {4, 7, 9, 8, 2, 5};
        int bolts[6] = {5, 2, 9, 9, 7, 4};
        printf("Let's clean up this Storage Chest...\n");
        recurMatch(nuts, bolts, 0);
        return 0;
    
    }
    
    void recurMatch(int nuts[], int bolts[], int position) {
        int i, j;
    
        for (i = 0, j = 0; nuts[i] <= 6; ++i, ++j) {
    
            //printf("Position is %d\n", position);
    
            ++position;
            //printf("Nuts #%d is: Size-%d\n", i + 1, nuts[i]);
            //printf("Bolts #%d is: Size-%d\n\n", j + 1, bolts[j]);
    
            if (nuts[i] == bolts[j]) {
                printf("Nut Size-'%d' found in Nut-Drawer #%d, is equal to Bolt Size-%d found in Bolt-Drawer #%d.\n",
                   nuts[i], i + 1, bolts[j], position);
                //nuts[i] = nuts[i + 1];
                position = 1;
                //i;
                //return recurMatch(nuts, bolts, position);
                //recurMatch(nuts + 1, bolts, 0);
            } else {
                return recurMatch(nuts, bolts + 1, position);
            }
    
        }
    
    }
    

    The output:

    Let's clean up this Storage Chest...
    Nut Size-'4' found in Nut-Drawer #1, is equal to Bolt Size-4 found in Bolt-Drawer #6.
    

    Which is correct, but how do I continue this process (recursively) until it gets through all 6 nuts with its proper pair?

    Thank you for any help.

    • thinice
      thinice over 10 years
      ps -waux |grep -i 'mysqld' - is it running already?
    • TheFiddlerWins
      TheFiddlerWins over 10 years
      Please post your my.conf
    • AngryWombat
      AngryWombat over 10 years
      we need to see your my.conf, almost sounds like mysql user can't create files to expected locations, i.e. socket file
    • Support Ukraine
      Support Ukraine over 5 years
      Why do you want to use recursion for this problem?
    • Stephan Lechner
      Stephan Lechner over 5 years
      What's the purpose of nuts[i] <= 6? Did you mean i<=6?
    • J Ben
      J Ben over 5 years
      @StephanLechner my bad, it was just something I was testing. It was originally i <=6
    • Support Ukraine
      Support Ukraine over 5 years
      @JBen Even i <= 6 would be wrong. It should be i < 6
  • J Ben
    J Ben over 5 years
    Thanks, I'm just having a hard time learning the basics of recursion.
  • Support Ukraine
    Support Ukraine over 5 years
    @JBen Recursion is pretty difficult so don't worry. One of the basic things in recursion is the stop condition, i.e. when shall you stop calling the function again. In this case there are kind of like two stop conditions. 1) When nuts_pos reach 6 (i.e. all done) and 2) when bolts_pos reach 6 (i.e. done with current nuts element). Anyway - when dealing with recursion always start by considering the stop condition.
  • J Ben
    J Ben over 5 years
    I'll remember that. Makes it a lot clearer when I can apply the principles to things I've worked on.