943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3211
  • C++ RSS
Aug 18th, 2008
0

About the modulo operator

Expand Post »
I'm working on some C++ excercies in my book and now I'm supposed to write a program that converats a number of seconds into days, hours, minutes and seconds with the help of arithmetic operators and integer and float variables. Nothing to fancy that is.

It ended up looking something like this:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. long Sec;
  7. const int HouPDay = 24;
  8. const int MinPHou = 60;
  9. const int SecPMin = 60;
  10.  
  11. cout << "Type in the number of seconds: ";
  12. cin >> Sec;
  13.  
  14. cout << "\n" << Sec << " seconds = "
  15. << Sec / (HouPDay * MinPTim * SecPMin) << " days, ";
  16. //Calculates and prints the number of whole days that Sek make out
  17. cout << (Sec % (HouPDay * MinPHou * SecPMin))/(MinPHou * SecPMin)
  18. //Calculates and prints the number of whole hours that the remainder of the previous argument make out
  19. << " hours, ";
  20. cout << ((Sec % (HouPDay * MinPHou * SecPMin))%(MinPHou * SecPMin))/SecPMin
  21. // Calculates and prints the number of whole minutes the the remainder of the previous argument make out
  22. << " minutes and ";
  23. cout << ((Sec % (HouPDay * MinPTim * SecPMin))%(MinPTim * SecPMin))%SecPMin
  24. // Lastly; calculates and prints the number of whole seconds that're still left.
  25. << " seconds.\n";
  26.  
  27. }

It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.

And yea, the variable containing the number of seconds had to be a long integer but if the code can be improved if that condition is overseen please tell. =)
Last edited by Rawfel; Aug 18th, 2008 at 10:55 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rawfel is offline Offline
2 posts
since Aug 2008
Aug 18th, 2008
0

Re: About the modulo operator

>>She will be met in the afterlife by her husband, Raymond, her son, Paul Jr., and daughter, Ruby.


Yes -- use the mod operator. such as seconds = secs % 60.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 18th, 2008
0

Re: About the modulo operator

Ah! Thanks, you just made me realize how the modulo operator works! I had some serious trouble grasping this yesterday night, well that might've been 'couse I tried to around 3 AM, but still, thanks!

So, for example:

Quote ...
cout << (Sec % (60 *60)) / 60
Would equal to the amount of whole minutes Sec make out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rawfel is offline Offline
2 posts
since Aug 2008
Aug 18th, 2008
0

Re: About the modulo operator

Quote originally posted by Rawfel ...
It feels like it's very inefficient coding considering the program has to repeat the same arithemtic argument for every "cout" and that's why I ended up posting this, I want to know whether it can improved without getting to advanced.
If you use the same expression more than once, you should consider caching it into a variable.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 18th, 2008
0

Re: About the modulo operator

try this, we know that
1 hr = 60 mins
1 min = 60 secs


so

sec = x

mins = int(sec / 60)
sec = sec % 60

hrs = int(mins/60)
mins = min % 60

days = int(hrs/24)
hrs = hrs%24

hence you get

days hrs mins and sec

Regards
Shaik Akthar
Last edited by aktharshaik; Aug 18th, 2008 at 12:02 pm.
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008
Aug 18th, 2008
0

Re: About the modulo operator

>>mins = int(sec / 60)
Its not necessary to use the typecast. sec/60 always returns an int when sec is an int.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can't use Strings :'(
Next Thread in C++ Forum Timeline: Understanding binary data and algorithms--





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC