LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2004, 04:40 AM   #1
acbenny
Member
 
Registered: Jul 2004
Posts: 44

Rep: Reputation: 15
Counting Time problem


How can this programe can count time , how to get this result ? can any one teach me in detail ?

#!/bin/bash

seconds=0

echo -n "Enter number of seconds > "
read seconds

hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))

echo "$hours hour(s) $minutes minute(s) $seconds second(s)"

Enter number of seconds > 6001
1 hour(s) 40 minute(s) 1 second(s)

Enter number of seconds > 120
0 hour(s) 2 minute(s) 0 second(s)
 
Old 09-08-2004, 04:51 AM   #2
Oliv'
Senior Member
 
Registered: Jan 2004
Location: Montpellier (France)
Distribution: Gentoo
Posts: 1,014

Rep: Reputation: 36
Hi,

that's not exactly a count time program... that's more a convert time program
first: echo -n "sentence" to display the sentence and don't go to the next line
second: read value ... it displays a prompt (cursor) and wait for a value... the entered value is stored in value
Then that's maths
so it counts how many hours there's in value thanks to a division (3600 sec = 1 hr)... the result is stored in hour variable... it takes the rest of the division. Then it does the same for minutes...

Oliv'
 
Old 09-08-2004, 04:59 AM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
OK, first:

1 hour = 60 minutes = 3600 seconds

You enter a number of seconds...

hours=$((seconds / 3600))

this divides your number of seconds by 3600 (discarding the remainder) to give you a number of hours.

seconds=$((seconds % 3600))

x % y means get the remainder of x / y, so this gets the "left over" number of seconds after the full hours have been removed. Note that this value then replaces the original value of the variable 'seconds'.

the next two lines do the same thing to the remaining seconds, only to calculate minutes instead.
The three variables are then printed out.

Does this help, or have I missed something?
 
Old 09-08-2004, 08:49 PM   #4
acbenny
Member
 
Registered: Jul 2004
Posts: 44

Original Poster
Rep: Reputation: 15
Let's say, if I enter 5001

Enter number of seconds > 5001
1 hour(s) 23 minute(s) 21 second(s)

then

hours = 5001 /3600 -------------- =1.389...
seconds = 5001 % 3600 ----------- Reminder = 1401
and so on .....

I still don't know how to subsitude 5001 to this program to get the result .
 
Old 09-09-2004, 05:03 AM   #5
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
5001 /3600 -------------- =1.389

Yes, that's what a calculator will give you - but the shell uses integer division, and discards the .389. You can see this for yourself by typing:
Code:
echo $((5001/3600))
the output will just be 1. So the code sets $hours to 1, and moves on to replace $seconds with the remainder of 1401.
It then does the same thing with 1401 - divides by 60 to get $minutes, and the remainder replaces $seconds.
 
Old 09-09-2004, 09:15 PM   #6
acbenny
Member
 
Registered: Jul 2004
Posts: 44

Original Poster
Rep: Reputation: 15
According to the programe, I try to subsitute 5001.

It most likely would be this.

hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))

1=$(( 5001 / 3600))
1401=$(( 5001 % 3600))
23 =$(( 1401 / 60))
21=$((1401 % 60))

why the programe will choose 21 to be the seconds but not 1401 , is it programe will choose the last line for the final result ?
 
Old 09-09-2004, 09:58 PM   #7
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
If I say:

x = 5

and then say:

x = x + 1

the right hand side (x+1) will be calculated, and then the value will be put into x. So when the program sees:

seconds = $((seconds % 60))

it does the calculation (current value of $seconds (1401) modulus 60, and then puts the answer (21) in $seconds. At any point after this line, $seconds is 21 - the value 1401 is 'lost' in the program.
 
Old 09-11-2004, 07:19 AM   #8
aneeshm
Member
 
Registered: Jun 2004
Location: Indore , MP , India
Distribution: Ubuntu 5.04
Posts: 41

Rep: Reputation: 15
#include<iostream>
using namespace std;

int main()
{

int hr,min,sec,i;

cout<<"\nEnter the time in seconds\n";
cin>>i;

hr=i/3600;
min=(i%3600)/60;
sec=(i%3600)%60;

cout<<"\nThe time in hr/min/sec format is "<<hr<<"/"<<min<<"/"<<sec;

return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does it return zero when counting? PenguinMan Programming 5 12-21-2017 12:12 AM
Time stops counting when I shut my laptop jfall Linux - Hardware 3 10-31-2004 10:53 AM
Three Years and Counting! trickykid General 9 01-06-2004 06:30 PM
And counting... MasterC General 18 09-24-2003 05:07 AM
counting dummyagain Programming 3 09-24-2003 02:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration