Can somebody help me understand what does this function do? And also if there are any logical or syntactic errors in the function.

I was told that there are 2 bugs in the code. I can only think of 1 which is below:

1. In the first while loop in substr function the starting should be from 0 and not 1.

Can somebody correct me if I am wrong?

Much Obliged.

unsigned long normalise(unsigned long input_time)
{
 
      bool finished;
 
      // This produces a formatted time string like:
      // Fri_Nov_25_18:22:48_1986
      string str_time = format_time( input_time );
 
      while( str_time.substr(1,3) != "Sun")
      {
            input_time -= 24*60*60;
            str_time = format_time( input_time );
      }
 
      while( str_time.substr(11,2) != "00" )
      {
            input_time -= 60*60;
            str_time = format_time( input_time );
      }
 
      while( str_time.substr(14,2) != "00")
      {
            str_time = format_time( input_time );
            input_time -= 60;
      }
 
      while( str_time.substr(17,2) != "00")
      {
            input_time -= 1;
            str_time = format_time( input_time );
      }
 
      return input_time;
}

i will check it but also you should try running it a few times to see what happens.

EDIT:

also please include any headers and other functions you may have defined :)

I have not written the function. I was asked this question by a friend which he had got in an interview. These days I am trying to solve as many questions as possible as I learn more from it. If the question is not clear then I can re-send it with exact words.

Many Thanks for your time.

Much obliged.

Regards,
ad_rulz

found the second bug

while( str_time.substr(14,2) != "00")
{           
    str_time = format_time( input_time );
    input_time -= 60;
}

the reason this is incorrect is because the output time is formatted before the new time has been calculated thus the str_time format time will always be incorrect. the loop would continually get stuck forever.

Thank you very much. Appreciated.

I think the function will reach to zero time on sunday. That is what it does. It does not change

But can you please suggest how can I make this function more efficient as it is not very efficient with the use of too many while loops.

Thank you very much. Appreciated.

I think the function will reach to zero time on sunday. That is what it does. It does not change

But can you please suggest how can I make this function more efficient as it is not very efficient with the use of too many while loops.

the most efficient method is going to entail determining the difference between what you have and what zero is and then subtracting by that ammount instead of continually removing one unit at a time.

so if you have 42 seconds instead of just removing one remove 42

if you have 39 minutes instead of just removing one minute at a time remove all 39.

this completely eliminates the while statements.

Many thanks.

Have a great day.

Regards,
Ad_rulz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.