views:

243

answers:

3

I was hoping someone that is good with math and loops could help me out. I'm writing a program in Objective C where I need to come up with a way to do a cycle. If you don't know Objective C I would appreciate any help in pseudo code just to help me figure this out.

What I need is a scale that is based on two dates. I know this will be some sort of loop but not sure how to figure it out.

For instance, lets say that the first date is 5/25/1976 and the second date is 9/25/2009. Every 25 days there will be a "peak" so it's value will be 100. If I divide 23 in half I get 12 (rounded) so it would be the opposite or "valley" so it's numerical value would be 0. In other words on the 23rd day it would be at 100 but then on the 24th day it would start going back down and then bottom out 12 days later and then start the cycle back up and top out again at 23 days.

What I need to be able to do is find the numerical value for any given date in between any two given dates.

Thanks for any help you can offer!

+3  A: 

value = 100*cos(2*pi*(numDays/25))

Or something like that.

Dan Lorenc
This could be what i'm looking for. Let me give it a try.
Xcoder
I'm getting errors. This is what i'm trying: -(int *)getNumber:(int *)firstDate{ int *answer = 100*cos(2*pi*(firstDate/23)); return answer;}I'm getting the errors: invalid oparands to binary /andincompatible types in initialization
Xcoder
You probably want -(int)getNumber:(int)firstDate { int answer = 100*cos(2*pi*(firstDate/23)); return answer; }. Using * after int makes it a pointer, and you don't need and int pointer here.
Jay Conrod
I'm very close it just keeps giving me 0 when I try float pNumber = 100*cos(2 * M_PI * (days/23));
Xcoder
Finally got this to work. I just had to add a 0 at the end of 25 and 2. Thanks!
Xcoder
+1  A: 
  • Calculate the difference in days (optionally in fractional days too) between the starting point and the day you want the value for.
  • Divide by the cycle period (could be 23 or 25 according to the question).
  • Take the fractional part.
  • Apply the correct periodic function - for example, either sin() or cos(), appropriately scaled for the trigonometric functions (multiply the fraction by 2π).
  • You could simulate the shape by values out of a table describing the values indexed on days into the period (so you would use waveform[Δt mod period] to determine the value).
Jonathan Leffler
A: 

The NSDate class has a method timeIntervalSinceDate that will give you then number of seconds between two dates. You could calculate the number of days between two dates like this:

- (double) daysBetweenStart:(NSDate*)start end:(NSDate*)end
{
    return [start timeIntervalSinceDate:end] / 86400.0;    // seconds in a day
}

You could use this to compute a step function based on that:

- (double) someDescriptiveFunctionName:(NSDate*)date fromDate:(NSDate*)start
{
    double days = [self daysBetweenStart:start end:date];
    if ((int) days % 23 == 0)
        return 100.0;
    else
        return 0.0;
}

This function returns 100.0 if the given date is between 23 and 24 days from the start, and 0.0 otherwise. You could substitute 23 for whatever period you like. I'm not sure if this is what you wanted, so clarify your question if it wasn't.

Disclaimer: This is Cocoa. Hopefully it's the same as iPhone Cocoa?

Jay Conrod
yes, this is very close. What I'm trying to get a number for any day. So, 24 days would return, 99%, 23 would return 95% etc... down to the valley which would be 0%. Thats why I thought it would need to be a loop. Make sense?
Xcoder
Yeah,,, I tried this. It always gives me 100 or 0. I need the numbers in-between also.
Xcoder