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

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




PostPosted: Wed Nov 15, 2006 5:52 pm   Post subject: Code help

code:

var word : string
var count : int
%Enter Word%
procedure getname
    put "Enter a word"
    get word
    count := length (word)
end getname
%Prints word backwards
procedure backwards
    put "The word printed backwards is "
    for decreasing a : count .. 1
        put word (a) ..
    end for
end backwards
%Removes V
owels
procedure novowels
    put ""
    put "The word wihtout vowels is"
    for c : 1 .. count
        if word (c) = "a" or word (c) = "e" or word (c) = "i" or word (c) = "u"  then
        else
            put word (c) ..
        end if
    end for
end novowels
getname
backwards
novowels

With this program, i'm trying to input a letter and remove the vowels... I got this to work after toying around with the ifs. but i'm wondering why this works opposed to this type of coding...

code:

procedure novowels %This doesn't work...
    put ""
    put "The word wihtout vowels is"
    for c : 1 .. count
        if word (c) not= "a" or word (c) not= "e" or word (c) not= i or word (c) not= "u" then
            put word (c) ..
        end if
    end for
end novowels
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Nov 15, 2006 6:21 pm   Post subject: (No subject)

First thing's first... kill the global variables and use small, tightly focused functions and procedures (but only these if you really have to).

For instance:

code:
function get_name : string
   var name : string
   get name
   result name
end get_name

function backwards_string (word : string) : string
   var resulting_string := ""
   for decreasing character_index : length (word) .. 1
      resulting_string += word (character_index)
   end for
   result resulting_string
end backwards_string

function is_vowel (letter : string) : boolean
   var vowels : array 1..5 of string := init("a", "e", "i", "o", "u")
   for vowel_index : 1 .. 5
      if letter = vowels (vowel_index) then
         result true
      end if
   end for
   result false
end is_vowel

function strip_vowels (text : string) : string
   var resulting_string : string := ""
   for character_index : 1 .. length (text)
      if not is_vowel (text (character_index)) then
         resulting_string += text (character_index)
      end if
   end for
   result resulting_string
end strip_vowels
TokenHerbz




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

Nice wtd Smile
Wolf_Destiny




PostPosted: Fri Nov 17, 2006 11:11 pm   Post subject: (No subject)

Also the reason that:
Turing:
procedure novowels %This doesn't work...
    put ""
    put "The word wihtout vowels is"
    for c : 1 .. count
        if word (c) not= "a" or word (c) not= "e" or word (c) not= "i" or word (c) not= "u" then
            put word (c) ..
        end if
    end for
end novowels

Didn't work is due to the structure of your if statement. If letter not a OR e OR i OR o OR u then display the letter. Well the letter won't be more then one at any given time right? It can't be 'a' and 'u' right? So say it is 'a' lets analyse the IF statement.

if word (c) not= "a" %But is is equal to "a" so this is false
or word (c) not= "e" %It isn't equal to "e" so this is true
or word (c) not= "i" %It isn't equal to "i" so this is true
or word (c) not= "u" %It isn't eqaul to "u" so this too is true
then

So what is our final result from that?

false or true or true or true

Is either false or true = true? Yeah!
So that is true!
are any of: true or true or true = true? Yeah!
So that's true too!

The whole if statement is true, and always will be since only one parameter needs to be true for the whole thing to be true.

"So what do I do?"

Well since you don't want it to be ANY of the vowels, it can't be "a" AND it can't be "e" AND it can't be "i" ect...
So:

Turing:
var letter : string := "a"
if letter not= "a" and letter not= "e" and letter not= "i" and letter not= "o" and letter not= "u" then
put "Hey that letter: ",letter," isn't a LOWERCASE vowel!"
else
put "That was a vowel!"
end if


And if you want to do as wtd says, then kill the global variables...
Turing:
begin
var letter : string := "a"
if letter not= "a" and letter not= "e" and letter not= "i" and letter not= "o" and letter not= "u" then
put "Hey that letter: ",letter," isn't a LOWERCASE vowel!"
else
put "That was a vowel!"
end if
end

Ha! I did it without fancy neat and intelligent programming! Woot for begin and end!

~Wolf_Destiny
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  [ 4 Posts ]
Jump to:   


Style:  
Search: