Hi, i've been trying to do this algorithm problem recently but i cant seem to beable to follow the algorithm, It requires me to fill in a table which i can partly complete but dont seem to beable to understand some lines like when i have to push anAarray, could some please help me by seeing what the answers is so i can check if im right or not and could some please translate the algorithm so it is easier to understand so i can trace it properly, any help will be appreciated, thanks!
The procedure PUSH(anArray, top, ch) first adds one to the value of top and then stores the single
character held in ch in the one-dimensional array anArray at anArray[top].
The function POP(anArray, top) returns the character held in anArray[top] and then takes 1 off the
value of top.
The function NEXTCHAR(str) returns the next character in its argument str which is a string.
The functions F(ch), G(ch) and R(ch) take a single character as input and return a value according to the table below.
http://www.imagehosting.com/out.php/...9_untitled.bmp
In the algorithm, & joins two strings together (concatenation).
Code:
1. INPUT aString
2. aString = aString & '')''
3. top = 0
4. PUSH(anArray, top, ''('')
5. newString = '' ''
6. rank = 0
7. current = NEXTCHAR(aString)
8. WHILE current <> '' ''
9. IF top < 1 THEN
10. OUTPUT ''Invalid string''
11. END the procedure
12. ENDIF
13. WHILE F(current) < G(anArray[top])
14. temp = POP(anArray, top)
15. newString = newString & temp
16. rank = rank + R(temp)
17. IF rank < 1 THEN
18. OUTPUT ''Invalid string''
19. END the procedure
20. ENDIF
21. ENDWHILE
22. IF F(current) <> G(anArray[top]) THEN
23. PUSH(anArray, top, current)
24. ELSE
25. POP(anArray, top)
26. ENDIF
27. current = NEXTCHAR(aString)
28. ENDWHILE
29. IF top <> 0 OR rank <> 1 THEN
30. OUTPUT ''Invalid string''
31. ELSE
32. OUTPUT ''Valid string''
33. ENDIF
34. END of procedure
I have to fill in the table below three times by using the three different input strings which are:
CD
p*(q
(v-w)*(x+y)
http://www.imagehosting.com/out.php/..._untitled2.bmp[/URL]
you have to work left to write in the table filling it in everytime something changes,
Ive been able to fill in "cd" table which i think is mostly right but im having problem with the other two. its harder then its seems lol. If someone could translate it so its easier to understand, i would be very gratefull. thanks for your help!!