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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-17-2010
g8ortech14
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.
• 09-17-2010
rags_to_riches
Well...how would you do it on paper? Start there.
• 09-17-2010
g8ortech14
Quote:

Originally Posted by rags_to_riches
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.
• 09-17-2010
Celired
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
• 09-17-2010
rogster001
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.
• 09-17-2010
g8ortech14
Quote:

Originally Posted by rogster001
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?)
• 09-17-2010
laserlight
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.
• 09-17-2010
g8ortech14
Quote:

Originally Posted by laserlight
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.
• 09-17-2010
rogster001
Quote:

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
• 09-17-2010
g8ortech14
Quote:

Originally Posted by rogster001
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.
• 09-17-2010
g8ortech14
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?
• 09-17-2010
laserlight
You misplaced a closing parenthesis for the second printf.
• 09-17-2010
g8ortech14
Quote:

Originally Posted by laserlight
You misplaced a closing parenthesis for the second printf.

Thanks again
• 09-17-2010
g8ortech14
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.
• 09-17-2010
rags_to_riches
Shouldn't there be some sort of subtraction involved in your algorithm there?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last