
-----------------------------------
brokeded
Mon Nov 07, 2005 9:48 am

Help with Arrays in a Procedure...
-----------------------------------
I am using Turing in my Computer Science class and we are currenly working on Procedures, and I need help on a question.

The question I have to do is:

  Write a procedure that will output the largest element of an array of size N.  The call to the procedure should be largestInArray( A, N).

(((( How would I do it? I am not to good with Procedures right now))))

-----------------------------------
Tony
Mon Nov 07, 2005 9:51 am


-----------------------------------
first - do you know how to do this without a procedure?

if so, then procedure is just a wrapper for that code, and instead of creating an array full of values, you're simply passing one as an argument.

-----------------------------------
codemage
Mon Nov 07, 2005 9:59 am


-----------------------------------
If you're learning procedures, you should probably already know how to step through an array.  You might want to take a look at the array tutorial for a refresher.

Your teacher probably expects you to step through using a loop and check each element against the biggest element you've found yet.

EDIT:  Kind of redundant to Tony's post now.  Took too long to respond with the reply window open.  :(

-----------------------------------
MysticVegeta
Mon Nov 07, 2005 2:47 pm


-----------------------------------
Isnt this sorting?
Also, what kind of elements are you talking about? Strings or ints? There are lotsa tutorials for sorting the array.

-----------------------------------
beard0
Mon Nov 07, 2005 3:35 pm


-----------------------------------
Given that your teacher wants you to use A and N as parameters, and Turing already has the upper function, are you sure that you aren't expected to write a function using recursion?  You should check with your teacher.

-----------------------------------
[Gandalf]
Mon Nov 07, 2005 3:52 pm


-----------------------------------
Well... the difference here is that you only need the largest element in the array, you don't have to worry about anything else.  This makes it much easier.

Ah, almost didn't catch beard0's post.  I doubt that at this level (also just learning procedures) that he knows about recursion.  They might need to use N because they haven't covered upper() yet.

-----------------------------------
MysticVegeta
Mon Nov 07, 2005 7:50 pm


-----------------------------------
I still support tony's post about how you know how to do it without using procedure. I dont think they have learned the procedure parameters yet so just have to wrap up that code in procedure i guess.

-----------------------------------
brokeded
Tue Nov 08, 2005 8:36 am


-----------------------------------
Given that your teacher wants you to use A and N as parameters, and Turing already has the upper function, are you sure that you aren't expected to write a function using recursion?  You should check with your teacher.

We have learned upper( ), but I am not to good at using it. Plus I can't use a Function because the next question tells me to do this same question but with a function. I just have to find a way to figure out the highest number, but the 'upper ( )'  command always comfuses me.

-----------------------------------
Tony
Tue Nov 08, 2005 8:54 am


-----------------------------------
 the 'upper ( )'  command always comfuses me.
if your procedure is largestInArray( A, N) then upper(A) will be the same as N

-----------------------------------
brokeded
Tue Nov 08, 2005 8:58 am


-----------------------------------
Alright, I got it to work. Here is the procedure I have, and it works, so I am happy.


procedure largestInArray (var A : array 1 .. * of int, N : int)
    var high : int := minint
    for i : 1 .. N
        high := max (high, A (i))
    end for
    put high
end largestInArray


Alright, for the next question, I have to:


Modify the above procedure so that it is a function that returns the largest value in the array.  No output is expected within the function.   The function call might be:         large := largestInArray(Array, N)


To bad I haven't even been taught functions yet. Now i have to go look throught the Turing Help post thing, and find functions and read up on how to use them.... gonna be a while :(

-----------------------------------
codemage
Tue Nov 08, 2005 9:10 am


-----------------------------------
Functions are friendly little guys; don't worry.

They're basically procedures that return back a value.

-----------------------------------
Tony
Tue Nov 08, 2005 9:17 am


-----------------------------------
so instead of

put high

you'll have

result high

you should read up on [url=http://www.compsci.ca/v2/viewtopic.php?t=407]functions and how they work

-----------------------------------
brokeded
Tue Nov 08, 2005 9:20 am


-----------------------------------
Yeah, I got it. My friend in class is a wiz with Turing, and he helped me learn how to use Functions. So I am done that question and am on to the next. Here is what I got for my function, and it seems to work.


function largestInArray (var A : array 1 .. * of int, N : int) : int
    var high : int := minint
    for i : 1 .. N
        high := max (high, A (i))
    end for
    result high
end largestInArray


-----------------------------------
[Gandalf]
Tue Nov 08, 2005 4:10 pm


-----------------------------------
My friend in class is a wiz with Turing, and he helped me learn how to use Functions.
Then he should come here :).

Here is the exact same thing, but using upper():
function largestInArray (var A : array 1 .. * of int) : int
    var high : int := minint
    for i : 1 .. upper (A)
        high := max (high, A (i))
    end for
    result high
end largestInArray
If that example isn't enough, read some of the tutorials we have on arrays, they should be enough to clear things up for you...

One is:
http://www.compsci.ca/v2/viewtopic.php?t=1117

-----------------------------------
GlobeTrotter
Tue Nov 08, 2005 4:54 pm


-----------------------------------
high : int := minint

seems like a strange way to approach this problem.  You're introducing needlessly large negative numbers.  Why not set high equal to A(1) then loop from the 2nd to the last.

-----------------------------------
MysticVegeta
Tue Nov 08, 2005 7:08 pm


-----------------------------------
I dont think that really matters because a variable's size is 24 bits no matter the value. i might be wrong though.

-----------------------------------
[Gandalf]
Tue Nov 08, 2005 8:23 pm


-----------------------------------
Nope, you can have integers (or other things) of different sizes...

var number : int1 is a 1 byte integer, you can have 1, 2, and 4 byte integers.  1 byte holds up to +127, 2 bytes up to +32767, and 4 bytes lead up to maxint.

-----------------------------------
MysticVegeta
Tue Nov 08, 2005 9:55 pm


-----------------------------------
I think he needs a regular int 4 byte int for this problem, since no limitation of the value of the elements given.

-----------------------------------
brokeded
Wed Nov 09, 2005 8:50 am


-----------------------------------
I dont' think I do. Because we haven't even learned about the int1, int2, and int4 things, and if we don't know about them, how does he expect us to use them?

I think the way I did will work fine for what he wants....

-----------------------------------
[Gandalf]
Wed Nov 09, 2005 4:27 pm


-----------------------------------
Relating to the above two posts:  I was merely explaining the possibility.
