About the modulo operator

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 2
Reputation: Rawfel is an unknown quantity at this point 
Solved Threads: 0
Rawfel Rawfel is offline Offline
Newbie Poster

About the modulo operator

 
0
  #1
Aug 18th, 2008
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: About the modulo operator

 
0
  #2
Aug 18th, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: Rawfel is an unknown quantity at this point 
Solved Threads: 0
Rawfel Rawfel is offline Offline
Newbie Poster

Re: About the modulo operator

 
0
  #3
Aug 18th, 2008
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:

cout << (Sec % (60 *60)) / 60
Would equal to the amount of whole minutes Sec make out.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: About the modulo operator

 
0
  #4
Aug 18th, 2008
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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

Re: About the modulo operator

 
0
  #5
Aug 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: About the modulo operator

 
0
  #6
Aug 18th, 2008
>>mins = int(sec / 60)
Its not necessary to use the typecast. sec/60 always returns an int when sec is an int.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC