Problem with recursion

Hello. I'm hoping to get some hints for this recursion problem.
I am supposed to write the definition of a function named printPowerOfTwoStars that receives a non-negative integer N and prints a line consisting of "2 to the N" asterisks. So, if the function received 4 it would print 2 to the 4 asterisks, that is, 16 asterisks:

****************

and if it received 0 it would print 2 to the 0 (i.e. 1) asterisks:

*

The function must not use a loop of any kind (for, while, do-while) to accomplish its job. I am only allowed to use the one function, printPowerOfTwoStars.

Here is my code and it seems to be working correctly in visual studio 2013 when I run it, however when I try to submit it in myprogramminglab it will not accept it, saying that it goes into an infinite loop.

I've tried this problem about 60 times now and I am lost..any hints would be greatly appreciated!


1
2
3
4
5
6
7
8
9
10
11
12
  void printPowerOfTwoStars(int N){
    
    if (N == 0){
        cout << "*";
    }
    
    else {
        printPowerOfTwoStars(N - 1);
        printPowerOfTwoStars(N - 1);
    }

}
Hello,

I am new to programming and am working on a recursion problem for myself, but here was my attempt at your problem:

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
#include <iostream>

using namespace std;

int printStar(unsigned int Exponent)
{
	int sum=0;
	if (Exponent == 0)
	{
		sum = Exponent;
		return 1 + sum;
	}
	else 
	{
		sum = printStar(Exponent - 1);
		return printStar(Exponent - 1) + sum;
	}
}

int main()

{
	cout << printStar(4);
	return 0;
}


The maths side of it is there, so I will let you figure out how to print out the stars accordingly.

If anyone else can give any advice on my recursion code for Multiplication by Power, please say so, as I'm trying to learn this topic.

Thanks.
closed account (E3h7X9L8)
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
#include <iostream>

using namespace std;

void printStar(int var, int pow)
{
	if (var == 1)
    {
        for(int i = 0; i < pow; i++)
            cout << '*';
        cout << endl << pow <<" STARS !";
    }
    else
    {
        return printStar(var-1, pow *= 2); // pow = pow * 2
    }
}

int main()

{
    int p;
    cout << "2 to power "; cin >> p;
    printStar(p,2);
}
Last edited on
skwigelf, your original function looks good to me. How are you calling it? Maybe you're passing an uninitialized value to it.

To guard against problems, try adding this before line 3 (remove it once the program works when submitting it):
1
2
3
4
if (N<0) {
    cout << "bad value " << N << " in printPowerOfTwoStars()\n";
    return;
}
I appreciate the help, but unfortunately none of these worked. myprogramminglab does not really allow any creativity in solving the problems.. it must be a void function, and only receive ONE parameter int N, no for, do , do while loops, etc. It also does not like the if (N<0). Ah I will keep trying.
Show your full program, including main(). It looks like your recursive function should work fine.
here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
void printPowerOfTwoStars(int);

int main(){
printPowerOfTwoStars(Various test ints passed through here);
return 0;
}


void printPowerOfTwoStars(int N){
 	if (N == 0){
		cout << "*";
 	
	} 	

 	else  {
		printPowerOfTwoStars(N - 1);
 		printPowerOfTwoStars(N - 1);
	}

 }




here is the error I am getting:
CODELAB ANALYSIS: LOGICAL ERROR(S)

Remarks:
⇒ When executed, your code went into an infinite loop. Carefully check the condition in any loop you have written and carefully consider the body of the loop-- be sure that it will eventually make the loop condition false.


edit: To me, when looking at the code, I agree it does seem like it would go into an infinite loop, but when I try to compile it in VS 2013 it seems fine.
Last edited on
Try to use "if ( N<= 0)" instead of "if (N ==0)".
I wish that would have worked but when I do change it to <= it just says
CODELAB ANALYSIS: LOGICAL ERROR(S)

Remarks:
⇒ When executed, your code went into an infinite loop. Carefully check the condition in any loop you have written and carefully consider the body of the loop-- be sure that it will eventually make the loop condition false.

More Hints:
⇒ We think you might want to consider using: ==


And it seems like everything I change, it tells me to change it back. >.<
Last edited on
In your main program, do cout << '\n'; between each call to printPowerOfTwoStars().

printPowerOfTwoStars(Various test ints passed through here);
What sort of test values are you passing? How many stars will they print? How long do you think that will take? Hint: printPowerOfTwoStars(32) will attempt to print about 4 billion stars.

I suggest this for testing:
1
2
3
4
5
for (int i=0; i<8; ++i) {
    cout << i << ":\n";
    printPowerOfTwoStars(i);
    cout << '\n';
}
Unfortunately it is a homework type question on a site, I have no access to main only to my function that they tell me to create. And they limit your options and leave no room for creativity in the solutions (I guess that would make it easier for the site to grade it). Anyways it okay I am going to keep working on it, it's not worth any points actually but it is the only homework question out of 200+ that I could not figure out, so it is bugging me that I can't figure it out. I guess I need to just review recursion more too.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void printPowerOfTwoStars( int n )
{
    if( n == 0 ) std::cout << '*' ;

    else if( n > 0 )
    {
        printPowerOfTwoStars( n-1 ) ;
        printPowerOfTwoStars( n-1 ) ;
    }
}
That did not work either, when I changed the else to else if (n > 0) it still says

CODELAB ANALYSIS: LOGICAL ERROR(S)

Remarks:
⇒ When executed, your code went into an infinite loop. Carefully check the condition in any loop you have written and carefully consider the body of the loop-- be sure that it will eventually make the loop condition false
> CODELAB ANALYSIS: LOGICAL ERROR(S)

The LOGICAL ERROR(S) appear to be in the CODELAB ANALYSIS program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void printPowerOfTwoStars( int n )
{
    if( n == 0 ) std::cout << '*' ;

    else if( n > 0 )
    {
        printPowerOfTwoStars( n-1 ) ;
        printPowerOfTwoStars( n-1 ) ;
    }
}

int main()
{
    for( int i = 0 ; i < 7 ; ++i ) 
    {
        std::cout << i << "  " ;
        printPowerOfTwoStars(i) ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/441bc4b4e78e4c2d
Ah, they key was realizing that you don't control main(). The problem is that you aren't printing a newline at the end. To solve this you need a helper function that does all the work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
static void pp2(int N){
    
    if (N == 0){
        cout << "*";
    }
    
    else {
        printPowerOfTwoStars(N - 1);
        printPowerOfTwoStars(N - 1);
    }

}
void printPowerOfTwoStars(int N){
    pp2(N);
    cout << '\n';
}

If this doesn't work then do something to guard against negative inputs. Re-read the assignment to see if it says what to do (print one star? print a blank line? print nothing?)
Yes thank you. The problem actually just seems to be with the site I guess. Thanks everyone.
Topic archived. No new replies allowed.