Not Yet Answered # second largest num

Rashakil Fol 978 Drowzee 3 Discussion Starter m7r23 Discussion Starter m7r23 Discussion Starter m7r23 Discussion Starter m7r23 jeevsmyd -2 Adak 419 OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

0

Erm,

```
if (num > max) {
max2 = max;
max = num;
}
else if (num > max2) {
max2 = num;
}
```

0

Though the question has been answered, 'where to start' is important.

Before you start coding, always think out what you want the program to do on paper.

For this particular problem, what you need to think about is:

If I can find the largest number, how can I use that information or process to find the second largest number?

Assuming you compare the current maximum to another number, you then update the variable with the largest number.

But that means that the number just discarded is (by definition) the second largest number yet encountered.

But you're not done yet. As Rashakil Fol pointed out, there are additional cases, where the new number may be smaller than the maxiumum but smaller than the second, so you have to account for that.

And, what if you have equal numbers for your maximum or second largest?

0

Ok, thanks it work. But for some odd reason I thought that I tried it just like that but it did not work for me. I will have to study this way to see where I went wrong.

0

ok, i tried many cases. Then only case that I found not to work is descending order. When I put the numbers in descendin order it always put the first number as it max and second max.

0

Ok, I look at it carefully and think I found the problem. I think it was me declaring the variable max wrong. I initialized it to num and now I changed it to 0.

0

ok, i'm trying to have a case if the numbers are equal. I can do three numbers. But the fourth number I cant get to work.

```
else if (max == max2)
{
max2=num;
}
```

0

but what if the 1st number in the array is the largest? so max should be assigned to 0 , right? if max is intialised as the first element of the array, then if the 1st element is the largest then 2nd largest will also be that number only, right?

but is max is assigned to 0 then the program wont work with negetive integers ! any possible solutions?

0

Assign the largest number initially, to the value of the first element of the array. Assign the 2nd largest number initially to INT_MIN (your compilers macro for the least possible value for an integer.

That makes the "mouth" for trapping the numbered values you want in the array, suitable.

(Your compiler may call it MIN_INT or something different, but you can check in the header file, limits.h, and you should include it).

If you get "stuck" in a really sticky logic situation in this problem, you can always just sort the values in the array, say descending, and then count down by one, for every new value in the array. That way duplicates don't goof it up.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I don’t want at this stage work on a big separate project as I've already got plenty ...