PDA

View Full Version : Psuedo Code Problem!!! Urgent!!!



pharcyde
09-18-2001, 08:14 PM
hey i need help on the following problems in pseudocode, i need this by tonite sometime:


a) Given a triplet of numbers (day, month, year) representing a date, determine if that date is in the past, present, or the future (i.e., is it today's date, or is it before or after today’s date?). You may assume that today's date is available to you (e.g., assign thisYear to be the current year ).

(b) Given a passage of text, consisting of words separated by one or more blanks, commas, or periods, count how many words there are in the passage that end with the letter 'y'. You may assume that there are no other punctuation marks in the passage. For this problem, you must write at least one sub-algorithm (procedure) to determine if the end of a word has been reached. Your main algorithm must use this sub-algorithm to correctly solve the problem. Bear in mind that your algorithm may only examine one new letter at a time, starting with the left-most letter.

(c) Convert a binary (base 2) number into a decimal (base 10) number. Bear in mind that you do not know how many digits there are in the binary number, and you may only examine one digit at a time, starting with the left-most digit.

pharcyde
09-18-2001, 08:45 PM
ok i may have solved the first two, but i really need help on the last one.

MagMan5446
09-18-2001, 08:46 PM
Yeah, I don't know anything about what you're talking about, but I was wondering about your name. Pharcyde as in the group? I just listened to their remix of Doin' Time, it rocks.

pharcyde
09-18-2001, 08:57 PM
yeah i got the name from the group but, i don't listen to there music. i just needed a name, and my brother had a pharcyde cd laying around.

alf
09-18-2001, 09:07 PM
can you put the values into an array?

do you have icq? im 19631307, if you do

i know....read it left to right, keep concatenating it into a string...(the highest order digit will be put in first)

then process the string first 0/1 gets the value of 2 to the 0th power (= 1), second 0/1 gets 2 to the 1st power (=2), third 0/1 gets 2 to the 2nd power (=4), just accumulate it till you reach the end of the string

will that work for ya?

-alf

pharcyde
09-18-2001, 09:13 PM
no cant use arrays, i'll read over what you wrote now

Miscue
09-18-2001, 09:14 PM
a.

1.

(turn the date into YYYYMMDD format. YYYY carries most weight, so it is put in front. In this way you can directly compare it to other dates using < > =, etc.)

todayDate = yearToday * 1000 + (monthToday) * 100 + dayToday

otherDate = yearOther * 1000 + (monthOther) * 100 + dayOther

if todayDate == otherDate
present
if todayDate > otherDate
past
else
future

2.
(Easier to understand, but not as efficient and harder to code)

if yearToday > yearOther
past
else if yearToday < yearOther
future
else
if monthToday > monthOther...

pattern continues...

b.


main

repeat until end of file
read letter
if y read in
yFoundProcedure
end if
end repeat

end main

yFoundProcedure
read letter
if end of file, quit
else
if y read in
yFoundProcedure (call itself, takes the 'yyy' case into consideration)
else if . or , or (space) read in
increment wordsEndInY
end if
end yFoundProcedure

c. (are you sure it's left-most digit? or right?)

The left-most digits is way harder... I'm working on it now...

Miscue
09-18-2001, 09:21 PM
Originally posted by alf
can you put the values into an array?


i know....read it left to right, keep concatenating it into a string...

A string is an array of characters. If he can't use an array, he can't use a string.

Going right to left there is a super simple formula... left to right... a pain. I can't see why your teacher would ask you to do it that way...

pharcyde
09-18-2001, 09:21 PM
left most for sure

alf
09-18-2001, 09:42 PM
alright...
i wont do the pseudo code for you but here is the theory

for every new digit, you will add 1 or 0 (since it is the least significant bit)...before you do that, you have to shift the others left...do to this, just multiply by 2

(do some examples)

111=7
promote 111 to 1110 and that is 14, then add 1 or 0

11011 = 27
promote (shift left) to 110110 and that is 54, then add 0 or 1

will that work?

Miscue
09-18-2001, 09:43 PM
c. Here goes...

main
getBinary
convertToDec
end main

begin getBinary
digit=0
accumulator=0
repeat until end
accumulator = accumulator * 10 // move digit over
read digit
if not end
accumulator = accumulator + digit // tack on next dig
end if
digit = digit+1
end repeat
end getBinary

begin convertToDec
currExp = 0
decimalAnswer = 0
repeat until digit = 0
decimalAnswer = decimalAnswer + (accumulator modulo 10) * 2^(currExp)
digit = digit - 1
currExp = currExp + 1
accumulator = accumulator div 10
end repeat
end convertToDec


I did not double check this for three reasons.... It's probably right... close enough for pseudocode... and it does you no good if I just give you the answer and you don't understand it. You need to go through it and see how it works. If you catch an error, great.

Miscue
09-18-2001, 09:50 PM
Dammit alf. That's the easy way. :)


accumulator = 0
repeat until end
accumulator = accumulator * 2
readDigit
accumulator = accumulator + readDigit
end repeat

alf
09-18-2001, 09:51 PM
lol

sorry miscue!

what line of work are you in anyway?

pharcyde
09-18-2001, 09:53 PM
thanks guys, i'll have to go over all this and figure out what the heck you typed.

Miscue
09-18-2001, 10:00 PM
I take pictures of people and print them out onto school ID cards for $7/hour. :)

pharcyde
09-18-2001, 10:26 PM
WOOHOO!!! I finally got it!!

its so simple now