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!
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.
#include <iostream>
usingnamespace 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.
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. >.<
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.
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
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:
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?)