Algorithms (1 Viewer)

crammy90

Member
Joined
Mar 26, 2006
Messages
264
Gender
Male
HSC
2008
Hey, I have been looking over my notes and am still struggling with some aspects of coding
ok
in this algorithm it is finding the maximum value in an array through just a linear search


BEGIN FINDMAX
highindex = ARRAYLENGTH(element)
index = 1
maxval = element(index)
WHILE index <= highindex
IF element(index) > maxval
THEN maxval = element(index)
ENDIF
index = index + 1
ENDWHILE
DISPLAY "The maximum element is " maxval
END FINDMAX
</pre><br>I am having trouble understanding what the part in brackets is i.e. element(index)
im sorta guessing that the (index) part is taking the value of the element at index position 1, but what does the "element" part refer to before it?<BR>
And also if the (index) were to be presented as [index], what is the difference?
thanks alot if anyone can help me
 
Last edited:
Joined
Aug 15, 2006
Messages
842
Location
Sydney, NSW
Gender
Male
HSC
2007
Here is the algorithm with indents, and my comments to help you understand better:

Code:
BEGIN FINDMAX

// 'Highindex' is a variable, which stores the number of elements in the array.
// 'ARRAYLENGTH' is presumable a function, which returns the numnber of elements (or indexes) of an array
// The array in this case is 'element'.
highindex = ARRAYLENGTH(element)

// 'index' is a counter variable, which  will be used, to determine how long the while loop will execute for.
index = 1

// 'maxval' is a variable, which now gets set to the first index of the array 'element'. It is set to the first index, because the variable 'index', 
// which references an actual index in the array 'element' was set to 1 just above.
// This variable is being used for a secondary purpose here, apart from acting as a counter variable
maxval = element(index)

// Right so, while the 'index' variable is less than or equal to the 'highindex' variable, which is the last index of the array
WHILE index <= highindex
    // If the value in the 'element' array, with the index of 'index' is higher than the 'maxval' variable
    IF element(index) > maxval
        // Then it shall be the new 'maxval'
        THEN maxval = element(index)
    ENDIF
    // Increment the 'index' variable by one, for this to be a liniar search & for the loop to end eventually
    index = index + 1
ENDWHILE

// Print out the 'maxval' variable, which should hold the highest value in the array 'element'
DISPLAY "The maximum element is " maxval

END FINDMAX

Now as for brackets,
When they are used after an array - such as 'elements(index)' in the above algorithm, whatever is inside the brackets, denotes the index of the array. Many languages (in fact I think most do), use square brackets to denote the indexes of arrays to minimize confusion, however often that is not the case in pseudocode examples.

When the brackets are after a function - such as 'ARRAYLENGTH(element)' in the above example, whatever is inside them, is the input which is sent into the function.


Hope this clears things up!
 

crammy90

Member
Joined
Mar 26, 2006
Messages
264
Gender
Male
HSC
2008
Thankyou heaps man i understand so much more than i did before ur post but theres 1 thing i cant get right
i was deskchecking using this array
elements[21,6,5,4,40]
and so when i pass through, i notice i am setting the variable maxval to element(index), which using my array would be "21". So when we get down to the IF statement, it is saying:
IF element(index) > maxval
Which to me is saying:
IF 21 > 21 ?
Because we have previously set maxval to element(index).
which is not true which would then stop it from going into the statement.
What am i doing wrong
thanks heaps again to anyone
Im nearly there aha
 
Joined
Aug 15, 2006
Messages
842
Location
Sydney, NSW
Gender
Male
HSC
2007
You're right - it doesn't go into the THEN part of the IF statement - nor does it need to, because 'maxval' already holds the value of the highest element of the array, so in theory there is nothing more to be done at all, but in practise, the computer doesn't know this, so it must still compare it to every element of the array, including itself.

So whether or not it evaluates the condition inside the IF statement in your test case is quite irrelevant.

And of course, this is a rather silly algorithm, since we're starting off with the first index, we don't need to compare it with itself, but this is SDD for ya :D
 

crammy90

Member
Joined
Mar 26, 2006
Messages
264
Gender
Male
HSC
2008
ok awsome
i deskchecked it and understand it completely
you were heaps of help
thanks heaps
untill now ive got most of the written responses correct but lost 20% because i couldnt do the algorithm stuff
hopefully now i can do others and get the mark up:D
 

crammy90

Member
Joined
Mar 26, 2006
Messages
264
Gender
Male
HSC
2008
alrite well i had a go at one of the algorithms in the excel book and it makes sense to me but theres always a better way of doing it lol.

Q) An array of 1000 characters called "text" contains a sequence of sentences. Each sentence is terminated by a fullstop and the # character is used after the last fullstop.
Create an algorithm to process each character in the array. If a sentence does not start with a capital letter, the program is to change it to become a capital letter
e.g.
T|h|i|s| |i|s| |a|n| |e|x|a|m|p|l|e| |o|f| |t|h|e| |a|r|r|a|y|.| |g|o|o|d| |l|u|c|k|.|#|

My algorithm was:

INITIALISE
maxlen = ARRAYLENGTH(text)
index = 1
currentitem = item(index)
END INITIALISATION
FOR currentitem
capitalise letter
END
WHILE currentitem < maxlen
IF currentitem= "."
item(index+2) = capitalise letter
END IF
index = index + 1
ELSE
IF currentitem = maxlen
item(currentitem) = "#"
END IF
END WHILE
END


would that be at all right lol
thanks
 
Last edited:
Joined
Aug 15, 2006
Messages
842
Location
Sydney, NSW
Gender
Male
HSC
2007
crammy90 said:
alrite well i had a go at one of the algorithms in the excel book and it makes sense to me but theres always a better way of doing it lol.
Congratulates, you have the traits of a real programmer. Excel books are also pretty fail-prone, I used to love pointing out the mistakes in them - so don't always trust those books.


As for your algorithm, I'm really not sure what the question asks. If all you have to do is to capitalise all the starts of sentences, I'd do something like,

Code:
## First index of array is 1
BEGIN MAINPROGRAM
READ text
length = ARRAYLEN (text)
index = 2

WHILE index < text
   IF text(index-1) == "." THEN
      CAPITALISE text(index)
   END IF
END WHILE

END MAINPROGRAM
But I think you have the right idea there with your solution, and there is certainly no unique solution.

If you also had to put a # at the end, then make sure it's on index+1, where index would be the last index, otherwise you'd be overwriting the last element of the array.

Another good thing to do, is to clarify what the very first index of an array is, in your program. Since most languages use 0, but sometimes pseudo code uses 1, etc.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top