Algorithms
and Programming eXercises #15
Compound Data Structures
The ARRAY - Solutions
The following
represent strategies/algorithms and NOT actual coded solutions - it is
thought there is definite advantage here for students to actually wrestle
with the actual code to get a working solution. There may be MANY different
ways to encode these solutions - get creative and try to be efficient
1
- Your
function needs a local byte variable used as a tally of the spaces.
The procedure should step through each element of the array (using
a for..to..do loop) if an element is a space, inc the tally. At the
end of the scan, assign the tally value to the the function name to
return it.
- the
is a simple loop assignment
- this
needs a loop, and could be achieved through a large case statement
(or HA/VHA students might like to use a more efficient index-based
tally system)
- very
similar to part a but instead of looking for a space, you should prompt
for and accept a letter into a character variable and then use this
variable in the same way you searched for a space
- building
on the previous part, this is an application of part d's solution
- look
back to the 'how random is random' exercise to see how you achieved
the graph of frequency
2. Unless
you can see the pattern, you will have difficulty with this activity.
line 1 and 2 are always 1's, but in line 3, you will notice the 2 (which
is a sum of the previous line's ones. From thereon, each number is a sum
of the 2 digits above it on the previous line. An array can help here
to keep the previous line and help you generate the next line
Strings
1, 2, 3:
these are 'try and see' what happens exercises that test your understanding
of string functions.
4. This
is a 'head' or 'tail' exercise - the 'head' is the stuff at the beginning
od the string, the 'tail' is the last few characters. You would use length(FRED)
to determine how many chars are in fred, and then some maths using 'n'
to determine the character positions you are interested for each of the
parts to this exercise
5.
- this
should be a relatively simple task of stepping along the string, replacing
each character with it's upcase value. Remember upcase does nothing
to characters that are not lower case letters
- trim
can be achieved by scanning a string, deleting spaces until you reach
a non-space - this will eliminate the head spaces. To eliminate
the tail spaces, you could continually delete the last character
if it is a space
- to
deblank, you could use a pre-tested indefinite loop that finds the
position of a space in the string, and deleting it. The loop
should terminate when the pos returns a zero
- collapse
could be achieved by looking for double spaces and replacing them
with single spaces - use a pre-tested indefinite loop to contiue doing
this while double spaces are found
- overwrite
requires you to use delete and then insert
- replace
also requires you to use pos and then delete and then insert. take
care that you test to see if the substring is actually there before
replacing it, otherwise 'odd' results will occur int he beginning
of the string
- replaceall
is a little more tricky - like parts c and d, you need to loop whilst
you find the substring, replacing it as you go - stop when there are
no more occurrences of it.
|