Question:
Explain this to me?
♪£yricảl♪
2008-05-12 05:57:38 UTC
Problem Specification:
Design a program to solve the following problem:
Write a program to convert seconds to hours, minutes and seconds. When the user enters 7384, the following is displayed on the computer screen:

7384 seconds is 2 hours, 3 minutes and 4 seconds.

------------------------
Below is the answer given to me by the tutor:
------------------------
Read total sec
COMPUTE hour = total sec/3600
min = total sec - (hour * 3600)/60
sec = total sec % 60
PRINT hour, min, sec.
-----------------------------
Q1) Does the word 'Read' have to all capitalise? That is, READ? cos compute and print are all capitalise.

Q2) I don't understand this >>> min = total sec - (hour * 3600)/60.....why hour multiply 3600 then the answer divide by 60...

Q3) What does it mean when she wrote total sec % 60?...

I really don't understand...Please help...Thanks...
Five answers:
Galadriel
2008-05-12 06:23:19 UTC
1. No, it will not impact the code, if you want you can, just to have a balance throughout the printed response.



2. simple:



You need to get the integer of the hour (remember you will get something like this: 2.0511111 (using the example the tutor gave you... so we only need the 2 part).



Hour = int(total sec/3600)

then Min = (Total sec - (hour*3600))/60



Simply because you have converted the hour to seconds and you want to convert to minutes the seconds once the first operation has been done:



Hour = int (7384/3600) = 2 hours

Min = int ((7384-(7200))/60) = int(184 / 60) = int(3.06666)=3 mins



3. bit hard to explain... but think this may help:**

-------------------------------------------------------------------------------

"In computing, the percent character is also used for the modulo operation in programming languages that derive their syntax from the C programming language, which in turn acquired this usage from the earlier B.The ASCII code for the percent character is 37, or 0x25 in hexadecimal. In the textual representation of URIs, a % immediately followed by a 2-digit hexadecimal number denotes an octet specifying (part of) a character that might otherwise not be allowed in URIs (see percent-encoding). Names for the percent sign include percent sign (in ITU-T), mod, grapes (in hacker jargon), and the humorous double-oh-seven (in INTERCAL).



In SQL, the percent sign is a wildcard character in "LIKE" expressions, for example SELECT * FROM table WHERE fullname LIKE 'Lisa %' will fetch all records whose names start with "Lisa "



In TeX and PostScript, a % denotes a line comment.



In Basic, a trailing % after a variable name marks it as an integer.



In Perl % symbol is for hashes.



In many programming languages' string formatting operations (performed by functions such as printf) the percent sign denotes parts of the template string that will be replaced with arguments. In Python, the percent sign is also used as the string formatting operator. "

--------------------------------------------------------------------------------

Now... make it simple and ask your tutor if it was an error... I don't know what programming language you are using.. but seconds should be something like this:



Seconds = total seconds - ((hours*3600)+(min*60))



7384 - ( (2*3600)+(3*60))

7384 - (7200+180)

7384 - 7380

4 seconds
2008-05-12 06:12:06 UTC
1. This is irrelevant. Your tutor is just trying to explain the process to you.

2. It's just written this way for the sake of clarity. Your tutor is subtracting the number of seconds that are already accounted for by the calculated number of hours. Plug in the example values you provided in this question if you don't understand.

3. This is the modulo operator. It essentially returns the remainder of a division operation.



Think about it like you were going to solve it on paper. You have 7384 seconds. How many hours, minutes and seconds is this?



3600 goes into 7384 two times, so it is two hours.

To compute minutes, we have to subtract 7200 from 7384, because we 7200 of our seconds are represented by the two hours. That's what happens in the line including (hour * 3600).



So we have 184 seconds left. 60 goes into this number 3 times, which means there are 3 minutes. Then we use the % operator to get the number of seconds left over, which is 4. So, 2 hours, 3 seconds, and 4 minutes.



Your tutor basically solved the problem for you. If you don't understand the syntax, you really need to brush up on your language study because this is a very basic problem.
AnalProgrammer
2008-05-12 06:11:20 UTC
This appears to be pseudo code rather than an actual programming language. As such you do not have to worry about capitalisation. The print and compute are capitalised to aid reading the code. So yes you could also capitalise the read. You decide.



Hours will be an integer. So

COMPUTE hour = total sec/3600

will give the whole number of hours.

Now to get minutes you want the number of seconds in the whole number of hours (hour * 3600) and subtract that from the total secs.

Then you divide the result by 60 to get the whole number of seconds.



sec = total sec % 60

Will give the remainder of the calculation total sec divided by 60.



I hope that helps.
red_n_white_army
2008-05-12 06:10:17 UTC
I'm not going to answer your homework for you, but it looks like what you've been given there is pseudocode. It's not code written in any particular language, but it outlines the steps you would need to carry out in order to solve a particular problem.



2. Multiplying by 3600 and then dividing by sixty is just to show you how to convert seconds to minutes.



3. % is the modulo operator, it gives the remainder when one number is divided by another. e.g. 16%5 = 1
sauber
2016-12-17 18:56:32 UTC
All i prefer for Christmas is my 2 front enamel ( I had an twist of destiny years in the past and the front have porcelin caps that could desire to get replaced in approximately 2 months while i could have saved sufficient money to change them!)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...