recursion (part II)

I have a working program here with two recursive functions. The assignment here is:

a. Write a recursive function writeLine() that writes a character repeatedly to form a line of n characters. For example, writeLine('*',5) should produce the line *****.

b. Write a recursive function writeBlock() that uses the method writeLine() to write m lines of n characters each. For example, writeBlock('*',5,3) should produce the following output:
*****
*****
*****



Here is the working solution. However there is an error on my part. It compiled and everything but when I choose the number of lines to return in the main function, it only returns 0's until the stack overflows and the program crashes. What did I do wrong ?

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

int writeLine(char, int);
int writeBlock(int wRvar, int lnum);

int main()
{
	char answer;
	char xchar;
	int numInt;
	do 
	{
	   cout << "Enter a character: " << endl;
	   cin >> xchar;
           cout << "Enter a non-negative number: " << endl;
	   cin >> numInt;
	   writeLine(xchar, numInt);
	   cout << endl;
	   int lnum;
	   int wRvar = writeLine(xchar, numInt);
	   cout << "Enter number of lines";
	   cin >> lnum;
	   writeBlock(wRvar, lnum);
	   cout << "again? ";
	   cin >> answer;
	}
	while (answer == 'Y' || 'y');
}

int writeLine(char x, int num)
{
	if (num >= 1)
	{
		cout << x;
		writeLine(x, num - 1);
	}
	return 0;
}

int writeBlock(int wRvar, int lnum)
{
	if (lnum >= 1)
	{
		cout << wRvar;
		writeBlock(wRvar, lnum);
	}
	return 0;
}


Help is much appreciated
Last edited on
Hint: Compare lines 46 and 36. Notice any missing minuses? ;)

-Albatross
Last edited on
writeBlock does exactly the same thing as writeLine (aside from Albatross's comment), and doesn't fulfill the assignment requirement.

Edit: Both will also always return 0.
Last edited on
i figured out the return 0; thing and took that out but i cant figure out how to fix writeBlock()
ok so i edited it
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;

void writeLine(char, int);
int writeBlock(int wRvar, int lnum);

int main()
{
	char answer;
	char xchar;
	int numInt;
	
	do 
	{
       
	   cout << "Enter a character: " << endl;
	   cin >> xchar;
           cout << "Enter a non-negative number: " << endl;
	   cin >> numInt;
	   // i need to somehow make this variable a type 'void' 
           //wRvar = writeLine(xchar, numInt);
	   writeLine(xchar, numInt);
	   cout << endl;
	   int lnum;
	   
	   cout << "Enter number of lines";
	   cin >> lnum;
	   writeBlock(wRvar, lnum);
	   cout << "again? ";
	   cin >> answer;
	}
	while (answer == 'Y' || 'y');
}

void writeLine(char  x, int  num)
{
	
	if (num >= 1)
	{
		cout << x;
		writeLine(x, num - 1);
	}
	
	
}

int writeBlock(int wRvar, int lnum)
{
	if (lnum >= 1)
	{
		cout << endl;
		writeBlock(wRvar, lnum - 1);
	}
	return wRvar;
}


how would i make that variable of type 'void'?
closed account (zwA4jE8b)
why do you want your variable to be of type void?
well because if i make both of the recursive functions type 'int', then they have to return a value right? idk how to write a recursive function that isnt a void. if i set the variable wRvar = writeLine(xchar, numInt) then when i use it in the 'void' recursive function writeBlock() i dont know what type wRvar should be.
how would i define the type for wRvar?
Your assignment declares writeBlock should take 3 args writeBlock('*',5,3), neither function needs to return anything. You already ask the user for number of characters to output per line and how many lines. You need a call to writeLine within writeBlock for this to work.
Just a side note, this: while (answer == 'Y' || 'y'); will not do what you expect it to. It tests answer for 'Y', and then tests 'y' by itself (which will always evaluate to true). It's necessary to restate what object you're comparing. while(answer == 'Y' || answer == 'y') will work properly.

An alternative is including <cctype> and using tolower(char) or toupper(char) to lose case sensitivity.
1
2
3
4
5
6
7
8
9
10
#include <cctype>
...
    while(tolower(answer) == 'y') {
        ...
    }
    // OR
    while(toupper(answer) == 'Y') {
        ...
    }
...
There is still an "unresolved external" . I feel like im getting closer..
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

void writeLine(char, int);
void writeBlock(char& , int& , int&);

int main()
{
	char answer;
	char xchar;
	int numInt;

	do 
	{
           //testing writeLine()
	   cout << "Enter a character: " << endl;
	   cin >> xchar;
           cout << "Enter a non-negative number: " << endl;
	   cin >> numInt;
	   writeLine(xchar, numInt);
	   cout << endl;

	   //testing writeBlock()
	   int lnum;
	   cout << "Enter number of lines";
	   cin >> lnum;
	   writeBlock(xchar , numInt,  lnum);
	   cout << "again? ";
	   cin >> answer;
	}
	while (answer == 'Y' || answer == 'y');
}

void writeLine(char x, int num)
{
	
	if (num >= 1)
	{
		cout << x;
		writeLine(x, num - 1);
	}	
}

void writeBlock(char const & x, int const & num, int const & lnum)
{
	if (lnum >= 1)
	{
		cout << x;
		writeLine(x, num);
		writeBlock(x, num, lnum - 1);
	}
}
Last edited on
closed account (zwA4jE8b)
Still working on it?

you do not need address of's (&) in writeblock

you will need an endl or \n somewhere in writeblock to get your new lines.

also doing cout << x and then calling writeline is going to produce too many x's, writeline takes care of outputting the line of char's, right?

The unresolved external is because you added constant's to the writeBlock definition but not declaration and the call to writeBlock has too few args. As Creative said the references aren't necessary neither are the const.
Last edited on
Finally got a working program lol

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

void writeLine(char, int);
void writeBlock(char , int , int);

int main()
{
	char answer;
	char xchar;
	int numInt;

	do 
	{
           //testing writeLine()
	   cout << "Enter a character: " << endl;
	   cin >> xchar;
           cout << "Enter a non-negative number: " << endl;
	   cin >> numInt;
	   writeLine(xchar, numInt);
	   cout << endl;

	   //testing writeBlock()
	   int lnum;
	   cout << "Enter number of lines";
	   cin >> lnum;
	   writeBlock(xchar, numInt,  lnum);
	   cout << "again? ";
	   cin >> answer;
	}
	while (answer == 'Y' || answer == 'y');
}

void writeLine(char x, int num)
{
	
	if (num >= 1)   //anchor case
	{
		cout << x;
		writeLine(x, num - 1);     //inductive
	}	
}

void writeBlock(char  x, int  num, int  lnum)
{
	if (lnum >= 1)       //anchor case
	{
	    writeLine(x, num);
            cout << "\n";
            writeBlock(x, num, lnum - 1);    //inductive
	}
}

Topic archived. No new replies allowed.