Bourezg
Bourezg

Reputation: 59

How do I print two arrays using recursion, one after another (i.e. not concurrently)?

My code is posted as follows and currently prints out "1,6,2,7,3,8,4,9,5,10,DONE", in other words, it goes through and prints out the first arrays first element then the second arrays first element then the first arrays second and the second arrays second and so on and so forth (sorry for the run on sentence). I want an output of "1,2,3,4,5,6,7,8,9,10,DONE" so that the first array is completely printed, then the second array is too, then DONE is printed. PLEASE KEEP RECURSION IN THIS FUNCTION (I'm trying to understand the fundamentals of recursively calling a function):

#include <stdio.h>
#define N 5
void printcombo(int* a, int* b)
{
    int first,second;
    first = *a;
    second = *b;    
    if (first != 0 && second !=0)
    {
        printf("%d,",first);
        printf("%d,",second);
        printcombo(a+1,b+1);
    }
    else
    {
        printf("DONE\n");
    }

 }


int main()
{
    int a[N] = {1,2,3,4,5};
    int b[N] = {6,7,8,9,10};
    printcombo(a,b);
    return 0;
}

I would appreciate it if only a small bit of the code was changed, but obviously do what you gotta do.

Upvotes: 0

Views: 121

Answers (3)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>

#define N 5

void printcombo(int* a, int* b){
    int first,second;
    first = *a;
    second = *b;
    if(first == 0 && second == 0){
        printf("DONE\n");
        return ;
    }
    if (first != 0 && second !=0){
        if(first < second){
            printf("%d,",first);
            ++a;
        } else {
            printf("%d,",second);
            ++b;
        }
    } else if (first != 0 && second == 0){
        printf("%d,",first);
        ++a;
    } else if (first == 0 && second != 0){
        printf("%d,",second);
        ++b ;
    }
    printcombo(a, b);
}

int main(){
    int a[N+1] = {1,2,3,4,5, 0};//last element is 0 and sorted
    int b[N+1] = {6,7,8,9,10, 0};
    printcombo(a,b);
    return 0;
}

Upvotes: 0

Erik
Erik

Reputation: 91300

Here's one variant:

#include <stdio.h>
#define N 6

void printcombo(int* a, int* b)
{
    int first,second;
    first = *a;
    second = *b;
    if (first != 0) {
        printf("%d ", first);
        printcombo(a+1,b);
    } else if (second != 0) {
        printf("%d ", second);
        printcombo(a,b+1);
    } else {
        printf("DONE\n");
    }
 }


int main()
{
    int a[N] = {1,2,3,4,5,0};
    int b[N] = {6,7,8,9,10,0};
    printcombo(a,b);
    return 0;
}

Upvotes: 2

j__m
j__m

Reputation: 9635

Can you simply have two separate invocations of your recursive function?

(I've also added zeros to the end of the arrays, since if you're checking for it you ought to put it there. It's bad form to assume.)

#include <stdio.h>
#define N 6
void printarray(int* n)
{
    int number;
    number = *n;
    if (number != 0)
    {
        printf("%d,",number);
        printcombo(n+1);
    }
}


int main()
{
    int a[N] = {1,2,3,4,5,0};
    int b[N] = {6,7,8,9,10,0};
    printarray(a);
    printarray(b);
    printf("DONE\n");
    return 0;
}

Upvotes: 0

Related Questions