Ask your own question, for FREE!
Mathematics 20 Online
OpenStudy (lgbasallote):

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}\}\]

OpenStudy (lgbasallote):

does anyone know how to explain this?

OpenStudy (lgbasallote):

especially the argument of max

hartnn (hartnn):

\( \; max(a_1, a_2, \cdots, a_n: \; \text{integers})\) means that procedure accepts a1,a2,...an as integer values from the calling procedure

OpenStudy (lgbasallote):

layman's terms?

OpenStudy (shubhamsrg):

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..

OpenStudy (lgbasallote):

is it the same as this ``` int a1, a2, a3,..., an; ```

OpenStudy (lgbasallote):

by that i mean.. is the argument declaring a1, a2, a3, etc as integers?

OpenStudy (lgbasallote):

..probably not

hartnn (hartnn):

procedure calc() int y=max(5,7,1,4) a1 will be 5, a2=7 and so on

OpenStudy (shubhamsrg):

procedure is a function..

OpenStudy (lgbasallote):

calc()?

OpenStudy (lgbasallote):

we're talking about the argument of max @shubhamsrg

hartnn (hartnn):

calc() is just another procedure/function that uses max procedure and passes those integers as arguments, to find max integers among them

hartnn (hartnn):

and whats wrong with shubham's explanation? it seems correct to me

OpenStudy (lgbasallote):

first of all... "max" is just a name right?

OpenStudy (lgbasallote):

he wasn't talking about the one i'm asking

hartnn (hartnn):

yes, its just name, it can be lg also

OpenStudy (lgbasallote):

and procedure is the function..?

hartnn (hartnn):

yeah, just another name

OpenStudy (lgbasallote):

well i think i get the rest...but i really don't understand the argument of max...is it the input?

hartnn (hartnn):

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.

OpenStudy (lgbasallote):

but why a1, a2, a3,...., an : integers

OpenStudy (lgbasallote):

what is the significance of a1 -> an

OpenStudy (lgbasallote):

are those the values assigned to max?

OpenStudy (lgbasallote):

...suppose not

hartnn (hartnn):

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

OpenStudy (lgbasallote):

wait...go slow...too many lines there...

OpenStudy (lgbasallote):

what do you mean by max is a variable (on line max = a1)

hartnn (hartnn):

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...

hartnn (hartnn):

we need something to hold the max value we got so far, hence we need a avriable

OpenStudy (lgbasallote):

hmmm let me see if i get this right... a1, a2, a3, ..., an : integers means the input values are a1 -> an

hartnn (hartnn):

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

OpenStudy (lgbasallote):

wait...yes or no for my question first...

hartnn (hartnn):

YES! but only that would be incomplete

OpenStudy (lgbasallote):

then... a1 -> an are integers

OpenStudy (lgbasallote):

yes or no?

hartnn (hartnn):

yes

OpenStudy (lgbasallote):

so this is basically a declaration of the input values? <Y/N>

OpenStudy (lgbasallote):

i mean input variables

hartnn (hartnn):

yes

OpenStudy (lgbasallote):

hmm then the program proper i get... what about {max is the largest element} what's it for?

hartnn (hartnn):

after all iterations from 2 to n the variable max will hold the maximum of the input elements thats what is written

OpenStudy (lgbasallote):

i get what max is the largest element means... i was referring to those curly braces

OpenStudy (lgbasallote):

andalso *why* it's necessary to put {max is the largest element} at the end of the pseudocode

hartnn (hartnn):

O.o i thot it was just to specify that. idk it was necessary.

OpenStudy (lgbasallote):

well every pseudocode i see has that

OpenStudy (lgbasallote):

see?

OpenStudy (lgbasallote):

i suppose it's something that's like a comment

hartnn (hartnn):

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

OpenStudy (lgbasallote):

well seems i understand this...i think.....time to see if i really can...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!