Need Help on Arrays
Author |
Message |
Murphman
|
Posted: Wed Dec 16, 2009 2:19 pm Post subject: Need Help on Arrays |
|
|
What is it you are trying to achieve?
Create a program that has the user input a number of words (less than 20). The user should type done when finished. The program should then tell the user how many words were 3 characters in length or smaller and output the words, how many words were between 4 and 6 characters in length and output the words and how many words were 7 characters or larger and output the words.
What is the problem you are having?
The way my teacher wants it is wrong from how am i doing it is there any other way please help me i am stuck and it is due tommorow.
Describe what you have tried to solve this problem
tried to do what he said but got really lost and stuck.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
var word, word1 : array 1 .. 20 of string
var datafile, datafile1, datafile2 : int
%datafiles
open : datafile, "3.txt", put
open : datafile1, "7.txt", put
open : datafile2, "6.txt", put
% for loop
for x : 1 .. 20
put "Please type in some words. Must be less then 20 words type done when you are"
put "finished."
get word (x )
exit when word (x ) = "done"
%for loop
for i : 1 .. 1
put length (word (x ))
cls
%decides what data file the word gets put into by its length
if length (word (x )) = 3 then
put : datafile, word (x )
elsif length (word (x )) > 7 then
put : datafile1, word (x )
elsif length (word (x )) = 4 then
put : datafile2, word (x )
elsif length (word (x )) = 5 then
put : datafile2, word (x )
elsif length (word (x )) = 6 then
put : datafile2, word (x )
elsif length (word (x )) = 7 then
put : datafile2, word (x )
else
put ""
end if
end for
end for
close : datafile
close : datafile1
close : datafile2
|
Please specify what version of Turing you are using
4.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Wed Dec 16, 2009 2:34 pm Post subject: RE:Need Help on Arrays |
|
|
Use the <= (less than or equal to) operator, as well as the >= operator. Also, use 'and' to combine if statements; code: | if foo > baz and foo < foz then
...
end if |
|
|
|
|
|
|
mirhagk
|
Posted: Wed Dec 16, 2009 4:22 pm Post subject: RE:Need Help on Arrays |
|
|
your teacher wants you to put the answers in data files then output them from there??
seems kinda dumb, compsci teachers need to come up with more creative problems in my opinion |
|
|
|
|
|
Murphman
|
Posted: Thu Dec 17, 2009 10:25 am Post subject: Re: RE:Need Help on Arrays |
|
|
insectoid @ Wed Dec 16, 2009 1:34 pm wrote: Use the <= (less than or equal to) operator, as well as the >= operator. Also, use 'and' to combine if statements; code: | if foo > baz and foo < foz then
...
end if |
ok i that helps but how can i open the data files i tried and it won't let me. my teacher said today that there is a way to sort it in the array. But i have no idea how so do you know what needs to be done anoterh example of how to amke something sort would be great. Thanks in Advance |
|
|
|
|
|
mirhagk
|
Posted: Thu Dec 17, 2009 1:36 pm Post subject: Re: RE:Need Help on Arrays |
|
|
Murphman @ Thu Dec 17, 2009 10:25 am wrote:
ok i that helps but how can i open the data files i tried and it won't let me. my teacher said today that there is a way to sort it in the array. But i have no idea how so do you know what needs to be done anoterh example of how to amke something sort would be great. Thanks in Advance
so are you supposed to sort the array or are you supposed to put them into separate data files?? I'm confused.
The easy way to sort it would be make a couple more arrays (one for words 3-5 in length one for 6-7 etc whatever different groups there are)
Then basically do what your doing with the data files except put them into the arrays (you maybe should also have a variable to keep track of how many spots are used up in each array).
There obviously is a better (ie more complex) way to do it, and I'll explain that if you want me to okay?? |
|
|
|
|
|
Murphman
|
Posted: Thu Dec 17, 2009 2:24 pm Post subject: Re: Need Help on Arrays |
|
|
Thanks everyone for the help it helped alot so now i have it working without datafiles its sorting in the array. just got one more question is there away to make my new code shorter even more complex.
%Array
var word : array 1 .. 20 of string
%Variables
var x1, a1, b1, c1 : int
var word1, word2, word3 : string := ""
%Declaring Variables
x1 := 1
a1 := 0
b1 := 0
c1 := 0
put "Please type in some words. Must be less then 20 words type done when you are"
put "finished."
%loop
loop
get word (x1)
exit when word (x1) = "Done" or word (x1) = "done"
x1 := x1 + 1
exit when x1 = 22
end loop
cls
%for loop
for y : 1 .. x1 - 1
if length (word (y)) <= 3 then
a1 := a1 + 1
word1 := word1 + word (y) + ", "
elsif length (word (y)) = 4 or length (word (y)) = 5 or length (word (y)) = 6 then
b1 := b1 + 1
word2 := word2 + word (y) + ", "
else
c1 := c1 + 1
word3 := word3 + word (y) + ", "
end if
end for
%words that have 1-3 letters
put "You have ", a1, " words that have 1-3 letters:"
put word1
put ""
%words that have 4-6 letters
put "You have ", b1, " words that have 4-6 letters:"
put word2
put ""
%words that have 7 or more letters
put "You have ", c1, " words that have 7 or more letters:"
put word3 |
|
|
|
|
|
|
|