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
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 …
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.
But what should I do after that? The syntax is confusing to me. Thanks for the reply though!!
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
It’s showing a compiling error…
It can be done with single traversal only
IT’S NOT WORKING AHHHHH
Show me your code
ask me if you have any questions
#include <iostream>
using namespace std;
int main()
{
int size;
cin >> size;
int array[size];
for(int i = 0;i<size;i++)
cin >> array[i];
int largest = array[0];
int index = 0;
int secondLargest = array[0];
for(int i = 1;i<size;i++)
{
if(largest<array[i])
{
index = i;
largest = array[i];
}
if(i!=index)
{
if(secondLargest<array[i])
secondLargest = array[i];
}
}
cout << "second largest is " << secondLargest << endl;
return 0;
}
Dont h8 m8 .
I want one where i input a random set of numbers myself and it shows the second highest num
its working but the result isn’t coming correctly…
#include
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;
}
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
it isn’t working…
bro just take the input
Ok let me edit the solution for you
Okay I edited the solution first enter the size of the array and then your random set of numbers