×

# How do I find the second largest number when I input an array of numbers?

 0 For example, if i enter 5 values, 5,10,3,5,7,8 I want a code to find the second largest value I dunno how to do it I'm new to this :) Don't h8 m8 asked 08 Sep '17, 21:31 7●4 accept rate: 0%

 0 ask me if you have any questions  #include using namespace std; int main() { int size; cin >> size; int array[size]; for(int i = 0;i> array[i]; int largest = array[0]; int index = 0; int secondLargest = array[0]; for(int i = 1;i
 0 if you are beginner then first try to find the largest of all the elements..(it can be done if you know how to use loops.) after this you can remove this(largest) element(it's index) and then run the same code on the remaining .. :) answered 08 Sep '17, 22:42 3★pk301 627●10 accept rate: 16% But what should I do after that? The syntax is confusing to me. Thanks for the reply though!! (08 Sep '17, 23:43)

You can sort the array first for ex

your array after getting sorted will look like this

3 5 5 7 8 10

so, you know that last element is the largest is 10

set the int max value equals to (max = array[size-1])

and then make a loop which will start from the end and go until it finds a different value than the max, that will be our second largest

for(int i = size-1; i>=0;i--)

{

if(array[i]!=max)

{

secondLargest = array[i];

break;

}

} this will give you the second largest number

and as pk301 mentioned that you can find the largest number but you don't have to delete the value just keep the track the index of the largest element and then loop through one more time and check

if(j(which was the index of largest)!=i) and array[i] is largest in the array after the maximum.

2★kunnu120
5189
accept rate: 5%

Is there a way to do it without sorting to keep it as efficient as possible? I'll try this to see if it works. Thanks! meow

(08 Sep '17, 23:52)

It's showing a compiling error...

(09 Sep '17, 01:18)

(09 Sep '17, 21:26) 2★

its working but the result isn't coming correctly...

# include<iostream>

using namespace std;

int main() { int i = 0;

int run[6], second_max;
cout << "Enter 6 numbers";
for (int i = 0; i < 6; i++)
cin >> run[i];
second_max = 0;
for (int i = 0; i < 6; i++);
{
int second_max = i;
}

cout << second_max << "send second max num of"<< run[second_max];

void waitforuserinput();
char exitchar;
cin >> exitchar;


}

(09 Sep '17, 23:22)
 0 try this http://www.geeksforgeeks.org/find-second-largest-element-array/ Hope this helps :) answered 09 Sep '17, 04:16 240●1●10 accept rate: 13% It can be done with single traversal only :) (09 Sep '17, 04:17) IT'S NOT WORKING AHHHHH (09 Sep '17, 17:33) Dont h8 m8 . (09 Sep '17, 21:55) @crash21499 it is working (09 Sep '17, 23:57) @beginner_1111 show me (10 Sep '17, 19:42)
 0 array = [] // some array // first is the maximum of the arrray first = max(array) f_time=False ; second= 0 // iterating to find the next max for i in range(len(array)):   if second < array[i] and array[i] != first:     second = array[i]   elif array[i] == first :    // in case there more than one elemnt that is the max     if f _time :       second = array[i]     else :       f _time= True // second has the second max in the array answered 09 Sep '17, 23:26 94●4 accept rate: 0% it isn't working.... (10 Sep '17, 19:52)
 0 Wow, This is exactly what I was looking for! Thanks for detailed and clear description! dissertation editing answered 10 Sep '17, 20:51 -239 accept rate: 0% what.....? (12 Sep '17, 01:23)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,901
×23
×7
×4

question asked: 08 Sep '17, 21:31

question was seen: 1,933 times

last updated: 12 Sep '17, 06:13