Forumchem - Forum with AI(ALICE BOT & HAL9000) and TTS

More dificult for us, more easy for you
It is currently Fri Apr 19, 2024 1:03 pm

All times are UTC





Post new topic Reply to topic  Page 1 of 1
 [ 4 posts ] 
Author Message
 Post subject: C++ recursively do exponent of a number?
PostPosted: Thu Apr 03, 2014 12:18 am 
Offline
User avatar

Joined: Sun Oct 18, 2009 1:04 pm
Posts: 6
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.

This program got me really confused. This is what I have so far:
void printPowerOfTwoStars(int x)
if (x > 0)
cout << "*";
printPowerOfTwoStars (x);
cout << "*";



Top
 Profile      
 
 Post subject: C++ recursively do exponent of a number?
PostPosted: Wed Feb 15, 2017 1:35 pm 
Offline
User avatar

Joined: Thu Apr 02, 2009 6:58 am
Posts: 1367
Heres the sleekest answer.

void printPowerOfTwoStars(int x)
if (x == 0)
cout << "*";

else
printPowerOfTwoStars (x-1);
printPowerOfTwoStars (x-1);



Top
 Profile      
 
 Post subject: C++ recursively do exponent of a number?
PostPosted: Wed Feb 22, 2017 11:02 am 
Online
User avatar

Joined: Thu Apr 02, 2009 2:26 pm
Posts: 1367
First, something simple to keep in mind is that, when you write recurrent functions, if you write the call to itself at the end, the compiler can optimize it, so it doesnt eat more and more stack with each call. Its called tail-call optimization/elimination.

Second, you need to put 2^n, not 2*n, also, the order in which you print the asterisks doesnt matter.

If I could use two functions, I would do that, it i obvious, but otherwise something like this should do:


static void printPowerOfTwoStars(int x)

if (x > 0)
x = -(1 << x);

putchar(*);

if (++x < 0)
printPowerOfTwoStars(x);


Of course there are many other way, but thi one i pretty clean :). I guess it doesnt need much explanation, but just in case: it use negative values to encode iterations, and the (1 << n) is a way to calculate 2^n, using binary arithmetic.


Top
 Profile      
 
 Post subject: C++ recursively do exponent of a number?
PostPosted: Tue Feb 28, 2017 2:45 pm 
Online
User avatar

Joined: Thu Apr 02, 2009 9:54 pm
Posts: 1353
Id say that you should first calculate the number of star and then use that for the upper limit of your print loop:

void printPowerOfTwoStars(int x)


int stars =1;
for( int k=0; k
stars *= 2;

// cout<<"x "<
for( int k=0; k
cout<<"*";

cout<<"\\n"<

int main()


for( int k=0; k<=4; k++ )

cout<<"k: "< printPowerOfTwoStar(k);





Top
 Profile      
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  Page 1 of 1
 [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: vawceix and 16 guests


 
Search for:
 
Jump to:  

cron
Click me:
Powered by phpBB © 2000, 2002, 2005, 2007, 2008, 2009 phpBB Group
Chronicles phpBB3 theme by Jakob Persson. Stone textures by Patty Herford.
With special thanks to RuneVillage

This site have 4 type of tecnology in order to convert text to speech. By default you use the vozme tecnology. In order to know the other you need to sign for.


- Privacy Policy -