
-----------------------------------
uberwalla
Thu Nov 23, 2006 5:27 pm

help with this functions program
-----------------------------------
ok in class i got to do this assignment:


Write a function called reverse which takes one value string parameter and returns a string that is the reverse of the parameter string. Incorporate this function in a main program which tests to see if the word is a palindrome.

ok well i know that u won't do my homework for me :P and so i obviously tried.
ok so i got the base program of what i need:

%The "Palindrome" Program
%By: Mike Lanthier
%Thursday, November 23,2006
var word : string
var EndWord : string
put "Please Enter A Word: " ..
get word

fcn reverse (word : string) : string
    var Length : int
    Length := length (word)
    loop
        put word (Length) ..
        Length := Length - 1
        exit when Length = 0
    end loop
    result ""
end reverse

put "The Reverse of Your Word Is: ", reverse (word)

EndWord := reverse (word)

if EndWord = word then
    put " The word ", word, " is a palindrome."
else
    put " The word ", word, " is not a palindrome."
end if


ok so what i tried to do is:
- u enter a word
- it puts it into a reverse order eg. mike would be ekim
- then im trying to have it check that if the reversed word is equal to the first word (the one u put in not in reverse) then it says its a palindrome.
and if not then it puts its not.

the trouble i have is that it always says its not a palindrome even when it is. plz compsci, PIMP MY PROGRAM! :P
lol
well help would be great :D

thx in adv.

-----------------------------------
Clayton
Thu Nov 23, 2006 5:36 pm


-----------------------------------

fcn reverse (word : string) : string
    var Length : int
    Length := length (word)
    loop
        put word (Length) ..
        Length := Length - 1
        exit when Length = 0
    end loop
    result ""
end reverse 


what is this functions result every single time?

-----------------------------------
uberwalla
Thu Nov 23, 2006 5:44 pm


-----------------------------------
every single time when u input a word it reverses it.

as an example:

u enter the word: dog

it puts out: god

it moves it one letter at a time in the loop til it does it to every letter in reverse. so it goes

g    then
o     then
d

because its Length (the last letter it ends up being) - 1 every time it loops so it goes down from the last to first letter.

-----------------------------------
wtd
Thu Nov 23, 2006 5:56 pm


-----------------------------------
A function should ideally have no effect on the world outside of it.  A function simply takes one value and returns another.

If you wish to simply have a word printed in reverse, then you should probably write a procedure called "put_string_in_reverse" or such.

-----------------------------------
uberwalla
Thu Nov 23, 2006 6:52 pm


