-
C++ Help
Hello, im pretty new to C++ but i know a bit about it.
I have a formula that i want to turn into c++ code, What the formula does is outputs a lvl and its amount of experience.
The formula is: 
This is my attempt at it.
Code:
#include <iostream>
#include <math.h>
using namespace std;
void wait()
{
cout << "\nPress Enter to Continue\n";
cin.ignore(1);
}
int main()
{
int xp = 83;
for ( float lvl = 2; lvl <= 99; lvl++)
{
xp += floor( floor(lvl + 300 * pow(2, lvl / 7 )) / 4);
cout << "Level: " << lvl << "\tXP: " << xp << endl;
}
wait();
return 0;
}
The probblem is that it doesnt output the xp correctly.
lvl 2 outputs xp = 174
lvl 3 outputs xp = 275, when its suppose to output 276.
This formula ends at level 99 and when its reachers that level, the output is off by 39.
lvl 99 outputs xp = 13034394, when its suppose to output 13,034,431
So im wondering if anyone could help me out with this. Would be great to get it working properly.
The following link has what its suppose to look like, also has the formula in some other languages http://www.mirwoj.opus.chelm.pl/rs/index.php?pid=b04
Minor59er
Last edited by minor59er; 01-14-2007 at 09:44 AM.
-
first using [ IMG ] code will be better :

now following the formula u must :
1- start by 1; for(lvl=1 ..
2- u must calculate the summation first in a variable temp say [not xp], in the loop don't divide by 4 but after the loop or in the display result //u may not initialize xp by 83 but 0 .
3- the first floor is around the whole summation ..
try this :
Code:
int main()
{
int xp = 0;
for ( float lvl = 1; lvl <= 99; lvl++)
{
xp += floor( lvl + 300 * pow(2, lvl / 7 ));
cout << "Level: " << lvl << "\tXP: " <<floor( xp /4 )<< endl;
}
system("PAUSE");
return 0;
}
Last edited by Amahdy; 01-14-2007 at 03:29 PM.
-
Amahdy is right (he's very strong in math). You were making a sum of the whole formula, but if you look the formula closely, the sum regards only the upper part of the fraction.
-
Thanks very much for your help, i tried it out on my graphics calc, and it worked fine, just got to get use to getting things to work in C++, thanks so much again.
Will
-
Ok, one more question, when its level 74+, the numbers start to go 1.09628e+006, 1.21042e+006, 1.33644e+006, 1.47558e+006. Any idead to make them look like 1,096,278, 1,210,421, 1,336,443?
Thanks again :)
-
thanks mikkus I'm nothing near u , and happy to see u again :)
well u need the setiosflags(fixed) and because the return from floor() is double u may need to setprecision(0) .. all this could be done in iomanip.h ;
to explain more;
include this :
#include <iomanip>
and the output line must be updated to this :
cout << "Level: " << lvl << "\tXP: " <<setiosflags(ios::fixed)<<setprecision(0)<<floor( xp /4 )<< endl;
about the comma after every three digits u may use also :
setiosflags(ios::viewpoint) but it's not in the output u sent before .
-
Another issue: you should replace float with double or long double for higher accuracy. Furthermore, xp/4 returns an integer because the two operands are of type int. It's beter to use xp/4. to ensure that the result is of type double.
Danny Kalev
-
Thanks everyone for all your help, if i get stuck on anything else, ill come back here for help :)
Will
-
Yup Danny I think also the simple int instead of float will work fine as we only need to count from 1 to say 200 or a normal countable range !
I haven't take attention in this before .
-
I'm not concerned about the small number of 200, float can represent this value. However, I'm more worried about the truncation and other inaccuracy problems that float causes. Besides, float is never a good choice. Except the case of special hardware of embedded systems, the default floating point type should be double. Not a big issue here, but it's never too late to adopt good programming practices;)
Danny Kalev
-
There are a few other good places for floats -- we capture a large data log using a binary write of a float array to keep the logs half sized. Same thing can be used if you are having page faults/memory issues due to a LOT of doubles.
Similar Threads
-
Replies: 6
Last Post: 12-11-2002, 05:08 AM
-
By Sangeetha in forum .NET
Replies: 1
Last Post: 04-18-2002, 07:36 AM
-
By Simon Jones in forum Java
Replies: 0
Last Post: 10-17-2001, 05:44 AM
-
By kennie hall in forum Java
Replies: 0
Last Post: 09-27-2001, 09:58 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks