Game Development Community

Extreme C++ Newbie needs help!

by Christopher · in General Discussion · 10/31/2004 (6:59 am) · 17 replies

I really need help with this problem guys, I know it is really simple to you but Im running into problems. I came here because I've been reading for a while, and you seem like a good croud.
Here is the problem:

Quote: If a human heart beats on average of once a second for 78 years, how many times does the heart beat in a lifetime? (Use a funtion for the calculation). Use 365.25 for days in a year. Rerun your program for a heart-rate of 75 beats per minute

Thats the problem, and this is the crazy stuff I've come up with, I know its wierd, and it may be petty for you to try and explain, so Im sorry.

#include <iostream.h>
#include <math.h>

double bpl(int a, int b, int c, int d, int e);


int main()
{
       int sec, minutes, hours, days, years;
       double beats;
       beats=bpl(sec, minutes, hours, days, years);
       cout<<"There are "<<beats<<" beats per lifetime\n";
       return 0;
}

double bpl(int a, int b, int c, int d, int e)
{
       return (60*60*24*365.25*78);
}

and I keep getting a return of 2.46149e+9, and apparently I'm not supposed to get that.

About the author

Recent Threads


#1
10/31/2004 (7:37 am)
Christopher,

The value being return is correct but it seems that the fact that it's being returned in scientific-notation may be confusing you?

60 * 60 * 24 * 365.25 * 78 = 2461492800 which equates to approximately 2.46149e+9.

If you didn't already know, this notation specifies a real number and the "e+9" notation specifies what to do with the decimal-point.

"e+9" means shift the point right, 9 places e.g. 2461490000. I say this is approximate because as you can see, some rounding-error has occurred.
"e-4" would mean shift the point left, 4 places e.g. 1.23456e-4 would equate to 0.000123456 approximately.

Essentially, the number on the left is the coefficient and the number on the right is the exponent in the form of 10(n) so you get coeff*10(exp).

You can find many references on the internet to this. I would suggest using an alternative to COUT. Also, don't use double-precision unless you've got a specific reason for so much accuracy. Try using float and outputting with something like printf("%f", myFloatNumber)
float beats = 60*60*24*365.25f*78;
printf("There are %f beats per lifetime\n", beats);
Note the use of 365.25f to indicate float precision. This precision is accurate enough for almost any task apart from extremely complex scientific analysis.

Hope this helps,

- Melv.
#2
10/31/2004 (7:39 am)
Your stack (is that the correct name?) just overflowed.
I ran into this when coding a scrambler a few weeks ago.

What you can do is using unsigned values instead, if your value never will go below 0. That way you can get higher numbers that are positive, but you still have a limit.

I never really understood how to work around this problem totally and no one I asked knew either, so I hacked around it really dirty and used several integers instead of one, and added these into eachother.

I'm not a C++ programmer, so someone else might wanna answer in a more "programmer" way than all my rambling :) Good luck.

Edit: Melv to the rescue while I was typing, good to go Melv :D

Second Edit: Just realised it was NOT an overflow. Disregard my post please.
#3
10/31/2004 (7:41 am)
:) :)
#4
10/31/2004 (7:48 am)
If you want cout to print in fixed-point rather than scientific notation, you just have to use this line of code before you try to print the number.
cout.setf (ios_base::fixed, ios_base::floatfield);
#5
10/31/2004 (7:53 am)
The number may be right, but I don't think your code is for what you hope to acheive.

Nowhere are you passing anything useful, I'll try to explain. Your function bpl is taking some parameters, a to e, judging by the previous code, I would assume you mean these to be seconds,minutes...years. You aren't actually using them in the function anywhere. In adition to this, you have hardcoded the beats per second in that function, meaning you have to change the function when you want to calculate with different values. Here is what I beleive you were trying to do

#include <iostream.h>
#include <math.h>

const float DAYS_PER_YEAR=365.25f;
const int HOURS_PER_DAY=12;
const int MINUTES_PER_HOUR=60;
const int SECONDS_PER_MINUTE=60;

float bpl(int seconds, int minutes, int hours, int days, int years, float beatsPerSecond);

int main()
{
       float beatsPerSecond=1;
       int lifetime=78;
       float beatPerLifetime=bpl(0,0,0,0,lifetime,beatsPerSecond);

       cout<<"There are "<<beatsPerLifetime<<" beats per lifetime\n";

       return 0;

}


float bpl(int seconds, int minutes, int hours, int days, int years, float beatsPerSecond);
{
       float totalSeconds=0;
       totalSeconds+=seconds;
       totalSeconds+=minutes*SECONDS_PER_MINUTE;
       totalSeconds+=hours*SECONDS_PER_MINUTE*MINUTES_PER_HOUR;
       totalSeconds+=days*SECONDS_PER_MINUTE*MINUTES_PER_HOUR*HOURS_PER_DAY;
       totalSeconds+=years*SECONDS_PER_MINUTE*MINUTES_PER_HOUR*HOURS_PER_DAY*DAYS_PER_YEAR;
       return totalSeconds*beatsPerSecond);
}

give that a good read, and ask if theres anything you dont understand.

Edit: fixed the code ....
#6
10/31/2004 (11:13 am)
Wow, you guys are good, right now I'm trying to digest what is in your posts.

Hmmm, sorry for asking this, but Im not even quite sure what a float is compared to a double. (sorry)
Also, Im useing Codewarrior as my IDE, and trying to run the program you just setup there gives me all kinds of errors

"Undefined identifier"

syntax errrors

So if you could help me out here that would be great.
#7
10/31/2004 (11:25 am)
Christopher,

Here's a reference to give you an idea of one of the standards...

FP IEEE 754

Essentially, double has a higher precision but is slower to process, takes more memory and others.

- Melv.
#8
10/31/2004 (12:38 pm)
Could someone break down this piece of code and tell me what each part is doing?

cout.setf (ios_base::fixed, ios_base::floatfield)
#9
10/31/2004 (12:49 pm)
A quick search on the net reveals...

setf

Hope this helps,

- Melv.
#10
10/31/2004 (1:31 pm)
Sorry Christopher, you would indeed have gotten an Unidentified identifier trying to compile it. Should work now.
#11
10/31/2004 (1:55 pm)
Christopher, the setf is probably slightly above the head of a newbie to C++. I suggested it because it was the same solution provided by Bjarne Stroustrup ["the creator of C++," the cover of his book explains]. Basically, the program is setting a certain property of cout, the "floatfield", which deals with floating-point numbers. In C++, floating-point means "float" and "double". And you're setting the way cout handles these variables--as "fixed". The "ios_base::" prefix means that both "fixed" and "floatfield" are static member variables in the class "ios_base". That last line may be a bit above your head, but if you stick with C++, you *will* eventually understand exactly what I'm talking about.

@William: Looks like you've been using a lot of Java lately. Try changing "final" to "const".
#12
10/31/2004 (2:14 pm)
You're very right Mike :) Thanks for the help, I had to think hard to remember how to write C++ for that post.
#13
10/31/2004 (2:39 pm)
Thanks everyone your awsome!
#14
11/01/2004 (2:44 pm)
Hey guys, I figured out a way to write it so that it functions. Compile it and see.

#include <iostream.h>
#include <math.h>

float bpl(int a, int b);


int main()
{
	   cout.setf (ios_base::fixed, ios_base::floatfield);
       int beats, years;
       float beatp;
       cout<<"How many times does your heart beat per second?\n";
       cin>>beats;
       cout<<"How many years in your life?\n";
       cin>>years;
 
       beatp=bpl(beats, years);
       cout<<"There are "<<beatp<<" beats in your lifetime\n";
       return 0;
}

float bpl(int a, int b)
{
       return (a*60*24*365.25*b);
}

Thanks again
#15
11/01/2004 (11:50 pm)
60*24*365.25*b gives you minutes per lifetime, not seconds. a is beats per second, so you need to multiply by 60 again to get beats per minute. Remember that in your input, you are requesting beats per second. A heartrate of 60 gives 1 beat per second.

a*60*60*24*365.25*b will return beats per lifetime.

Try using some descriptive names for your arguments, instead of a and b. It isn't obvious at all what the arguments represent when they aren't descriptive, and have no comments.

But well done for getting past the errors in my code :)
#16
11/02/2004 (2:36 am)
@William & Chris

You need to change your code to

#include
#include

float bpl(int a, int b);
int main()
{
cout.setf (ios_base::fixed, ios_base::floatfield);
int beats, years;
float beatp;c
cout<<"How many times does your heart beat per minute?\n";
cin>>beats;
cout<<"How many years in your life?\n";
cin>>years;
beatp=bpl(beats, years);
cout<<"There are "<
return 0;
}

float bpl(int a, int b)
{
return (a*60*24*365.25*b);
}

this actually corrects the code rathere than adding another multiply by 60, which would break the program when part b of the question is attempted.
#17
11/02/2004 (2:50 am)
You are right in saying the extra *60 is superfluous, but it is not incorrect. 75 bpm is simply 75/60 bps. Hence the float type for BeatsPerSecond. Shouldn't make any real difference though.