Recurrsion

My teacher gave me the following code and told me to give the output, the conclusion I came with is that it will print the contents of array "copystring0" which contains all 0's, but it will not print out the contents of array "copystring1", since the first array will iterate recursively first and hit the base case

Am I correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main()
{
     char testString[50] = "";
     mysteryRecurse(teststring, 4);
     return 0;
}

void mysteryRecurse(char mystring[], int digit)
{
    char copyString0[50];
    char copyString1[50];
    if (digit == 0)
    {
        cout << mystring << endl;
    }
    else
    {
	strcpy(copyString0, mystring);
	strcat(copyString0, "0");
	mysteryRecurse(copyString0, digit - 1);

	strcpy(copyString1, mystring);
	strcat(copyString1, "1");
	mysteryRecurse(copyString1, digit - 1);
    }
}

So the second block of code which contains:

strcpy(copyString1, mystring);
strcat(copyString1, "1");
mysteryRecurse(copyString1, digit - 1);

will not get iterated right? Since the first block will hit the base case first?

No, you are incorrect.
closed account (48T7M4Gy)
At the risk of sounding facetious alex067 this is what you get for output so far. As a practical suggestion why don't you fill out the #includes etc to fix up you program and run it.
In function 'int main()':
4:21: error: 'teststring' was not declared in this scope
4:34: error: 'mysteryRecurse' was not declared in this scope
3:11: warning: unused variable 'testString' [-Wunused-variable] In function 'void mysteryRecurse(char*, int)':
14:9: error: 'cout' was not declared in this scope
14:29: error: 'endl' was not declared in this scope
18:30: error: 'strcpy' was not declared in this scope
19:25: error: 'strcat' was not declared in this scope
Last edited on
Topic archived. No new replies allowed.