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

Username:   Password: 
 RegisterRegister   
 Writing a Word in Reverse
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
class_eff




PostPosted: Sat Jan 12, 2013 3:28 pm   Post subject: Writing a Word in Reverse

What is it you are trying to achieve?
The task is basically to enter a word and the program will output the word backwards. (e.g., "backwards" ; output = "sdrawkcab") Also, the program will notify the user if the word is a palindrome (e.g., "madam", "racecar").


What is the problem you are having?
I tried to think of the problem logically, but it didn't work. I think I'm not understanding what the actual task is.

Describe what you have tried to solve this problem
I tried to put the word entered into an array, and swap the positions of each letter in the array so that it appears backwards; however, that didn't work. I have the correct way of doing this program, but I'm not too sure what's actually happening in the code itself.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
This is my attempt. It's somewhat a rough draft so there's probably plenty of errors.

Turing:


var word : string

procedure swap2 (x : array 1..* of string)
var temp := 0

temp := x(1)
x(10) := x(1)
x(9) := x(2)
x(8) := x(3)
x(7) := x(4)
x(6) := x(5)
x(5) := x(6)
x(4) := x(7)
x(3) := x(8)
x(2) := x(9)
x(1) := x(10)
x(10) := temp

end swap2

for i : 1..length(word)
get word : *
swap(word)
end for



And this is the "correct" way of doing it.

Turing:


var word : string

function backwards (x : string) : string

var switch : string := ""

    for decreasing i : length(x)..1
        switch += x(i)
    end for

result switch

end backwards

put "Please enter a word."
get word

put backwards(word)

if word = backwards(word) then
    put "The word is a palindrome."
end if



Any help is greatly appreciated. Thank you in advance!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Jan 12, 2013 4:21 pm   Post subject: RE:Writing a Word in Reverse

The answer is obvious if you use two strings.
class_eff




PostPosted: Sat Jan 12, 2013 4:23 pm   Post subject: RE:Writing a Word in Reverse

Meaning?
Insectoid




PostPosted: Sat Jan 12, 2013 4:44 pm   Post subject: RE:Writing a Word in Reverse

Use one string to input the word, and the other string to store the reversed word. Put the swapped word in the 2nd string one letter at a time. How you do that is for you to figure out.
Panphobia




PostPosted: Sat Jan 12, 2013 4:50 pm   Post subject: RE:Writing a Word in Reverse

just use two strings and a loop that goes 0 to string.length and then just string2 = string1.charat(loop count) + string2
class_eff




PostPosted: Sun Jan 13, 2013 1:42 pm   Post subject: Re: RE:Writing a Word in Reverse

Panphobia @ 1/12/2013, 4:50 pm wrote:
just use two strings and a loop that goes 0 to string.length and then just string2 = string1.charat(loop count) + string2


Could you show me an example?

&&, Also, I was wondering if you could explain what's happening in the "correct" code.
Panphobia




PostPosted: Sun Jan 13, 2013 1:55 pm   Post subject: Re: Writing a Word in Reverse

just make your existing loop into an increasing loop and then just do switch := switch + x(i)
Insectoid




PostPosted: Sun Jan 13, 2013 2:28 pm   Post subject: RE:Writing a Word in Reverse

Christ, Panphobia. Don't just tell him how to do it. Point him in the right direction. Give hints, maybe. But don't give away the answer.

I guess it doesn't really matter though, since your answers are barely legible and often not even in the right language.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Jan 13, 2013 2:58 pm   Post subject: Re: Writing a Word in Reverse

There are a lot of problems with the code you listed:
- only supports strings with length of exactly 10
- Turing doesn't let you assign individual letters in a string, so x(10) := x(1) doesn't work.
- the 'temp' variable is assigned to 0, which makes it an integer, which cannot hold string or character values (without conversion)

The correct version of the code is pretty straightforward:

Turing:

function backwards (x : string) : string

    % This variable is poorly named. It will hold the "reversed" string as we build it.
    var switch : string := ""

    % Go backwards from the end of the given word to its start...
    for decreasing i : length(x)..1
        % At each letter, add that letter to another string.
        switch += x(i)
    end for

    % Return the reversed string.
    result switch
end backwards


You should choose a short word, like "dog" and try imagining how the computer will execute the code in the "backwards" function. Use a piece of paper to keep track of the values of variables. If you don't know what something does, look it up in the Turing help (also available online here: http://compsci.ca/holtsoft/doc/)

You should learn how to "read" code like this. Turing is a really easy language to read: it pretty much says what it's doing.
Panphobia




PostPosted: Sun Jan 13, 2013 3:27 pm   Post subject: RE:Writing a Word in Reverse

Insectoid, I know I should not be giving him the answer yes, but as for the different language stuff, I prefer to call it pseudo code, since concepts are transferable through languages
Insectoid




PostPosted: Sun Jan 13, 2013 3:40 pm   Post subject: RE:Writing a Word in Reverse

Quote:
I know I should not be giving him the answer


Then why do you keep doing it?

Quote:
I prefer to call it pseudo code


I know what pseudocode is, and what you write is not it. Even it it was passable pseudocode, the algorithms are not easily transferable across languages if OP only knows Turing. OP has no idea what dot notation is. He does not know what changes between languages and what stays the same. We're talking to people who have been programming for a much as a few weeks. Things that are trivial to you are next to impossible for many of the people here.
class_eff




PostPosted: Sun Jan 13, 2013 4:34 pm   Post subject: Re: Writing a Word in Reverse

DemonWasp @ 1/13/2013, 2:58 pm wrote:
There are a lot of problems with the code you listed:
- only supports strings with length of exactly 10
- Turing doesn't let you assign individual letters in a string, so x(10) := x(1) doesn't work.
- the 'temp' variable is assigned to 0, which makes it an integer, which cannot hold string or character values (without conversion)

The correct version of the code is pretty straightforward:

Turing:

function backwards (x : string) : string

    % This variable is poorly named. It will hold the "reversed" string as we build it.
    var switch : string := ""

    % Go backwards from the end of the given word to its start...
    for decreasing i : length(x)..1
        % At each letter, add that letter to another string.
        switch += x(i)
    end for

    % Return the reversed string.
    result switch
end backwards


You should choose a short word, like "dog" and try imagining how the computer will execute the code in the "backwards" function. Use a piece of paper to keep track of the values of variables. If you don't know what something does, look it up in the Turing help (also available online here: http://compsci.ca/holtsoft/doc/)

You should learn how to "read" code like this. Turing is a really easy language to read: it pretty much says what it's doing.


Thanks a lot. I really appreciate the clarification. Seems as though I was just over thinking it.Thanks again!
Panphobia




PostPosted: Sun Jan 13, 2013 4:36 pm   Post subject: RE:Writing a Word in Reverse

Sorry, I will not give any answers on here anymore.
Raknarg




PostPosted: Mon Jan 14, 2013 4:12 pm   Post subject: RE:Writing a Word in Reverse

Unless you can reasonably justify doing it, because there are some cases where a user probably won't profit by making him do more work than he has to. Some things are about learning, but some things can just be trivial.
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  [ 14 Posts ]
Jump to:   


Style:  
Search: