944,057 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 418
  • Java RSS
Nov 3rd, 2009
0

Pseduocode help - extracting parts of a real number

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tom.t is offline Offline
6 posts
since Nov 2009
Nov 3rd, 2009
1
Re: Pseduocode help - extracting parts of a real number
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
Last edited by javaAddict; Nov 3rd, 2009 at 7:03 am.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Nov 3rd, 2009
0
Re: Pseduocode help - extracting parts of a real number
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tom.t is offline Offline
6 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Input an integer number and display its largest factor
Next Thread in Java Forum Timeline: Casting of int[] to Object[]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC