Hi, I can find the largest number. But I can not find the second largest number. I dont know where to start.

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

Erm,

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

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 …

## All 12 Replies

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

Erm,

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

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?

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.

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.

let me see, i'm thinking I need two inputs. Let me look at it closer

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.

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;

}``````

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?

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.

no , this logic works perfectly ! Thanks a ton

Be a part of the DaniWeb community

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