math kills braincells

K.I.L.E.R

Retarded moron
Veteran
If age is > or = 40:

add 1/8 on jacket size for every 10th year starting from 40.

IE:
if you are 40:
jacketSize = ((1/8 )+jacketSize)*jacketSize

if you are 50:
(add another 1/8 of jacketSize to jacketSize)
jacketSize = (((1/8 )*2)+jacketSize)*jacketSize

etc... for every 10th year.

Simple when doing it on paper or in your head but when using C++...

I tried using a simple shortcut with the switch statement with formula above but the teacher wants no shortcuts and it has to work with any age. Even if it is a 50,000,000,000,000 year old man/women. He will specifically test mine if it works with an insanely old women.

I've been known to take shortcuts. ;)

Question:
Jacket size = (height * weight) / 288 then adjusted by adding 1/8 of an inch for each 10 years over age 30. The adjustments only take place after a full 10 years. So there is no adjustment for ages 30-39 but 1/8 of an inch is added for age 40.

Let me re-iterate that it's easy on paper but a little more complicated in C++. I'm still thinking. If anyone can drop me a hint or 2 I would be grateful.

So far (I think it's right):
Code:
	jacketSize = (weight*height)/288.0;

	if(age >= 40)
	{
		while(age > 0)
		{
			jacketSizeTemp = age-10;
			count++;
		}
	}

	jacketSize += (age/10)*((1/8)*count);
 
Why don't you use integer division?
Or if you must use floating point numbers, use "floor."
 
Not sure if I read the question correctly, seems like
if jacket size =x at age 41.. then its x at age 49 or is it perpetually multiplied every year by the multiplier (thats further changed every 10 years)?

(in pseudocode)

for i = 1 to infinity (or whenever you break it)
if (( i + 30 ) mod 10 == 0) then jacketsize += 1/8 inch (put it in good units)

if its the other way, then its

counter = 0
for ... (same)
if (same)... then counter += 1/8
jacketsize += counter


Make sure you are in the right units, but that should do it
 
Isn't your maths wrong? The way I read it, it should look like this:

IE:
if you are 40:
jacketSize = jacketsize + jacketsize/8 = 9/8*jacketsize

if you are 50:
(add another 1/8 of jacketSize to jacketSize)
jacketSize = jacketsize + jacketsize/8 + jacketsize/8 = 10/8*jacketsize

if you are 10n, where n>=4:

jacketsize = jacketsize + jacketsize*(n-4)/8 = jacketsize(8+n-4)/8

You can get n using the following:

n = age\10

This is assuming I understood your problem correctly. My way, 100 year woman would have a jacketsize of: jacketsize(8+(100-40)\10)/8 = jacketsize*14/8

edit:
Actually, I'm not sure C++ has the "\" operator. But you can use the modulus operator "%". Something like this:

r=age%10 Returns the remainder of dividing by 10.

age=age-r Makes age a multiple of 10.

n = age/10 Done :)
 
This right?

Code:
	//1/8 inches added onto jacket size for every 10 years over 40
	double jacketSize;
	double jacketSizeTemp=0.0;
	int ageTemp=0;

	jacketSize = (weight*height)/288.0;

	age /= 10;
	ageTemp = age % 10;

	while(ageTemp == 0 && age > 39)
	{
		jacketSize += 1/8;
		age -= 10;
	}

	return jacketSize; //done somewhat. Don't have test data to correct!
 
Your code is not correct. You should do mod before division.
Anyway, the integer division is just like "\" for positive integers, so there's no need to use mod to correct the result.

I think it should be like this:

Code:
   age = (age / 10) - 3;
   jackSize += 1/8 * age;

It's inefficient to use a loop for this. That reminds me of a function:

Code:
bool isOdd(int n)
{
    if(n == 0) return false;
    else if(n == 1) return true;
    else return isOdd(n - 2);
}
 
Um, no. I think it should be more like this:

Code:
 //1/8 inches added onto jacket size for every 10 years over 40 
 double jacketSize; 
 double ageTemp; 

 jacketSize = weight*height/288.0; 

 ageTemp = (age - age%10)/10.0; 

 jacketSize = jacketSize*((ageTemp-3.0)/8.0 +  1.0)

If I understand the problem right, looping is not required. In fact, it's quite an ugly, brute-force way of solving it. :D
 
Yes, but you can just do this

Code:
ageTemp = age / 10;

instead of

Code:
ageTemp = (age - age % 10) / 10;

Both generate the same results in C/C++, as long as age >= 0.
 
I may be being stupid here, but wouldn't the ageTemp have to be and integer for it to be truncated simply by dividing by 10? Not that there's any reason why it couldn't be an integer that I can see...
 
Wouldn't I need a loop if the person is 100 years old?

For every 10 years after 30 1/8 is added to an inch on the jacket size.

So if they are 100 it would be:

var = 70*(1/8 ) (which would make 8.75 inches)

jacketSize = jacketSize + var

I would take 10 away from the age until they are 30 and a counter to keep count how many times I needed to minus 10 from the age untill I hit 30 then I can multiply the counter by 1/8 and display the result.

Would that get me the correct answer?

I think that's what I'm doing now?
 
Nathan said:
I may be being stupid here, but wouldn't the ageTemp have to be and integer for it to be truncated simply by dividing by 10? Not that there's any reason why it couldn't be an integer that I can see...

Yes, that's why I said "integer dividision."
In C/C++, integer division produce integers, so there's no need to truncate the result. If you use floating point numbers (eg. for the 50,000,000,000,000 years old case), you'll have to use "floor" function to truncate the result (or just convert it to an integer). However, you have to be careful about the precision problem. By the way, C/C++ can't do mod on a floating point number, you have to use "fmod" function.


K.I.L.E.R:

It's not your program is wrong, it's just too inefficient. Your program won't be able to handle your "50,000,000,000,000 year old man/women" case: it may take days on a fast PC to complete.
 
Isn't it 1/8 for every 10 years, so you have

var = 7*(1/8 ) for a 100 year old.

If the person is 100

ageTemp = (age-30)/10 = 7 which is fine as that's an integer value. If they were 101 you would have:

ageTemp = 71/10 = 7.1 which you're not allowed to use because it is a floating point number. However, if you convert ageTemp to an integer, it will truncate to 7 - giving you the integer value that you need. Its basically a more elegant way of doing what I proposed earlier. ;) (listen to pcchen, he's smart)

The way you've proposed will work - it's just a bit icky. But hey, Intel makes 3GHz processors so Microsoft can make bloatware, so we may as well too. :p
 
Right, I thought the point was to make a loop in the first place (kinda a tutorial in loops), but yea if you are allowed to input the age, its directly solvable.
 
Back
Top