Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 6th, 2005, 6:29 PM   #1
stopplayingamez
Newbie
 
Join Date: Mar 2005
Posts: 11
Rep Power: 0 stopplayingamez is on a distinguished road
FActorial Conv. Progr

Hey guys I'm wondering how I could convert this to one that computes 3^n - 2^n....that is 3 to the power n minus 2 to the power n. I understand you can't use ^ for power in c++ is that right..I'm coming from a Java background this is kinda new to me. So i'm wondering how to convert this factorial to the function above....


#include<iostream>
using namespace std;

template <int n> struct Fac // general case n! = n*(n-1)!
{
	enum { result = n * Fac<n-1>::result }; // constant
};
template <> struct Fac<0> // base case 0! = 1
{
enum { result = 1 }; // constant
};
int main()
{
// Fac<c>::result is now a compile-time constant!
cout << Fac<5>::result << endl; // = 5! = 120
cout << Fac<10>::result << endl; // = 10! = 3628800
cout << Fac<0>::result << endl; // = 0! = 1
}
stopplayingamez is offline   Reply With Quote
Old Apr 6th, 2005, 7:02 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
To find the power of something, you use the pow function, found in math.h. Here's an example: http://www.cplusplus.com/ref/cmath/pow.html.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 6th, 2005, 7:32 PM   #3
stopplayingamez
Newbie
 
Join Date: Mar 2005
Posts: 11
Rep Power: 0 stopplayingamez is on a distinguished road
#include<iostream>
using namespace std;

template <int n> struct Fac // general case n! = n*(n-1)!
{
	enum { result = 3 ^ n - 2 ^ n}; // constant
};

int main()
{
// Fac<c>::result is now a compile-time constant!
cout << Fac<5>::result << endl; 
cout << Fac<10>::result << endl; 
cout << Fac<0>::result << endl; 
}


OK what now??
stopplayingamez is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC