# Board index » delphi » HELP:an algorithm is getting on my nerves...

## HELP:an algorithm is getting on my nerves...

Hello everyone.
I'm kinf of a newbie with pascal. I was reading a book the other day and
one of the exercises they sugested was writing an algorithm that selects
the heaviest student in a class. Perhaps I'm facing a mental block but I
can't figure it out. Can anyone help me on this one??Pleeeeeease.....

## Re:HELP:an algorithm is getting on my nerves...

Hello everyone.
I'm kinf of a newbie with pascal. I was reading a book the other day and
one of the exercises they sugested was writing an algorithm that selects
the heaviest student in a class. Perhaps I'm facing a mental block but I
can't figure it out. Can anyone help me on this one??Pleeeeeease.....
Pls reply by mail , too.

sincerely
P.P.

## Re:HELP:an algorithm is getting on my nerves...

##### Quote
eu wrote:
> Hello everyone.
> I'm kinf of a newbie with pascal. I was reading a book the other day
> and
> one of the exercises they sugested was writing an algorithm that
> selects
> the heaviest student in a class. Perhaps I'm facing a mental block but
> I
> can't figure it out. Can anyone help me on this one??Pleeeeeease.....
> Pls reply by mail , too.

> sincerely
> P.P.

Oh, P.S- I just read tha article of that other novice and just for
you not to think I haven't done my homework here's the catch.
1- suppose i have three students in my class would the following program
work. If it would, then why doesn't it?:)

program pesos;
var i, A, B, C, :integer;

BEGIN
writeln ('enter weight nr 1');
writeln ('...nr 2');
writeln ('nr 3');
if (A>B) and if (A>C) then writeln ('the heaviest is A');
if (B>A) and if (B>C) then writeln ('the heaviest is B');
if (C>A) and if (C>B) then writeln ('the heaviest is  C');
END.

2) what if my class has n students , i.e. i dono the number os students
and have to ask the operator....??

P.P.

## Re:HELP:an algorithm is getting on my nerves...

Try posting your code, so we will be able to tell you what you are doing
wrong.

--
Ken Robbins
puyrebel <AT> prodigy <DOT> net

"If everything seems to be going well, you have obviously overlooked
something."
-- Murphy's Eighth Law

eu wrote:

[snip]

## Re:HELP:an algorithm is getting on my nerves...

Well, what you could do is at the beginning have it ask for the # of students,
and then a readln(num), and then some code like this:

temp :=0
for I:=1 to num do
begin
writeln('Enter weight #',I)
if weight > temp then
temp:=weight
end
writeln('Heaviest person weighs',temp,'pounds')

Noah

##### Quote
>eu wrote:

>> Hello everyone.
>> I'm kinf of a newbie with pascal. I was reading a book the other day
>> and
>> one of the exercises they sugested was writing an algorithm that
>> selects
>> the heaviest student in a class. Perhaps I'm facing a mental block but
>> I
>> can't figure it out. Can anyone help me on this one??Pleeeeeease.....
>> Pls reply by mail , too.

>> sincerely
>> P.P.

>   Oh, P.S- I just read tha article of that other novice and just for
>you not to think I haven't done my homework here's the catch.
>1- suppose i have three students in my class would the following program
>work. If it would, then why doesn't it?:)

>program pesos;
>var i, A, B, C, :integer;

>BEGIN
>writeln ('enter weight nr 1');
>writeln ('...nr 2');
>writeln ('nr 3');
>if (A>B) and if (A>C) then writeln ('the heaviest is A');
>if (B>A) and if (B>C) then writeln ('the heaviest is B');
>if (C>A) and if (C>B) then writeln ('the heaviest is  C');
>END.

>2) what if my class has n students , i.e. i dono the number os students
>and have to ask the operator....??

>P.P.

## Re:HELP:an algorithm is getting on my nerves...

##### Quote
"McFly!!!!!!" <np2...@mail.telepac.pt> writes:
>> eu wrote:
>> Hello everyone.
>> I'm kinf of a newbie with pascal. I was reading a book the
>> other day and one of the exercises they sugested was writing an
>> algorithm that selects the heaviest student in a class. Perhaps
>> I'm facing a mental block but I can't figure it out. Can anyone
>> help me on this one??Pleeeeeease..... Pls reply by mail , too.
>> sincerely
>> P.P.
>    Oh, P.S- I just read tha article of that other novice and
> just for > you not to think I haven't done my homework here's
> the catch. 1- suppose i have three students in my class would
> the following program work. If it would, then why doesn't it?:)
> program pesos;
> var i, A, B, C, :integer;
> BEGIN
> writeln ('enter weight nr 1');
> writeln ('...nr 2');
> writeln ('nr 3');
> if (A>B) and if (A>C) then writeln ('the heaviest is A');
> if (B>A) and if (B>C) then writeln ('the heaviest is B');
> if (C>A) and if (C>B) then writeln ('the heaviest is  C');
> END.
> 2) what if my class has n students , i.e. i dono the number os
> students and have to ask the operator....??
> P.P.

-----------------------------------------------------------------
is an incorrect IF-THEN syntax.  If the question deals with
incorrect results, no meaningful comments are possible without
knowing what those results are and what they were expected to be.

Saying that you can't figure out an algorithm and then providing
source code you have written is a contradiction.  You cannot
produce source code without having some kind of algorithm.  In
fact, the algorithm must come first and should be independent of
the language being used.

Assuming each student's weight is different from all other
students' weights, one possible algorithm is:

1. Place the students in a single line
2. Take a piece of paper and write down the name and weight of
the heaviest known student.  Since no one has been weighed
yet, you could write something like "NO ONE  -  0 lbs".
3. Weigh the first student in line.  If his/her weight is
greater than the weight you have written down, erase what
you have written and replace it with the name and weight of
the student just weighed.
4. Repeat step 3 using the the 2nd student and then the 3rd,
etc.
5. When all the students have been weighed, the paper will
have the name and weight of the heaviest student written on
it.

Now all you have to do is implement the algorithm in the computer
language of your choice, which is what you're supposed to be
learning in class.
-----------------------------------------------------------------
Derek Asari
eq...@cleveland.freenet.edu

## Re:HELP:an algorithm is getting on my nerves...

##### Quote
McFly!!!!!! wrote:

> eu wrote:

> > Hello everyone.
> > I'm kinf of a newbie with pascal. I was reading a book the other day
> > and
> > one of the exercises they sugested was writing an algorithm that
> > selects
> > the heaviest student in a class. Perhaps I'm facing a mental block but
> > I
> > can't figure it out. Can anyone help me on this one??Pleeeeeease.....
> > Pls reply by mail , too.

> > sincerely
> > P.P.

>    Oh, P.S- I just read tha article of that other novice and just for
> you not to think I haven't done my homework here's the catch.
> 1- suppose i have three students in my class would the following program
> work. If it would, then why doesn't it?:)

> program pesos;
> var i, A, B, C, :integer;

> BEGIN
> writeln ('enter weight nr 1');
> writeln ('...nr 2');
> writeln ('nr 3');
> if (A>B) and if (A>C) then writeln ('the heaviest is A');
> if (B>A) and if (B>C) then writeln ('the heaviest is B');
> if (C>A) and if (C>B) then writeln ('the heaviest is  C');
> END.

The problem here is not understanding how IF works.  The syntax
for IF is
IF <logical test>
THEN <statement>

The lines you have above do NOT follow this syntax.  For example,

IF (B > A) AND <ifstatement>

where <ifstatement> is "if (A>C) then writeln ('the heaviest is A')".
Unfortunately, "(B > A) AND <ifstatement>" is not a proper logical test.
However, "B > A) AND (A > C)" is proper.  Try taking the second "if"
out and see if that helps.

##### Quote

> 2) what if my class has n students , i.e. i dono the number os students
> and have to ask the operator....??

Well, here again is where doing the experiment "in your head" would
help.  Suppose I ask you to tell me the weight of the heaviest student
in your class, or even in your school.  How would you do it, assuming
you could ask each student his/her weight?  One way would be to write
down all of the weights (which is the solution method you've chosen),
in which case you need to store an unspecified number of variables.
However, if all I want is the weight of the heaviest, I don't need to
store any weights (except the heaviest!), nor do I need to know how many
students I'll have (except to know it is > 0, i.e. at least 1).  Can
you see how to do it?  Try asking "Amy, how much do you weight?  Betty,
how much do you weight?  Charlie, how much do you weight?" and, without
having to remember all three weights, come up with the heaviest.  [I
dare say, if you actually ask those questions, you are likely to get

Bob Schor
Pascal Enthusiast

## Re:HELP:an algorithm is getting on my nerves...

In article <371A0D26.7C07F...@vms.cis.pitt.edu>,
Bob Schor <bsc...@vms.cis.pitt.edu> wrote:
[snip]
##### Quote
>      Well, here again is where doing the experiment "in your head" would
> help.  Suppose I ask you to tell me the weight of the heaviest student
> in your class, or even in your school.  How would you do it, assuming
> you could ask each student his/her weight?  One way would be to write
> down all of the weights (which is the solution method you've chosen),
> in which case you need to store an unspecified number of variables.
> However, if all I want is the weight of the heaviest, I don't need to
> store any weights (except the heaviest!), nor do I need to know how many
> students I'll have (except to know it is > 0, i.e. at least 1).  Can
> you see how to do it?  Try asking "Amy, how much do you weight?  Betty,
> how much do you weight?  Charlie, how much do you weight?" and, without
> having to remember all three weights, come up with the heaviest.  [I
> dare say, if you actually ask those questions, you are likely to get

> Bob Schor
> Pascal Enthusiast

That's good, Bob.  I would have tried to store all the weights and sort them.
But you're right.  If all that is needed is the one weight and name, why store
all the others?
----------