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.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
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.
Edited by Ancient Dragon because: n/a
LostnC 0 Newbie Poster
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.
#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);
}
Edited by WaltP because: Added CODE tags -- with all the help about them, how could you miss using them????
yellowSnow 607 Posting Whiz in Training
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.
#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); }
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:
#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;
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.