| | |
Pseduocode help - extracting parts of a real number
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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
1
#2 Nov 3rd, 2009
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
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.
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- I need a program that prompts the user to input a real number and promptly rounds it (C)
- Question about how to design a parts location database (Database Design)
- Complex Number Class (C++)
- problem with guess a number program (C++)
- extracting an arbitrary number of numbers from a string (C)
- How can I modify this program? (C++)
- Extracting Numbers (C)
- Real number to binary? (C)
- How to code a friend template function? (C++)
Other Threads in the Java Forum
- Previous Thread: Input an integer number and display its largest factor
- Next Thread: Casting of int[] to Object[]
Views: 185 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






