I'm writing pseducode for a Java class. Obviously being pseudo the language shouldn't matter but thought I'd mention it as it would explain any 'bias' I have. BTW, this is my first programming class. I'm revising for my end of semester exam.

Write a pseudo code algorithm which will input a real number. The number represents a length of time, hh.mmss where hh=hours, mm = minutes and ss = seconds. If the input is valid then the algorithm should output the time hours, minutes and seconds.

My attempt:
MAIN
INPUT time
hours = extractHours<-time
minutes=extractMinutes<-time
seconds=extractSeconds<-time
IF (validateTime<-hours, minutes, seconds) THEN
OUTPUT hours, minutes, seconds
END IF

extractHours
hours=(int)time

extractMinutes
tempMinutes=(int)(time*100)
minutes=tempMinutes MOD 100

extractSeconds
tempSeconds=(int)(time*10000)
seconds=tempSeconds MOD 10000

validateTime
isValid=false
IF (0 <= hours <=23) AND (0<= minutes <=59) AND (0<=seconds <=60) THEN
isValid=true
END IF

At the validation I believe that the seconds should until 59. For the hours you 23, 59. Why not the same for seconds.

For the extract when you multiply and then do a mod, you will get 0.

123 * 100 = 12300
12300 mod 100 = 0
12300/100 = 123

mod returns what is left from the division:
9 = 2 * 4 + 1
9 mod 2 = 1
9 mod 4 = 1

This is the time: 125345 (12:53:45)
In java when you divide an int with an int you will get an int:

125345/100 = 1253
1253 * 100 = 125300

125345/100.0 = 1253.45

So when you divide by 100 you have:
1253
The time is:
125345
1253
Think how can you get the seconds.

THEN:
1253 / 100 =
12
Now how can you get the minutes.

The 12 is the hours

commented: Very helpful. Thank you. +0

Thanks very much.

Yes, I certainly did mean to validate seconds against 59.

Regarding the integer calculations, I'll have to refresh that section. I can see now that I should have used / not MOD.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.