Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, Respected members, I'm trying to find the First Maximum value and Second Maximum value and their Index Too ..
up till now, i'm just getting the largest & index and 2nd largest.. Now I'm here as i'm confused about how to get the index for the 2nd largest value .. :(

How do i manage to get index of the 2nd largest value ..

Thanks For Your Consideration.

->(code here)->(cpp);
C++
#include <iostream>
using namespace std;


void setData(int a[],int size) {

	for(int i=0;i<size;i++) {
		cout <<"Enter element " << i+1 <<": ";
		cin >> a[i];
	}
}
void printData(int a[],int size) {

	for(int i=0;i<size;i++) {

		cout << a[i]<< endl;
	}
}
void foxi(int a[],int sizi) {

	int largest = a[0];
	int index, index2;
	int largest2 = a[0];
	for (int i=0;i<sizi;i++)
    {
        if(largest <= a[i])
        {
            largest2 = largest;
            index2 = i;
            largest = a[i];
            index = i;
        }
        else if (a[i] > largest2 && a[i] != largest)
        {
             largest2 = a[i];
        }
    }
    cout<<"largest = "<<largest << endl << " Index is = "<<index << endl
        << "largest2 = " << largest2<<" Index is = " <<index2-1 ;

	cout<<endl;
}


int main ()
{
	const int sizi=4;
	int a[sizi];

	setData(a,sizi);
	printData(a,sizi);
	cout << "\n";

	foxi(a, sizi);
	return 0;
}
Posted

Try
C++
    // initialize both indices to zero
    int index = 0, index2 = 0;

    for (int i=0;i<sizi;i++)>
{
    if(a[i] > largest)
    {
        largest2 = largest;
        // ensure you also don't lose the index of the largest so far
        index2 = index;

        largest = a[i];
        index = i;
    }
    else if (a[i] >= largest2 && index != i) // use >= instead of >
    {
         largest2 = a[i];
         // keep track of the second best index
         index2 = i;
    }
}


Hope this Helps :)

BCD
 
Share this answer
 
v3
Comments
Usman Hunjra 20-Mar-14 15:47pm    
BingO .. it works .. :) +5
Thanks ..
BupeChombaDerrick 21-Mar-14 7:18am    
You are welcome :) I have slightly improved the solution.
Philippe Mori 20-Mar-14 21:41pm    
Although it works in general, initialization should be modified slightly to properly handle a list of 2 items that would contains the minimum value twice. Also, it might be modified slightly if the list can have 0 or 1 item.
BupeChombaDerrick 21-Mar-14 7:31am    
I think the improvement to the solution can now handle the case of 2 items having the same value.
Usman Hunjra 21-Mar-14 16:08pm    
Thank you sir, for your help and suggestions .. :)
C#
int[] sorted_data = original_data;
Array.Sort(sorted_data);
int secondIndex = Array.IndexOf(original_data, sorted_data[1]);
 
Share this answer
 
v2
Comments
Philippe Mori 20-Mar-14 21:14pm    
This is C# code and not C++ code.
You can also use partial_sort method if you don't mind about ordering. http://www.cplusplus.com/reference/algorithm/partial_sort/[^]

Although for 2 item, it might be overkill, if you want say the 10 biggest items, it might be a good idea to use standard (STL) algorithms.

Otherwise solution 1 is appropriate and you might modify it slightly if for example, you want to properly handle the case when all values are the minimal possible value as that algorithm would return index 0 twice in such case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900