-----------------------------------
well i know that kinda but to actually get marks i have to actually use a function. :(

in class no1 has learned procedures so im not aloud to use them :(

im the only one im my class who is really actual "kinda compedent" or however u spell it in turing// a bit a java.

so any help on how i can get an if statement or something to get it to tell if it is a palindrome would be great :D

-----------------------------------
wtd
Thu Nov 23, 2006 7:04 pm


-----------------------------------
I am not advocating using a procedure, but rather simply using functions properly.  ;)

-----------------------------------
uberwalla
Thu Nov 23, 2006 7:45 pm


-----------------------------------
yea :P sry i just randomly understood what u were saying after i posted hehe. and i did indeed solve my problem once i figured out wat i did wrong with procedure and such.

here is what i did :D


%The "Palindrome" Program
%By: Mike Lanthier
%Thursday, November 23, 2006
function reverse (w : string) : string
    var x : string := ""
    for decreasing i : length (w) .. 1
        x := x + w (i)
    end for
    result x
end reverse

var word : string
var YesOrNo : string
var winID := Window.Open ("nobuttonbar")
loop
    put "Enter a word and I will tell you if it is a palindrome or not: " ..
    get word
    put reverse (word)
    if reverse (word) = word then
        put word, " is a palindrome."
    else
        put word, " is not a palindrome."
    end if
    put " "
    put "Test Another Word? (Yes (y) Or No (n)?)"
    get YesOrNo
    exit when YesOrNo = "n"
    cls
end loop
Window.Close (winID)


some of it is just fluff i added :P but hey it works and im happy

and if any1 would like to help me code wise if they see something that could be written better thatd be great. but i still MUST use the function and cant use procedures or process' cuz teacher is a meanie :(

-----------------------------------
ericfourfour
Thu Nov 23, 2006 8:38 pm


-----------------------------------
Just another optimization to your code.

put reverse (word)
if reverse (word)
You are doing the exact same calculation twice. You should store the reversed word in a variable and use the variable instead.


Another one.
if reverse (word) = word then
    put word, " is a palindrome."
else
    put word, " is not a palindrome."
end if
This can be reduced to.
put word, " is "..
if reverse (word) not= word then
    put "not "..
end if
put "a palindrome."
That should save you a few characters.


The last one.
put " "
put "Test Another Word? (Yes (y) Or No (n)?)"
These can both be on one line.
put " Test Another Word? (Yes (y) Or No (n)?)"

-----------------------------------
DemonZ
Thu Nov 23, 2006 8:38 pm


-----------------------------------
I know this is a off topic a little, but is everyones teacher a meanie? cuz mine will let us just do research and experiment with the help file and we use our own commands and our own logic of thinking, the only thing my teacher does care about is when using FIELDS instead of the locate command to make something line up.

Ok now for suggestions on your program, nothing really it looks good and organized, only thing i would give u advice on would be to declare your function after your variables, because thats regularly the order your supposed to declare them, the order is (u dont really have to folllow it)

global variables, constants created
procedures, functions, process
assigning values to global variables (if there are any that need to be)
main program structure (particularly a loop or repetative structure)

and thts really it, now u dont have to follow the order that way, but its recommended u do because it makes your program look nice and organized.

-----------------------------------
ericfourfour
Thu Nov 23, 2006 8:49 pm


-----------------------------------
global variables, constants created
procedures, functions, process
assigning values to global variables (if there are any that need to be)
Global variables (if you need them) should be assigned values in procedures (or functions).

If I were you I would keep the function above the variables. If it depends on a certain variable or constant then send it in through the parameters.

-----------------------------------
wtd
Thu Nov 23, 2006 9:36 pm


-----------------------------------
if reverse (word) = word then ...

Can you factor this test out into an "is_palindrome" function?

-----------------------------------
uberwalla
Thu Nov 23, 2006 10:03 pm


-----------------------------------
That should save you a few characters.
put " "
put "Test Another Word? (Yes (y) Or No (n)?)"
These can both be on one line.
put " Test Another Word? (Yes (y) Or No (n)?)"

well for this i had the line b4 so that i would skip a line so u can read. i just thought it looked a little nicer :P


and also id like to thank all of you for your feedback and help :D

-----------------------------------
Clayton
Thu Nov 23, 2006 10:15 pm


-----------------------------------
uberwalla, as wtd suggested, factor your check into a function to the effect of "is_palindrome".

Looking back at my first post, do you realize what I was trying to show you? Do you actually realize why your function wasn't working properly? Happening upon this answer by chance helps you alot less than figuring out where you went wrong and fixing it with what you know.

-----------------------------------
ericfourfour
Thu Nov 23, 2006 10:24 pm


-----------------------------------
That should save you a few characters.
put " "
put "Test Another Word? (Yes (y) Or No (n)?)"
These can both be on one line.
put " Test Another Word? (Yes (y) Or No (n)?)"

well for this i had the line b4 so that i would skip a line so u can read. i just thought it looked a little nicer :P


and also id like to thank all of you for your feedback and help :D

You got me (I don't know what I was thinking at the time). :D Anyway, you should put an empty string ("") instead of a space (" ") on the screen for the newline. Or you can use the newline character ("\n") and keep it on the same line if you want to be fancy.
put "\nTest Another Word? (Yes (y) Or No (n)?)"

-----------------------------------
wtd
Fri Nov 24, 2006 1:05 am


-----------------------------------
Testing for being a palindrome is best done using recursion.
