Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help with this functions program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
uberwalla




PostPosted: Thu Nov 23, 2006 5:27 pm   Post subject: help with this functions program

ok in class i got to do this assignment:

Quote:

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 Razz and so i obviously tried.
ok so i got the base program of what i need:
code:

%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! Razz
lol
well help would be great Very Happy

thx in adv.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Thu Nov 23, 2006 5:36 pm   Post subject: (No subject)

code:

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




PostPosted: Thu Nov 23, 2006 5:44 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 23, 2006 5:56 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 23, 2006 6:52 pm   Post subject: (No subject)

well i know that kinda but to actually get marks i have to actually use a function. Sad

in class no1 has learned procedures so im not aloud to use them Sad

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 Very Happy
wtd




PostPosted: Thu Nov 23, 2006 7:04 pm   Post subject: (No subject)

I am not advocating using a procedure, but rather simply using functions properly. Wink
uberwalla




PostPosted: Thu Nov 23, 2006 7:45 pm   Post subject: (No subject)

yea Razz 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 Very Happy

code:

%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 Razz 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 Sad
ericfourfour




PostPosted: Thu Nov 23, 2006 8:38 pm   Post subject: (No subject)

Just another optimization to your code.

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.
code:
if reverse (word) = word then
    put word, " is a palindrome."
else
    put word, " is not a palindrome."
end if

This can be reduced to.
code:
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.
code:
put " "
put "Test Another Word? (Yes (y) Or No (n)?)"

These can both be on one line.
code:
put " Test Another Word? (Yes (y) Or No (n)?)"
Sponsor
Sponsor
Sponsor
sponsor
DemonZ




PostPosted: Thu Nov 23, 2006 8:38 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 23, 2006 8:49 pm   Post subject: (No subject)

Demonz wrote:
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




PostPosted: Thu Nov 23, 2006 9:36 pm   Post subject: (No subject)

code:
if reverse (word) = word then ...


Can you factor this test out into an "is_palindrome" function?
uberwalla




PostPosted: Thu Nov 23, 2006 10:03 pm   Post subject: (No subject)

ericfourfour wrote:
That should save you a few characters.
code:
put " "
put "Test Another Word? (Yes (y) Or No (n)?)"

These can both be on one line.
code:
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 Razz


and also id like to thank all of you for your feedback and help Very Happy
Clayton




PostPosted: Thu Nov 23, 2006 10:15 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 23, 2006 10:24 pm   Post subject: (No subject)

uberwalla wrote:
ericfourfour wrote:
That should save you a few characters.
code:
put " "
put "Test Another Word? (Yes (y) Or No (n)?)"

These can both be on one line.
code:
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 Razz


and also id like to thank all of you for your feedback and help Very Happy


You got me (I don't know what I was thinking at the time). Very Happy 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.
code:
put "\nTest Another Word? (Yes (y) Or No (n)?)"
wtd




PostPosted: Fri Nov 24, 2006 1:05 am   Post subject: (No subject)

Testing for being a palindrome is best done using recursion.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: