Discrete Mathematics question. Given a pseudocode: \[\textbf{procedure} \; max(a_1, a_2, \cdots, a_n: \; \text{integers})\] \[max = a_1\] \[\textbf{for} i = 2 \; \textbf{to} \; n\] \[\qquad \textbf{if} \; max < a_i \; \textbf{then} \; max = a_i\] \[\{\text{max is the largest element}\}\]
does anyone know how to explain this?
especially the argument of max
\( \; max(a_1, a_2, \cdots, a_n: \; \text{integers})\) means that procedure accepts a1,a2,...an as integer values from the calling procedure
layman's terms?
we let max = a1 then there's a loop,, we increase i one by 1.. first we get i2 if i2> i1 , then max = i2 if i2<i1, then max remains i1 then comes i3, we compare max with i3 the greater one equates with max. going on, we get comparison of max and in finally, we get to know the maximum value of procedure..
is it the same as this ``` int a1, a2, a3,..., an; ```
by that i mean.. is the argument declaring a1, a2, a3, etc as integers?
..probably not
procedure calc() int y=max(5,7,1,4) a1 will be 5, a2=7 and so on
procedure is a function..
calc()?
we're talking about the argument of max @shubhamsrg
calc() is just another procedure/function that uses max procedure and passes those integers as arguments, to find max integers among them
and whats wrong with shubham's explanation? it seems correct to me
first of all... "max" is just a name right?
he wasn't talking about the one i'm asking
yes, its just name, it can be lg also
and procedure is the function..?
yeah, just another name
well i think i get the rest...but i really don't understand the argument of max...is it the input?
we actually call a procedure to do some work. it needs some input to work on. so yes, the arguments are inputs to work upon.
but why a1, a2, a3,...., an : integers
what is the significance of a1 -> an
are those the values assigned to max?
...suppose not
max is a variable (on line max=a1) it can be assigned only one value. so for a start we assign it to 1st element, then keep on updating it.... and a1,a2,....an can be anything(non-integers also) but then max has to be of same type. here its taken as integers because your procedure is made to find max among integers only
wait...go slow...too many lines there...
what do you mean by max is a variable (on line max = a1)
the name of your procedure is 'max' the variable u used temporarily also is 'max' u could have used 'y' for that or something else...
we need something to hold the max value we got so far, hence we need a avriable
hmmm let me see if i get this right... a1, a2, a3, ..., an : integers means the input values are a1 -> an
and the values of a1 to an should be integer type the 1st value passed will go to a1, 2nd to a2 and so on
wait...yes or no for my question first...
YES! but only that would be incomplete
then... a1 -> an are integers
yes or no?
yes
so this is basically a declaration of the input values? <Y/N>
i mean input variables
yes
hmm then the program proper i get... what about {max is the largest element} what's it for?
after all iterations from 2 to n the variable max will hold the maximum of the input elements thats what is written
i get what max is the largest element means... i was referring to those curly braces
andalso *why* it's necessary to put {max is the largest element} at the end of the pseudocode
O.o i thot it was just to specify that. idk it was necessary.
well every pseudocode i see has that
see?
i suppose it's something that's like a comment
i think that is just the description of the variable where final answer is calculated, and it would be necessary to give it in the end of procedure. yes, like comment
well seems i understand this...i think.....time to see if i really can...
Join our real-time social learning platform and learn together with your friends!