Thread: Help me please!!!

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    Help me please!!!

    I didn't find sollution:S I wrote program but I didn't indicate a.m.(a) or p.m.(p).

    Write a function that gets a time in seconds as a parameter and returns the time in hours, minutes, seconds and a character indicating a.m.(a) or p.m.(p) .

    Code:
    #include<stdio.h>
    
    int duration(int time,int *hour, int *minutes, int *seconds)
    {
    	char a,p;
    	
    	*hour=time/3600;
    	*minutes=time%3600/60;
    	*seconds=time%3600%60;
    	
    		
    }
    
    int main(void)
    {
    	int seconds,hour,min,sec;
    
    
    	printf("Enter a time in seconds(<=0 stop):");
    	scanf("%d",&seconds);
    	
    	while(seconds>0)
    	{
    		duration(seconds,&hour,&min,&sec);
    		printf("Time is %d:%d:%d\n",hour,min,sec);
    		
    		
    		
    		printf("Enter a time in seconds(<=0 stop):");
    		scanf("%d",&seconds);
    		
    	}
    	
    	
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Without knowing the actual starting time there is no way of knowing the time of day.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Without knowing the actual starting time there is no way of knowing the time of day.

    Jim
    example run:
    ------------

    Enter a time in seconds (<=0 to stop): 45930
    Time is 12:45:30 a.m.


    Enter a time in seconds (<=0 to stop): 65987
    Time is 6:19:47 p.m.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So your sample run knew the time of day to start with. No where in your program is there a starting time. Looking at your examples it appears that your starting time is midnight. So you need to use this fact to determine am or pm.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think he is using seconds elapsed since midnight, if I read his duration function correctly. If that's the case, hour should be a number from 0-23. If it's less than 12, it's AM, between 12 and 23 inclusive, it's PM. The only trick is that if hour comes back as 0, you need to print 12.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    I think he is using seconds elapsed since midnight, if I read his duration function correctly. If that's the case, hour should be a number from 0-23. If it's less than 12, it's AM, between 12 and 23 inclusive, it's PM. The only trick is that if hour comes back as 0, you need to print 12.
    yes time>12 ----->p.m else a.m
    ex: 65987 seconds /3600 >12 , its p.m else a.m but I didn't.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    printf("Time is %d:%02d:%02d %c.m.\n", hour, min, sec, "ap"[hour > 11]);
    Note the %02d so that each has leading zero if there is only 1 digit.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do you not know how to write an if statement? Check your books and class notes. Read our tutorial and find some more on Google.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by nonoob View Post
    Code:
    printf("Time is %d:%02d:%02d %c.m.\n", hour, min, sec, "ap"[hour > 11]);
    Note the %02d so that each has leading zero if there is only 1 digit.
    thank you very much! but this part must be "ap"[hour>12]

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How would you go about adding this? I suggest you read the previous posts and try to implement a solution.

    Jim

Popular pages Recent additions subscribe to a feed