| | |
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:
Solved Threads: 0
0
#3 Oct 26th, 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.
c Syntax (Toggle Plain Text)
#include<stdio.h> void time(int, int*, int*, int*); //Function Prototype int main(void) //function header { int hold; float total; printf("Enter the number of seconds ->"); scanf("%d",&total); scanf("%d", &hold); return 0; } // end of main function void time (int total, int *hours, int *min, int *sec) // Function header Line { int rem1, rem2; *hours = total / 3600; rem1 = total % 3600; *min = rem1 / 60; rem2 = rem1 % 60; printf ("the number of hours is ->", *hours); printf ("the number of minutes is ->", *min); printf ("the number of seconds is ->", *sec); }
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????
0
#4 Oct 27th, 2009
•
•
•
•
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.
c Syntax (Toggle Plain Text)
#include<stdio.h> void time(int, int*, int*, int*); //Function Prototype int main(void) //function header { int hold; float total; printf("Enter the number of seconds ->"); scanf("%d",&total); scanf("%d", &hold); return 0; } // end of main function void time (int total, int *hours, int *min, int *sec) // Function header Line { int rem1, rem2; *hours = total / 3600; rem1 = total % 3600; *min = rem1 / 60; rem2 = rem1 % 60; printf ("the number of hours is ->", *hours); printf ("the number of minutes is ->", *min); printf ("the number of seconds is ->", *sec); }
- 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:
C Syntax (Toggle Plain Text)
#include<stdio.h> void time(int, int*, int*, int*); int main(void) { int total, hours, mins, secs; printf("Enter the number of seconds -> "); scanf("%d", &total); time(total, &hours, &mins, &secs); printf("the number of hours is -> %d\n", hours); printf("the number of minutes is -> %d\n", mins); printf("the number of seconds is -> %d\n", secs); return 0; } void time(int total, int *hours, int *min, int *sec) { int rem1; *hours = total / 3600; rem1 = total % 3600; *min = rem1 / 60; *sec = rem1 % 60; }
Manic twiddler of bits
![]() |
Similar Threads
- convert seconds to minutes, hours, days, months, years, etc. (C)
- Passing Structures by Reference (C++)
- Days, Hours, Minutes and Seconds countdown (VB.NET)
Other Threads in the C Forum
- Previous Thread: alignments
- Next Thread: socket alive after exe closes
Views: 542 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable feet fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide infiniteloop initialization interest intmain() kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft multi mysql oddnumber odf open openwebfoundation pattern pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user visualstudio voidmain() win32 win32api windows.h






