Convert Seconds to Hours Minutes (must use Pass by Reference)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 5
Reputation: LostnC is an unknown quantity at this point 
Solved Threads: 0
LostnC LostnC is offline Offline
Newbie Poster

Convert Seconds to Hours Minutes (must use Pass by Reference)

 
0
  #1
Oct 22nd, 2009
I need to convert seconds to Minutes, hours and seconds.
I must use pass by reference in variables.
I know I need to use the modulus to deal with the remainders when I'm converting to Hours, minutes and seconds. I am really having a difficult time with the code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
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: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Oct 23rd, 2009
We only help those who help themselves. Post the code you tried. You will have to know a little about pointers if you are required to pass variables by reference.
Last edited by Ancient Dragon; Oct 23rd, 2009 at 1:27 am.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: LostnC is an unknown quantity at this point 
Solved Threads: 0
LostnC LostnC is offline Offline
Newbie Poster
 
0
  #3
Oct 26th, 2009
Originally Posted by Ancient Dragon View Post
We only help those who help themselves. Post the code you tried. You will have to know a little about pointers if you are required to pass variables by reference.
Here's what I have so far. I am trying to enter a number and convert the number entered into hours, minutes and seconds. I must use "pass by reference" in my code.



  1. #include<stdio.h>
  2.  
  3. void time(int, int*, int*, int*); //Function Prototype
  4.  
  5. int main(void) //function header
  6.  
  7.  
  8. {
  9. int hold;
  10. float total;
  11.  
  12.  
  13. printf("Enter the number of seconds ->");
  14. scanf("%d",&total);
  15. scanf("%d", &hold);
  16. return 0;
  17. } // end of main function
  18.  
  19. void time (int total, int *hours, int *min, int *sec) // Function header Line
  20. {
  21. int rem1, rem2;
  22. *hours = total / 3600;
  23. rem1 = total % 3600;
  24. *min = rem1 / 60;
  25. rem2 = rem1 % 60;
  26. printf ("the number of hours is ->", *hours);
  27. printf ("the number of minutes is ->", *min);
  28. printf ("the number of seconds is ->", *sec);
  29. }
Last edited by WaltP; Oct 27th, 2009 at 3:22 am. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training
 
0
  #4
Oct 27th, 2009
Originally Posted by LostnC View Post
Here's what I have so far. I am trying to enter a number and convert the number entered into hours, minutes and seconds. I must use "pass by reference" in my code.
  1. #include<stdio.h>
  2.  
  3. void time(int, int*, int*, int*); //Function Prototype
  4.  
  5. int main(void) //function header
  6.  
  7.  
  8. {
  9. int hold;
  10. float total;
  11.  
  12.  
  13. printf("Enter the number of seconds ->");
  14. scanf("%d",&total);
  15. scanf("%d", &hold);
  16. return 0;
  17. } // end of main function
  18.  
  19. void time (int total, int *hours, int *min, int *sec) // Function header Line
  20. {
  21. int rem1, rem2;
  22. *hours = total / 3600;
  23. rem1 = total % 3600;
  24. *min = rem1 / 60;
  25. rem2 = rem1 % 60;
  26. printf ("the number of hours is ->", *hours);
  27. printf ("the number of minutes is ->", *min);
  28. printf ("the number of seconds is ->", *sec);
  29. }
Here are some observations on the code you have posted.

- I'm not sure why you have declared the variable hold. I'm assuming that the variable total is supposed to be the total number of seconds that will be the input to your time function. That being the case, then it should be declared as int.

- In main you don't even call your time function. You haven't declared variables for hours, minutes and seconds as well. These should be declared and their addresses should be passed to the time function you have defined (there's your pass by reference).

- In your time function, you haven't calculated the number of seconds and you don't need the rem2 variable as well.

- Depending on the requirements of your time function, you probably don't want to print the results in the function itself - if this was the case, there would be no need to pass parameters for hours, minutes and seconds. In any case, you are missing a format specifier (%d) in your printf function calls.

- You should look at providing a more meaningful name for the time function.

Here's a modified version of your code. Post back if you have more questions:
  1. #include<stdio.h>
  2.  
  3. void time(int, int*, int*, int*);
  4.  
  5. int main(void) {
  6.  
  7. int total, hours, mins, secs;
  8.  
  9. printf("Enter the number of seconds -> ");
  10. scanf("%d", &total);
  11.  
  12. time(total, &hours, &mins, &secs);
  13. printf("the number of hours is -> %d\n", hours);
  14. printf("the number of minutes is -> %d\n", mins);
  15. printf("the number of seconds is -> %d\n", secs);
  16.  
  17. return 0;
  18. }
  19.  
  20. void time(int total, int *hours, int *min, int *sec) {
  21.  
  22. int rem1;
  23. *hours = total / 3600;
  24. rem1 = total % 3600;
  25. *min = rem1 / 60;
  26. *sec = rem1 % 60;
  27. }
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 542 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC