Can someone help me with the code to display the second largest value in a set of numbers using 'while loop'???

I have done programs using while loop to display the largest value, as well as the largest and smallest value.But this seems a bit tough for me.

This is just an extension of the largest value program. A super easy way to do it is two loops in succession. The first loop finds the largest value unconditionally, then the second loop finds the largest value excluding the result of the first loop:

max = first

while not eof do
  if value > max then
    max := value
  end if
loop

next_max = first

while not eof do
  if value < max and value > next_max then
    next_max := value
  end if
loop

don't forget second largest value can be the same as largest value
so you will want to count how many times the first value appears in the second while loop too.

>don't forget second largest value can be the same as largest value
Don't forget that this is a simple homework problem. You don't want to confuse the OP by adding unstated requirements.

Don't forget that this is a simple homework problem. You don't want to confuse the OP by adding unstated requirements.

Sorry, not my intention to cause confusion, but I imagine this task
is designed to gradually introduce the idea of sorting and ordering numbers.

And the most important part of any design problem is identifying the different situations that your code has to deal with.

IMHO it is part of the stated problem that there might be duplication of values as there is nothing saying otherwise:
say you had : 1, 6, 7, 8, 9, 3, 5, 6, 4, 9, 3
in which case I would interpret the seond largest number as second in the ordered list:
9, 9 , 8, 7, 6, 6 , 5, 4, 3, 1
which would be 9 not 8

Now while ordering all the numbers is obviously a lot more complicated than the question warrants being aware that the above case is a situation that the problem setter might be expecting the code to deal with, is important and represents part
of the design procedure.

Even if you are deliberately ignoring duplicate numbers and saying the 2nd largest is 8. It should be explicitly stated, to show an awareness that this is something that has been considered.

commented: G.A.Y -2
commented: iamthwee is being a little troll again +12

Nice try. Homework questions are generally not designed to be tricky. They are designed to teach. Narue is correct.

How about:

max = max2 = first

while not eof do
	if value > max then
		max2 := max
		max  := value
	else if value > max2 then
		max2 := value
	end if
loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.