Word Length Comparison
Author |
Message |
Fashoomp
|
Posted: Tue May 22, 2007 11:47 am Post subject: Word Length Comparison |
|
|
What if...lets say i wanted a program, where you enter in 10 words. How could turing find out which of those words is the longest? i assume it has something to do with a for loop, and length (word). And an array...i just cant figure out how to put it together. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
PaulButler
|
Posted: Tue May 22, 2007 12:10 pm Post subject: RE:Word Length Comparison |
|
|
No need for an array, just store the longest word in a variable and for each word the user types, set that variable to the typed-in word if it is longer. For example, (I don't know Turing, so this is pseudocode):
code: |
longestword = ""
while words remain
if wordLen(longestword) < wordLen(newword)
longestword = newword
|
|
|
|
|
|
|
Shades
|
Posted: Thu May 24, 2007 2:44 pm Post subject: Re: RE:Word Length Comparison |
|
|
I have this, as my program for an assigment, and since it's related, I'll just show you here:
code: | var info : flexible array 1 .. 400 of string
var counter : int := 1
var ans : string
var longchar : array 1 .. 4 of int
var biggest : int
%Make longchar array all = 1
for w : 1 .. 4
longchar (w) := 1
end for
%Just my proc for every program, just cls+View.Update
proc clear
cls
View.Update
end clear
%Fill the info array
proc ask
loop
put "Please enter your name"
get info (counter)
put "Please enter your phone number"
get info (counter + 100)
put "Please enter your city"
get info (counter + 200)
put "Please enter your email"
get info (counter + 300)
put "Any more data (Y/N)"
get ans
counter += 1
clear
exit when ans = "N"
end loop
end ask
ask
%These are to find the biggest value entered by the user
for i : 1 .. 100
if longchar (1) < length (info (i)) then
longchar (1) := length (info (i))
end if
end for
for q : 100 .. 200
if longchar (2) < length (info (q)) then
longchar (2) := length (info (q))
end if
end for
for k : 200 .. 300
if longchar (3) < length (info (k)) then
longchar (3) := length (info (k))
end if
end for
for n : 300 .. 400
if longchar (4) < length (info (n)) then
longchar (4) := length (info (n))
end if
end for
%So that we can determine the spacing
function space : int
for x : 2 .. 4
if longchar (1) < longchar (x) then
biggest := longchar (1)
end if
end for
for d : 3 .. 4
if longchar (2) < longchar (d) then
biggest := longchar (2)
elsif longchar (2) < longchar (1) then
biggest := longchar (2)
end if
end for
for g : 1 .. 2
if longchar (3) < longchar (g) then
biggest := longchar (3)
elsif longchar (3) < longchar (4) then
biggest := longchar (3)
end if
end for
for f : 1 .. 3
if longchar (4) < longchar (f) then
biggest := longchar (4)
end if
end for
result biggest
end space
%Make it look like columns, and display it
clear
put "Name" : space, "Phone" : space, "City" : space, "Email"
counter -= 1
for b : 1 .. counter
put info (b) : space, info (b + 100) : space, info (b + 200) : space, info (b + 300) : space
end for |
So yea, when I try and run it it works, up until the part where the length checkers come in, it highlights the first for loop of the series of 4 , and says "Variable has no value" |
|
|
|
|
|
Carey
|
Posted: Fri Jun 01, 2007 8:19 am Post subject: Re: Word Length Comparison |
|
|
its because there is nothing in the 2nd spot of the info array. |
|
|
|
|
|
|
|