Logical! Logical! Why do you have to be so Logical?

This is a discussion on Logical! Logical! Why do you have to be so Logical? within the C Programming forums, part of the General Programming Boards category; Ok so I am attempting to formulate my first program. The input is time in seconds (i.e. 122 secs). This ...

  1. #1
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22

    Logical! Logical! Why do you have to be so Logical?

    Ok so I am attempting to formulate my first program. The input is time in seconds (i.e. 122 secs). This would then be output to 2 min 2 secs. The program would also need to be able to display hours if there were enough seconds. I thought about using an if/else statement, but can't seem to formulate the logic to accomplish this. Can anyone kick start my brain and set me down a good path to complete this project. Not looking for an answer just a place to start.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Well...how would you do it on paper? Start there.

  3. #3
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by rags_to_riches View Post
    Well...how would you do it on paper? Start there.
    If I was given 122 seconds. I would take 122 divided by 60 giving me 2 minutes with a remainder 2 would be output to 2 minutes 2 seconds. Declare the variables to be hrs, min, and sec.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Paper

    You'll need to plan this out on paper first, that would be the best way to do it. You could then figure out all of the variables you will need to use and what kind of logic to go with it. rags_to_riches has the right idea

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Well you just gave yourself the start you were talking about.

    one thing you should notice is you now have a constant (60seconds is always a minute) and by extension you will have another constant (60 minutes is always an hour)
    you have noted you will need variables for hrs mins(note this will be your accumulated or total minutes) and seconds,
    you have noted you need to do division, so that should tell you something about the type of variable that one of them at least will have to be.
    you have noted you will need to calculate a remainder so you need an equation (or maybe something is provided by the language) to let you calculate that.
    Last edited by rogster001; 09-17-2010 at 09:27 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by rogster001 View Post
    Well you just gave yourself the start you were talking about.

    one thing you should notice is you now have a constant (60seconds is always a minute) and by extension you will have another constant (60 minutes is always an hour)
    you have noted you will need variables for hrs mins(note this will be your accumulated or total minutes) and seconds,
    you have noted you need to do division, so that should tell you something about the type of variable that one of them at least will have to be.
    you have noted you will need to calculate a remainder so you need an equation (or maybe something is provided by the language) to let you calculate that.
    So I went to paper here is what I have:

    I need to declare variables:
    int total_secs
    float hrs, mins, secs

    input total_secs
    get total_secs

    if total_secs <= 60 then display secs = total_secs
    if total_secs >= 61 then total_secs/60 = mins

    (I am missing something to capture and display the left over secs?)

    if mins <= 60 display mins
    if mins >= 61 then mins / 60 = hrs

    (I am missing something to capture and display the left over min and secs?)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by g8ortech14
    if total_secs <= 60 then display secs = total_secs
    if total_secs >= 61 then total_secs/60 = mins
    That looks like a mistake in the boundary condition. Consider, if you are given 60 seconds, shouldn't you display 1 minute and 0 seconds rather than 0 minutes and 60 seconds?

    My recommendation is to find out the number of hours first. You can then subtract the number of seconds corresponding to that number of hours (or more simply, use modulo). Repeat this process to find out the number of minutes and seconds.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by laserlight View Post
    That looks like a mistake in the boundary condition. Consider, if you are given 60 seconds, shouldn't you display 1 minute and 0 seconds rather than 0 minutes and 60 seconds?

    My recommendation is to find out the number of hours first. You can then subtract the number of seconds corresponding to that number of hours (or more simply, use modulo). Repeat this process to find out the number of minutes and seconds.
    I see the boundary error thanks.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    if total_secs <= 60 then display secs = total_secs
    if total_secs >= 61 then total_secs/60 = mins
    Here you should look up a way of defining a value you never mean to change...

    This will let you name your value MINUTE or whatever it is you are defining, then in your program wherever you have 60 (this would be called a magic number at present in most cases)
    you write MINUTE instead, this makes it easier for you and other people to understand, it lets you and everybody know that that value should not be changed. it also means that at times where you do need to alter your constant values you only have to alter a couple of lines instead of going through the whole program
    Last edited by rogster001; 09-17-2010 at 10:59 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #10
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by rogster001 View Post
    Here you should look up a way of defining a value you never mean to change...

    This will let you name your value MINUTE or whatever it is you are defining, then in your program wherever you have 60 (this would be called a magic number at present in most cases)
    you write MINUTE instead, this makes it easier for you and other people to understand, it lets you and everybody know that that value should not be changed. it also means that at times where you do need to alter your constant values you only have to alter a couple of lines instead of going through the whole program
    That makes sense and will help with clarifying my code. Thanks for the helpful input.

  11. #11
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main() 
    { 
    	double total_seconds, hours, minutes, seconds;
    	
    	printf("Input Seconds: ");
    	scanf("%lf", &total_seconds);
    
    	printf ("Seconds Inputed = %lf \n\n"), total_seconds; 
    
     
    
    system ("pause");	
    return(0);
    When I type this code in its returning 0. Where did I hose it up at?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    You misplaced a closing parenthesis for the second printf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Quote Originally Posted by laserlight View Post
    You misplaced a closing parenthesis for the second printf.
    Thanks again

  14. #14
    Registered User g8ortech14's Avatar
    Join Date
    Oct 2007
    Location
    Orlando FL
    Posts
    22
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    main() 
    { 
    	int minutes = 60;
    	int total_seconds; 
    	double mins, seconds, hours; 
    	
    	printf("Input Seconds: ");
    	scanf("%i", &total_seconds);
    		
    	hours = (total_seconds / minutes) / minutes;
    	mins = total_seconds / minutes;
    	seconds = total_seconds % minutes;
    
    	printf("Converted = %.0lf:%.0lf:%.0lf\n\n", hours, mins, seconds);
     
    
    system ("pause");	
    return(0);
    }
    Minutes are still calculating incorrectly ? Grrr! I feel stupid. I make it happen on paper, but can't translate to C.

  15. #15
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Shouldn't there be some sort of subtraction involved in your algorithm there?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Physical and Logical Memory.
    By TheUmer in forum General Discussions
    Replies: 3
    Last Post: 11-04-2010, 05:28 AM
  2. Logical And
    By shiju in forum C Programming
    Replies: 6
    Last Post: 01-11-2004, 11:15 AM
  3. Logical Error
    By gardenair in forum C Programming
    Replies: 2
    Last Post: 04-06-2003, 05:18 PM
  4. Size of 1 pixel
    By ooosawaddee3 in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2002, 09:06 PM
  5. logical operators
    By sballew in forum C Programming
    Replies: 4
    Last Post: 09-04-2001, 07:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21