944,111 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1987
  • C RSS
Oct 22nd, 2009
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LostnC is offline Offline
7 posts
since Oct 2009
Oct 23rd, 2009
-7
Re: Convert Seconds to Hours Minutes (must use Pass by Reference)
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 26th, 2009
0
Re: Convert Seconds to Hours Minutes (must use Pass by Reference)
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????
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LostnC is offline Offline
7 posts
since Oct 2009
Oct 27th, 2009
0
Re: Convert Seconds to Hours Minutes (must use Pass by Reference)
Click to Expand / Collapse  Quote originally posted by LostnC ...
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. }
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: alignments
Next Thread in C Forum Timeline: socket alive after exe closes





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


Follow us on Twitter


© 2011 DaniWeb® LLC