Unable to create a simple function

This is a discussion on Unable to create a simple function within the C Programming forums, part of the General Programming Boards category; Hi Friends, This is my first message on this forum. I am trying to learn C language. Recently i was ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Unable to create a simple function

    Hi Friends,

    This is my first message on this forum. I am trying to learn C language. Recently i was trying to find a solution for a
    problem..... To be more specific I was trying to make C-Function for a proble, lets say If a user inputs 2 different times of a day & a band period, we want to find whether Action_Start_Time lies in range of Present_Time+band_Period, let me explain with the help of an example. lets say he enters:-

    1.)Present_Time :- 00:30
    2.)Action_Start_Time :- 00:29
    3.)band_Period :- 90 minutes


    we need to know whether Action_Start_Time(00:29) lies anywhere between 00:30(Present_Time) -- to -- 02:00 (Present_Time

    +band_Period)

    Now, in this example the answer should be "No".



    Now lets take one more example, lets say he enters:-

    1.)Present_Time :- 23:30
    2.)Action_Start_Time :- 00:05
    3.)band_Period :- 90 minutes


    now here we need to know whether Action_Start_Time(00:05) lies anywhere between 23:30(Present_Time) -- to -- 01:00 (Present_Time +band_Period)

    Now, in this example the answer should be "Yes".

    I hope I'm clear enough. Thanks a lot for giving me your time. :-). I have to work in 24 hours mode. Not with 12am/pm mode.

    With Best Regards,
    garg

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't over think it. It's just a simple carry over, after Midnight.

    if(2400 hrs is less than (present time- [which is really a start time] + Period), then carry over the difference to the next day. If the target date falls anywhere in there, you've got a "Yes".

    If you call "Present_Time", "start time", and call "band_Period", "ending time", and think of "Action Start Time" as "target time", it might help you see it.

    And welcome to the forum.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Thanks a lot for replying "Adak". First of all I am sorry for posting wrong logic. What I actually need goes like this:-

    Lets say we have to start a motor at a particular time, in case there was no power supply at motor start time , & the power resumes after sometime we now need to check whether "Motor start time" lies between (Present time + band period) or has the period got over. By "Present_Time" I mean to say the time actually in the clock. By "band period" i mean to say the maximum time allowed beyond the Motor_Start_Time to start the motor. Your method works great for below 3 conditions(Motorstart time + band_period - Present time) but for last one it fails.

    1st For example, lets say:-

    1.)Present_Time :- 00:10 (when the power resumed)
    2.)Motor_Start_Time :- 23:30 (Lets say power was not present at this time)
    3.)band_Period :- 90 minutes

    In this example motor can start if power resumes upto 0100 (23:30 + 90 minutes) the answer should be "Start the Motor".



    2nd For example, lets say:-

    1.)Present_Time :- 23:00 (In this case still their is time to start the motor)
    2.)Motor_Start_Time :- 00:10
    3.)band_Period :- 90 minutes

    In this example the answer should be "do not start the Motor".



    3rd For example, lets say:-

    1.)Present_Time :- 03:00 (when the power resumed)
    2.)Motor_Start_Time :- 23:30
    3.)band_Period :- 90 minutes

    Now as the period which was allowed to start the motor has got over the motor shouldn't start so in this example the answer should be "do not start the Motor".



    Now In 4th case, lets say:-

    1.)Present_Time :- 03:00 (when the power resumed)
    2.)Motor_Start_Time was:- 21:00
    3.)band_Period :- 90 minutes

    here maximum time that was allowed was uptil 2230 hrs but power resumed at 0300 hrs so the motor shouldn't start but formula gives reverse result, so should there be some other second condition included?

    Thanks a lot once again,

    With best regards,
    garg

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, I wrote you up a rather long reply, and the forum said "Your message can't be posted because you've re-registered since loading that page. Click the back arrow of your browser and reload the page."

    Which naturally erased my post.

    In a thumbnail, I believe some of these factors are going to have to be worked out on your program, after it's up and running correctly with most of the situations.

    This is my suggestion:

    I would try to change everything into integers, instead of "time" or floats, as early in the process, as possible.

    Solving it may be as simple as using a for loop - starting with the motor_start_time, and ending with the motor_start time + band_period (mst + bp)
    Code:
    startFlag = 0;
    for(i = mst, midnightFlag = 0; i <= (mst+bp); i++) {
      /* if i == 2400, then it's a midnight X over event. For this calculation, 
           increment present_time by 2400;
      */
      if(i == 2400) {
        present_time += 2400;
        midnightFlag = 1;
    
      }
      if(i == present_time)  //which is now an integer
        startFlag = 1;
    }
    
    if(midnightFlag) 
      present_time -= 2400;
    
    if(startFlag == 1)
      //start the motor
    Last edited by Adak; 02-27-2010 at 05:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 2
    Last Post: 04-22-2009, 03:35 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21