Question
Can you help me with the following C programming questions below from the given sample code! sample code: #include #include /*------ Function to read the
Can you help me with the following C programming questions below from the given sample code!
sample code:
#include
/*------ Function to read the time --------*/ void readtime(int* h, int* m, int* s){ printf("Enter an initial 24-hour time as hours minutes and seconds (0 to stop): "); scanf("%d", h); if(*h == 0) // if h == 0, than exit the program; exit(1);
scanf("%d %d", m, s); }
/*--------- Function to read the time that add to actual time ------------*/ void addtime(int* h, int* m, int* s){ printf("Enter the time to add as hours minutes and seconds: "); scanf("%d %d %d", h, m, s); }
// start of main function int main() { int h1 = 0, m1 = 0, s1 = 0; int h2 = 0, m2 = 0, s2 = 0; int h3 = 0, m3 = 0, s3 = 0; int sum = 0, count = 0;
while(1) { // while body start from here readtime(&h1, &m1, &s1); addtime(&h2, &m2, &s2);
h3 = h1 + h2; m3 = m1 + m2; s3 = s1 + s2;
if(s3 >=60){ if(s3%60 >= 0) m3++;
s3 = s3%60; } if(m3 >= 60){ if(m3%60 >= 0) h3++;
m3 = m3%60; }
if(h3 >= 24){ count = h3/24; h3 = h3%24; }
if(count == 0) printf(" Total is %d:%d:%d ", h3, m3, s3); else if(count == 1) printf(" Total is %d:%d:%d tomorrow ", h3, m3, s3); else printf(" Total is %d:%d:%d in %d days ", h3, m3, s3, count); }
return 0; }
Execution and Output Example
Enter an initial 24-hour time as hours minutes and seconds: 7 30 50
Enter the time to add as hours minutes and seconds: 1 29 50
Total is 9:00:40 AM
Enter an initial 24-hour time as hours minutes and seconds 7 30 0
Enter the time to add as hours minutes and seconds: 5 0 0
Total is 12:30:00 PM
Enter an initial 24-hour time as hours minutes and seconds: 7 30 0
Enter the time to add as hours minutes and seconds: 25 30 0
Total is 9:00:00 AM tomorrow
Enter an initial 24-hour time as hours minutes and seconds 7 30 0
Enter the time to add as hours minutes and seconds: 49 30 0
Total is 9:00:00 AM in 2 days
Questions:
1. Explain how modulus is used in your calculations. Describe one other technique for performing the calculations and compare it to the use of modulus. Does modulus simplify the calculations?
2. Adding times is similar to adding two 3 digit numbers. You add them digit by digit and, when the sum exceeds 9, you do a carry. Explain how adding number is similar to adding times and where the two techniques differ and why.
3. Assume that, instead of adding hours, minutes and seconds, you need to add years, months and days. What new problems would you encounter solving this problem that you did not have to worry about in the addition of times? Describe how you would solve these problems.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started